博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PreparedStatement 用问号传参引发查询(SQLServer )数据慢的解决方案
阅读量:6926 次
发布时间:2019-06-27

本文共 1478 字,大约阅读时间需要 4 分钟。

hot3.png

这个坑比较恶心,一般数据小的时候不容易发现,如果你用SqlServer可以参考看看

JDBC使用PreparedStatement 的问号传参方式默认会不走索引,导致查询非常慢,但查询分析器直接查就很快,貌似是传参的编码引起的:

确定是以Unicode还是以数据库的默认字符编码方式将字符串参数发送到SQL Server数据库。这会严重影响SQL Server 2000的性能,因为它不会自动转换类型(如7.0所示),这意味着如果索引列是Unicode并且字符串是使用默认字符编码提交的(或以其他方式)SQLServer将执行索引扫描而不是索引查找。对于Sybase,确定在服务器的字符集中不能编码的字符串是否作为unicode字符串发送。编码逻辑的性能受到影响,因此如果unitext或univarchar数据类型未使用或者charset为utf-8,则将此选项设置为false

解决方法:在jdbc-url上加上 ;sendStringParametersAsUnicode=false

jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test;sendStringParametersAsUnicode=false

也可以用传参的方式加上

Class.forName(DRIVER_CLASS_NAME);Properties info = new Properties();info.put("user", USERNAME);info.put("password", PASSWORD);info.put("sendStringParametersAsUnicode", "false");conn = DriverManager.getConnection(URL, info);

以上是最简单的解决方案,看网上也有说改变字段类型,varchar -> nvarchar ,没试过,不懂是否有效

官方文档介绍:

SendStringParametersAsUnicode={true | false}. Determines whether string parameters are sent to the SQL Server database in Unicode or in the default character encoding of the database. True means that string parameters are sent to SQL Server in Unicode. False means that they are sent in the default encoding, which can improve performance because the server does not need to convert Unicode characters to the default encoding. You should, however, use default encoding only if the parameter string data that you specify is consistent with the default encoding of the database. 

The default is true. 

 

转载于:https://my.oschina.net/zwtlong/blog/1789397

你可能感兴趣的文章
外媒:BAT增速超美国科技巨头将成常态
查看>>
我的友情链接
查看>>
【Unity】从Profile中窥探Unity的内存管理
查看>>
《谷歌如何测试》 三、四、五、六
查看>>
企业IT运维以及信息管理部服务器管理
查看>>
阿里云 企业邮箱开通指南【企业邮箱标准版-】
查看>>
saltstack(一)安装篇
查看>>
ansible(二)基础篇
查看>>
java 对xml文件的解析(转载)
查看>>
java 类加载机制与初始化顺序
查看>>
Kubernetes 架构浅析
查看>>
Docker 容器概念
查看>>
每日一shell(七)统计站点的IP和PV
查看>>
Excel 处理重复数据的几种方法
查看>>
php缓存技术总结
查看>>
1.2.1 Visual C++6.0
查看>>
oracle 使用plsql 创建表空间,用户以及分配权限
查看>>
PXE-KickStart无人值守安装简单操作
查看>>
8.[Think in Java笔记]并发
查看>>
Java synchronized使用
查看>>