程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> sqlserver2000的jdbc驅動和PreparedStatement的性能問題。

sqlserver2000的jdbc驅動和PreparedStatement的性能問題。

編輯:JAVA編程入門知識

  人們都說用PreparedStatement會提高程序的性能。我在sqlserver下面試了一下,結果令我大吃一驚 。
  connection.setAutoCommit(false);
  pstmt = connection.prepareStatement(sql);
  pstmt.setFetchSize(100);
  pstmt.setString(1,"026011009004");
  ResultSet rs = pstmt.executeQuery();
  connection.commit();
  做一個查詢竟然需要6秒多,在數據庫裡數據很少的情況下很快的,但是數據庫表裡面的記錄多到100萬的時候查詢真的很慢。一開始我懷疑是jdbc驅動的事情可是我換了一個sqlserver的jdbc驅動結果還是一樣。
  
  當向pstmt 設置int類型的參數時性能又正常了。
  為什麼設置string類型的時候會出現的?令我百思不得其解。
  我查看sqlserver jdbc 驅動的文檔 發現裡面有這麼一個參數:
  SendStringParameters
  AsUnicode
  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
  
  原來string型的參數傳到數據庫裡面默認是轉換成unicode的。
  當我把SendStringParameters 設置成false時,查詢的性能得到了巨大的提高,原來用6秒的查詢現在只需要16毫秒了。
  問題解決了。
  


 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved