setFetchSizeã®å¤æ´ã«ããå½±é¿
JDBCçµç±ã§ãã¼ã¿åå¾ããã¨ããjava.sql.Statement#setFetchSize(int rows)ã§ãã§ããããè¡æ°ãæå®ã§ããããã®è¡æ°ãå¤ããã¨ããå®è¡æéãã©ã®ç¨åº¦å¤ãããã確èªããã
ã¾ãä¸è¨ã®é©å½ãªãã¼ãã«ãä½ãã¬ã³ã¼ãã100ä¸ä»¶å ¥ãã¦ããã
CREATE TABLE FROM_A_TBL (CLM VARCHAR2(16));
次ã«ãããªæãã®ã³ã¼ããå®è¡ãããå®è¡æéã®è¨æ¸¬æé ã¯ãåä¸ãã§ãããµã¤ãºã§ä¸åé£ç¶å®è¡ããããã®å¾ããã§ãããµã¤ãºãå¤æ´ãã¦å度å®è¡ãã¨ããæµãã
long start = System.currentTimeMillis(); Connection connection = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "xxxx", "xxxx"); Statement s = connection.createStatement(); s.setFetchSize(50);//ãããå¤æ´ ResultSet r = s.executeQuery("SELECT CLM FROM TO_A_TBL"); int i=0; System.out.println(r.getFetchSize()); while (r.next()) { String s1 = r.getString("CLM"); i++; } System.out.println(i); r.close(); s.close(); connection.close(); long end = System.currentTimeMillis(); System.out.println(end - start);
ä¸è¨ã®è¡¨ã®æéã¯end - startããã®ã¾ã¾è¼ãã¦ãããfetchSizeã®10ï¼ããã©ã«ãï¼ã¯setFetchSizeã§æ示çã«æ°å¤ãæå®ããªãã£ãå ´åï¼Oracleã®JDBCã¯10ãããã©ãããï¼ã§ããã
fetchSizeï¼æé | ï¼ | ï¼ | ï¼ |
---|---|---|---|
1 | 46937 | 48688 | 49297 |
10ï¼ããã©ã«ãï¼ | 5750 | 5781 | 5703 |
50 | 2078 | 2078 | 2000 |
100 | 1641 | 1594 | 1578 |
1000 | 1219 | 1110 | 1109 |
10000 | 1047 | 1047 | 1062 |
100000 | 1282 | 1297 | 1265 |
ãµã¤ãºã1ã«ããã¨ãã¯ãé©ç°çã«é
ããªãããã¯ãä¸ä»¶åå¾ãããã¨ã«DBã¨ã®éä¿¡ãçºçãããããªç¶æ³ã«ããã¨ç¸å½é
ããªãããã ã
ãã§ãããµã¤ãºãä¸ãã¦ããã¨ãåºæ¬çã«ã¯å¤§ããããåã ãã®ã¹ãã¼ãã¢ãããããããã ããç¡éã«ãã«ã¤æ°å¤ãªãè¯ãã¨ããããã§ããªãã100000ã¯10000ã«æ¯ã¹ã¦éã«é
ããªã£ã¦ããããããDBã¨ã®éä¿¡åæ°ãæ¸ãããããã¨ãã£ã¦ãä½ãã«å¤§ãããã§ãããµã¤ãºã«ãããã¨éã«ãã®å¦çã足ãå¼ã£å¼µãå§ããã¨ãããã¨ãªãã ããã
ãã®çµæã ãè¦ãã¨10000åå¾ãä¸çªé©æ£æ°å¤ã£ã½ãããããã¯ç°å¢ä¾åã¨æãããããã·ã³ã®ã¹ããã¯ã ã¨ãã®ãã¼ãæ§è½ããããã¯ãµã¤ãºãåãåããã¯ã¨ãªã®æ§è³ªããã£ãã·ã¥ã®ç¶æ
ã ã¨ãã®Oracleããã«ã¦ã§ã¢ã®è¨å®ããããã¯ã¼ã¯ã®åç·é度ãªã©ãªã©ã«å·¦å³ãããã®ã§ã¯ãªããããã
addBatch, executeBatchã®é »åº¦å¤æ´ã«ããå½±é¿
JDBCçµç±ã§ãã¼ã¿è¿½å ããã¨ããjava.sql.Statement#addBatch(String sql) ã java.sql.PreparedStatement#addBatch() ãªã©ã§è¤æ°ã®INSERTãUPDATEã追å ããjava.sql.Statement#executeBatch() ã§ã¾ã¨ãã¦DBã«éãè¾¼ãããexecuteBatchãaddBatchã®ä½åç®ã§å®è¡ããããå¤æ´ããã¨ããå®è¡æéãã©ã®ç¨åº¦å¤ãããã確èªããã
ã¾ãä¸è¨ã®é©å½ãªãã¼ãã«ãä½ãããã®ãã¼ãã«ã«100ä¸ä»¶INSERTãã¦ãããã¨ã«ãããã¾ããè¨æ¸¬ä¸åãã¨ã«DROPãã¦CREATEã¨é©å½ãªãã¼ãã«ãä½ããªããã¦ã«ã©ã«ãã¦ããã
CREATE TABLE FROM_A_TBL (CLM VARCHAR2(16)); DROP TABLE TO_A_TBL;
addBatch,executeBatch ã®åã«ããããã使ããã«100ä¸ä»¶INSERTããå ´åã®å®è¡æéãè¨æ¸¬ãã¦ãããããã°ã©ã ã¯ãããªæããå®è¡æéã®çµæã¯ãå¾è¿°ã®è¡¨ã«ã¾ã¨ãã¦è¼ããã
long start = System.currentTimeMillis(); Connection c = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "xxxx", "xxxx"); c.setAutoCommit(false); PreparedStatement p = c.prepareStatement("INSERT INTO TO_A_TBL VALUES (?)"); for (int i=0; i<1000000; i++) { p.setString(1, RandomStringUtils.randomAlphanumeric(16)); p.executeUpdate(); } p.close(); c.commit(); c.close(); long end = System.currentTimeMillis(); System.out.println(end - start);
次ã«ãããªæãã®ã³ã¼ããå®è¡ãããå®è¡æéã®è¨æ¸¬æé ã¯ãåä¸é »åº¦ã§ä¸åé£ç¶å®è¡ããããã®å¾ãé »åº¦ãå¤æ´ãã¦å度å®è¡ãã¨ããæµãã
long start = System.currentTimeMillis(); Connection c = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:XE", "xxxx", "xxxx"); c.setAutoCommit(false); PreparedStatement p = c.prepareStatement("INSERT INTO TO_A_TBL VALUES (?)"); int j=0; for (int i=0; i<1000000; i++) { p.setString(1, RandomStringUtils.randomAlphanumeric(16)); p.addBatch(); if (j == 10) {//ãããå¤æ´ p.executeBatch(); j=0; } j++; } p.close(); c.commit(); c.close(); long end = System.currentTimeMillis(); System.out.println(end - start);
ä¾å¤å¦çã¨ããåè¨ä»¶æ°ãé »åº¦ã§å²ãåããªãã£ããçµç«¯ã¯ã©ããªããã ã¨ããç´°ããçªã£è¾¼ã¿ã©ããã¯æ²¢å±±ãããããã®è¾ºã¯çç¥ã
ä¸è¨ã®è¡¨ã®æéã¯end - startããã®ã¾ã¾è¼ãã¦ãããããªããã¯addBatch,executeBatchã使ç¨ããªããã®ã®æ°å¤ã
é »åº¦ï¼æé | ï¼ | ï¼ | ï¼ |
---|---|---|---|
ãªã | 68781 | 69140 | 70031 |
10 | 10094 | 11203 | 10453 |
100 | 3672 | 5000 | 3922 |
1000 | 2985 | 4204 | 3000 |
10000 | 2820 | 2828 | 2891 |
100000 | 3063 | 5500 | 3688 |
ãã§ãããµã¤ãºã®æã¨åæ§ãDBã¨ã®éä¿¡åæ°ãæ¸ãã°æ¸ãã»ã©å®è¡æéãç縮ãããå¾åã«ããããªãã¨10ã®ã¨ãã¨ã§é絶ãã¦ããã®ãåæ§ã§ããã
ã¾ãããã®ç°å¢ã§ã¯10000ããããé度ã®ããã§ãããããããã§ãããµã¤ãºåæ§ãããããã®ç°å¢ã§ã®é©æ£æ°å¤ã¯ç°ãªãã¨æãããã
ã¾ã¨ã
- JDBCçµç±ã§å¤§éãã¼ã¿ãåå¾ããã¨ãããã§ãããµã¤ãºãããç¨åº¦å¤§ãããããã¨ã§å®è¡æéã®ç縮ãè¦è¾¼ããã
- JDBCçµç±ã§å¤§éãã¼ã¿ã追å ããã¨ããaddBatch,executeBatch ãé©åã«ä½¿ç¨ãããã¨ã§å®è¡æéã®ç縮ãè¦è¾¼ããã
ã¨ãã£ãã¨ããã確èªã§ãã¾ããã
2012/07/04追è¨
ãããã»ãµã¤ãºã100以ä¸ã®ç¯å²ã«ä¿ã¤ãã¨ããè¦ããã¾ãããããã大ãããªãã¨ãããã©ã¼ãã³ã¹ã¯ã»ã¨ãã©ãã¾ãã¯ã¾ã£ããåä¸ãããå®éã«ã¯ã大ããªããããå¦çããããã«å¿ è¦ãªã¯ã©ã¤ã¢ã³ãã»ãªã½ã¼ã¹ã®ããã«ãããã©ã¼ãã³ã¹ãä½ä¸ããå¯è½æ§ãããã¾ãã
Oracle Database JDBC開発者ガイド11gリリース2(11.2)- コーディングのヒント - 標準バッチ更新とOracleバッチ更新 ããæç²ã太åé¨åã¯ä¿ºã«ãããã®