Mysql大表查詢注意事項

husthxd發表於2016-04-23
在執行查詢時,Mysql預設把結果全部load到記憶體後再返回(這種模式可理解為Oracle的ALL_ROWS最佳化模式),如果表資料量太大的話,會導致記憶體溢位。
1.在mysql console連線資料庫時:
加入-q選項,mysql -h hostname -u root -p -q

2.在jdbc連線資料庫時:
在連線串中加入useCursorFetch=true
在建立的語句中,加入setFetchSize,如
stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,java.sql.ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(Integer.MIN_VALUE); 

注意:The Integer.MIN_VALUE is used by the MySQL driver as a signal to switch to streaming result set mode. It is not used as a value.
See the documentation, under "Resultset". In summary:
By default, ResultSets are completely retrieved and stored in memory. You can tell the driver to stream the results back one row at a time by setting stmt.setFetchSize(Integer.MIN_VALUE); (in combination with a forward-only, read-only result set).

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-2086751/,如需轉載,請註明出處,否則將追究法律責任。

相關文章