我正在讀取來自Access數據庫中的三個表的大量數據〜200萬行,可能是100MB的文本。 我用下面的SELECT語句:我的SQL PreparedStatement有什麼問題?
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + databaseLocation + databaseName +";selectMethod=cursor; READONLY=true; TYPE=FASTLOAD";
con = DriverManager.getConnection(url);
String SQL = "SELECT ADDRESS_MODEL.ADDR_LINE_1, ADDRESS_MODEL.ADDR_LINE_2, LOCALITIES.NAME, ADDRESS_MODEL.SECONDARY_LOCALITY, ADDRESS_MODEL.POST_TOWN, ADDRESS_MODEL.COUNTY, BUILDINGS.ED_ID FROM (ADDRESS_MODEL LEFT JOIN BUILDINGS ON ADDRESS_MODEL.BUILDING_ID = BUILDINGS.BUILDING_ID) LEFT JOIN LOCALITIES ON BUILDINGS.LOCALITY_ID = LOCALITIES.LOCALITY_ID WHERE BUILDINGS.COUNTY_ID = " + county_ID;
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery(SQL);
這是服用一段時間(約2分鐘),所以我就開始尋找使其更快。 我讀了關於準備好的陳述,並決定嘗試做一個。繼各種教程後,我把它放在一起:
String SQL = "SELECT ADDRESS_MODEL.ADDR_LINE_1, ADDRESS_MODEL.ADDR_LINE_2, LOCALITIES.NAME, ADDRESS_MODEL.SECONDARY_LOCALITY, ADDRESS_MODEL.POST_TOWN, ADDRESS_MODEL.COUNTY, BUILDINGS.ED_ID FROM (ADDRESS_MODEL LEFT JOIN BUILDINGS ON ADDRESS_MODEL.BUILDING_ID = BUILDINGS.BUILDING_ID) LEFT JOIN LOCALITIES ON BUILDINGS.LOCALITY_ID = LOCALITIES.LOCALITY_ID WHERE BUILDINGS.COUNTY_ID = ?";
PreparedStatement prest = con.prepareStatement(SQL);
prest.setString(1, county_ID);
ResultSet result = prest.executeQuery();
它仍然有效,但現在它需要超過15分鐘。我有一個很好的谷歌,但我似乎無法發現我做錯了什麼。準備好的陳述不適合我的特殊問題嗎?
多久,如果你直接對數據庫運行的查詢採取的主要論點? – JamesB 2012-08-06 19:45:31
我沒有具體的建議,但我建議您調查獲得更好的JDBC驅動程序。 – 2012-08-06 19:50:12
@JamesB幾乎幾乎沒有時間。不到一秒鐘。 – Russell 2012-08-06 19:50:55