程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MYSQL JDBC快速查詢響應的方法,快速返回機制的實現

MYSQL JDBC快速查詢響應的方法,快速返回機制的實現

編輯:MySQL綜合教程

一直很糾結,Oracle的快速返回機制,雖然結果集很多,可是它能很快的顯示第一個結果,雖然通過MYSQl的客戶端可以做到,但是通過JDBC卻不行。

今天用了1個多小時,終於搞定此問題,希望對廣大Java朋友在處理數據庫時有個參考。

 

來由:

    通過命令行客戶端加上-q參數,可以極快的響應一個查詢。
    比如結果集為幾千萬的select * from t1,完整結果集需要20秒,通過-q參數顯示第一行只需要不到1秒。
    但通過jdbc進行查詢,卻不可以實現如上的效果,無論怎麼調整URL參數,也不行。
   
過程:
    查看了-q參數的解釋,如下:
    If you have problems due to insufficient memory for large result sets,
    use the --quick option. This forces mysql to retrieve results
    from the server a row at a time rather than retrieving the entire result set
    and buffering it in memory before displaying it. This is done by returning
    the result set using the mysql_use_result() C API function in the client/server
    library rather than mysql_store_result().
   
    可見,實現快速響應。
   
    查看 mysql_use_result() 函數,這個是C的API,如果通過C開發,可以用這個函數。
   
    那麼JAVA呢?
   
    查找便准JDBC規范裡面有關函數,沒有任何收獲。 setFetchSize()看上去有效,可在實際測試裡,無任何性能提升。
   
    搜索 JDBC mysql_use_result, 有了意外的收獲。
   
    在MYSQL的JDBC,com.mysql.jdbc.Statement 這個接口裡發現了如下的內容:
     abstract public  void disableStreamingResults() throws SQLException

    Resets this statements fetch size and result set type to the values they
    had before enableStreamingResults() was called.

 abstract public  void enableStreamingResults() throws SQLException

    Workaround for containers that 'check' for sane values of Statement.setFetchSize()
    so that applications can use the Java variant of libmysql's mysql_use_result() behavior.
   
   
  原來MySQL提供了自己的一個快速響應的實現。調整測試代碼
 
      stmt = (com.mysql.jdbc.Statement) con.createStatement();
      stmt.setFetchSize(1);
        //
        // 打開流方式返回機制
        stmt.enableStreamingResults();
       
        我期待的效果出現了。第一行數據被快速的現實出來,時間不到1秒中。
       
結論:
    MySQL在自己的JDBC驅動裡提供了特有的功能,來實現查詢的快速響應,
    特別是結果集非常大或者時間較長,而用戶非常想盡快看到第一條結果時特別有效。

 摘自:老紫竹的專欄

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