追求jdbc on oracle最佳性能?如何才好?

30
追求JDBC on Oracle最佳性能?如何才好? Maclean Liu

Upload: maclean-liu

Post on 19-Jun-2015

176 views

Category:

Software


0 download

DESCRIPTION

追求Jdbc on oracle最佳性能?如何才好?

TRANSCRIPT

  • 1. JDBC on OracleMaclean Liu

2. Delphi()Parnassus Mount() (Oracle) 3. 4. Jdbc1for (i = 0; i < 1000000; i++) { conn = ds.getConnection(userid, password); pstmt = conn.prepareStatement("SELECT name FROM employees WHERE id = " + i); rset = pstmt.executeQuery();...... conn.close(); }connection connection Oracle 5. Jdbc1 : 150ms : 300tpsCPUSysUser 6. Jdbc11conn = ds.getConnection(userid, password); for (i = 0; i < 1000000; i++) { pstmt = conn.prepareStatement("SELECT name FROM employees WHERE id = " + i); rset = pstmt.executeQuery();...... }conn.close();connection 7. Jdbc11: 20ms: 3,000tpslatch: shared pool(parse) 8. Jdbc1conn = ds.getConnection(userid, password); for (i = 0; i < 1000000; i++) { { pstmt = conn.prepareStatement("SELECT name FROM employees WHERE id = " + i); rset = pstmt.executeQuery();...... }conn.close();SQL hard parse 9. Jdbc1conn = ds.getConnection(userid, password); for (i = 0; i < 1000000; i++) { { pstmt = conn.prepareStatement("SELECT name FROM employees WHERE id = ?"); pstmt.setInt(1, i); rset = pstmt.executeQuery();...... }conn.close(); 10. Jdbc11: 1ms: 25,000tpsCPU 11. Jdbc11PrepareStatementPrepareStatementSQLBindExecuteSQLFor PrepareStatementBindExecute}PrepareStatementFor BindExecute} 12. Jdbc11conn = ds.getConnection(userid, password); pstmt = conn.prepareStatement("SELECT name FROM employees WHERE id = ?"); for (i = 0; i < 1000000; i++) { { pstmt.setInt(1, i); rset = pstmt.executeQuery();...... }conn.close();For prepareStatement , http://www.oracle.com/technetwork/testcontent/jdbc- ch5-131209.pdf 13. Jdbc11: 1ms: 34,000tps 14. Jdbc11 15. Jdbc12open_cursorORA-1000 maximum open cursors exceededsession SQL*Net break/reset to clientOPEN_CURSOR 10000DBA 30000 16. Jdbc12try { ...... rset = pstmt.executeQuery(); ...... rset.close(); pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } 17. Jdbc12try { ...... rset = pstmt.executeQuery(); ...... rset.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (pstmt != null) try {pstmt.close();} catch (SQLException e) {...} }Finally TRY 18. Jdbc13processesCPUORA-00020 DBAprocesses 19. Jdbc13try { ...... rset = pstmt.executeQuery(); ...... rset.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } connection 20. Jdbc13try { ...... rset = pstmt.executeQuery(); ...... rset.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (pstmt != null) try {pstmt.close();} catch (SQLException e) {...} if (conn != null) try {conn.close();} catch (SQLException e) {...}}Finallyconnection TRY 21. Jdbc14sessionDBAkillsession? 22. Jdbc14try { ...... rset = pstmt.executeUpdate(); ...... conn.commit(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace();}commit session leak 23. Jdbc14try { ...... rset = pstmt.executUpdate(); ...... conn.commit(); } catch (SQLException e) { if (conn != null ) try {conn.rollback();} catch (SQLException e) {...} e.printStackTrace(); } finally { if (pstmt != null) try {pstmt.close();} catch (SQLException e) {...} if (conn != null) try {conn.close();} catch (SQLException e) {...} }rollback 24. Other tips:Set AutoCommit Offcommitcommitlog file syncConnection.setAutoCommit(false); 25. Other tips:InsertDriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());Connection conn = DriverManager.getConnection ("jdbc:oracle:oci8:@","scott","tiger");PreparedStatement ps = conn.prepareStatement ("insert into dept values (?, ?,?)");//Change batch size for this statement to 3((OraclePreparedStatement)ps).setExecuteBatch (3);ps.setInt (1, 23);ps.setString (2, "Sales");ps.setString (3, "USA");ps.executeUpdate (); //JDBC queues this for later executionps.setInt (1, 24);ps.setString (2, "Blue Sky");ps.setString (3, "Montana");ps.executeUpdate (); //JDBC queues this for later executionps.setInt (1, 25);ps.setString (2, "Applications");ps.setString (3, "India");ps.executeUpdate (); //The queue size equals the batch value of 3//JDBC sends the requests to the// databaseps.setInt (1, 26);ps.setString (2, "HR");ps.setString (3, "Mongolia");ps.executeUpdate (); //JDBC queues this for later execution((OraclePreparedStatement)ps).sendBatch ();//JDBC sends the queued request.. 26. Other tips:Prefetch RowsDriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@","scott","tiger");//Set the default row prefetch setting for this connection((OracleConnection)conn).setDefaultRowPrefetch (7);/* The following statement gets the default row prefetch value forthe connection, that is, 7.*/Statement stmt = conn.createStatement ();/* Subsequent statements look the same, regardless of the rowprefetch value. Only execution time changes.*/ResultSet rset = stmt.executeQuery ("select ename from emp");System.out.println ( rset.next () );while( rset.next () )System.out.println ( rset.getString (1) );//Override the default row prefetch setting for this statement( (OracleStatement)stmt ).setRowPrefetch (2);rset = stmt.executeQuery ("select ename from emp");System.out.println ( rset.next () );while( rset.next () )System.out.println ( rset.getString (1) ); 27. Oracle Database 11g R2 JDBC BasicFiles LOB Pre-FetchsetLobPrefetchsize()selectname, biofromLOB_tab1.Parse2.Execute3.Fetch metadata4.Fetch LOB dataFaster Lob Data Fetching by performingall operations on server in a singlesingle roundtrip 28. Oracle Database 11g R2 Zero-Copy SecureFiles LOBsetupSecureFile()Blob.getBytes()1.Fetch LOB data(bypass internal buffer)Faster SecureFiles operations by bypassing server-side buffer copy 29. JDBC Lob Pre-Fetch PerformancePerformance benchmark fetches CLOBs of sizes 1K to 50K with PREFETCH enabled and PREFETCH disabled,against BASICFILE LOBs and SECUREFILE LOBs. 30. Thank Youwww.parnassusdata.com400-690-3643