©2010 PROS Holdings, Inc. All rights reserved. My JDBC Performance Sucks and What I Did About It!

Slides:



Advertisements
Similar presentations
Using the SQL Access Advisor
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced Piloting Cruise Plot.
Our library has two forms of encyclopedias: Hard copy and electronic versions. The first is simply the old-fashioned "book on the shelf" type of encyclopedia.
S the of partnership power Evaluation Code Presenter: Dan DeBower Technical Consultant Systems & Computer Technology Corp. Tuesday,
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
5 Copyright © 2005, Oracle. All rights reserved. Accessing the Database with Servlets.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
16 Copyright © 2005, Oracle. All rights reserved. Using JDBC to Access the Database.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
My Alphabet Book abcdefghijklm nopqrstuvwxyz.
Performance Tuning Compiled from: Oracle Database Administration, Session 13, Performance, Harvard U Oracle Server Tuning Accelerator, David Scott, Intec.
9 Copyright © 2006, Oracle. All rights reserved. Automatic Performance Management.
13 Copyright © 2005, Oracle. All rights reserved. Monitoring and Improving Performance.
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
Overview of performance tuning strategies Oracle Performance Tuning Allan Young June 2008.
© 2009 VMware Inc. All rights reserved View Pool Image Configuration Considerations for Gold Images around Application virtualization and performance.
© Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi © Bharati Vidyapeeths Institute of Computer Applications and.
QA practitioners viewpoint
Database Performance Tuning and Query Optimization
ABC Technology Project
3 Logic The Study of What’s True or False or Somewhere in Between.
MySQL Access Privilege System
Ingres 10 Key Features GIUA 2010, Würzburg September 30 Sarkaut Mohn.
Go-Faster Consultancy Ltd.1 Experiences of Global Temporary Tables in Oracle 8.1 David Kurtz Go-Faster Consultancy Ltd.
VOORBLAD.
8 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using Explicit Cursors.
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Addition 1’s to 20.
25 seconds left…...
Global Payroll Performance Optimisation - I David Kurtz Go-Faster Consultancy Ltd.
Slippery Slope
Week 1.
We will resume in: 25 Minutes.
PSSA Preparation.
Chapter 13 The Data Warehouse
<Insert Picture Here>
CpSc 3220 Designing a Database
13 Copyright © Oracle Corporation, All rights reserved. Controlling User Access.
DEV351 ADO.NET Performance Pablo Castro Program Manager – ADO.NET Team Microsoft Corporation.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Getting connected.  Java application calls the JDBC library.  JDBC loads a driver which talks to the database.  We can change database engines without.
1 Robert Wijnbelt Health Check your Database A Performance Tuning Methodology.
© Dennis Shasha, Philippe Bonnet – 2013 Communicating with the Outside.
1099 Why Use InterBase? Bill Todd The Database Group, Inc.
Connecting to Oracle using Java November 4, 2009 David Goldschmidt, Ph.D. David Goldschmidt, Ph.D.
JDBC Session 2 Tonight’s topics: 1.Prepared Statements 2.Transaction Processing 3.Callable Statements & Stored Procedures 4.Scrollable & Updatable Result.
Copyright  Oracle Corporation, All rights reserved. 6 Accessing a Database Using the JDBC API.
Copyright © 2002 ProsoftTraining. All rights reserved. Building Database Client Applications Using JDBC 2.0.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Performance/Scalability with JDBC, UCP & Oracle Database 12c
SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Triggers, Procedures, PL/SQL. u Read Sections , 8.1, 8.5. Next u Transaction concepts, security.
JDBC Statements The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enables to send SQL or PL/SQL.
JDBC.
3 Copyright © 2006, Oracle. All rights reserved. Designing and Developing for Performance.
JDBC III IS Outline  Scrollable ResultSets  Updatable ResultSets  Prepared statements  Stored procedures.
JDBC III IS
Web Technologies IT230 Dr Mohamed Habib.
Stored Procedures – Facts and Myths
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

©2010 PROS Holdings, Inc. All rights reserved. My JDBC Performance Sucks and What I Did About It!

©2010 PROS Holdings, Inc. All rights reserved. Who am I? 2 Peter Tran Senior Performance Technical Lead Over 12 years with PROS 8 years working on server-side performance 6 years Oracle tuning 3 years SQL Server tuning

©2010 PROS Holdings, Inc. All rights reserved. Topics 3 Assumptions Transaction Isolation Levels Connection Batching Queries Parsing Misc

©2010 PROS Holdings, Inc. All rights reserved. Assumptions 4 Will not compare different driver type Recommendations should be driver type agnostic Examples in presentation uses Type IV Thin Driver Examples uses SQL Server 2008 and Oracle 10g databases Will not cover SQL optimization Will not cover JDBC resource leak

©2010 PROS Holdings, Inc. All rights reserved. Transaction Isolation Levels 5 Big impact on performance Critical for correct business logic java.sql.Connection Transaction Isolation LevelDirty-ReadsNon-RepeatablePhantomPerformance TRANSACTION_NONE N/A Fastest TRANSACTION_READ_UNCOMMITED Yes Fast TRANSACTION_READ_COMMITTED NoYes Medium TRANSACTON_REPEATABLE_READ No YesSlow TRANSACTION_SERIALIZABLE No Slowest

©2010 PROS Holdings, Inc. All rights reserved. Connection 6 java.sql.Connection Use connection pooling Big performance penalty to create new physical connection to database

©2010 PROS Holdings, Inc. All rights reserved. Batching 7 Batch UPDATE and INSERT SQLs Use java.sql.(Prepared/Callable)Statement Added benefit of preventing SQL Injection (security) Do not use java.sql.Statement Remember to setAutoCommit to FALSE SQL statement reuse Reduces parse

©2010 PROS Holdings, Inc. All rights reserved. Batch Insert Example – Wrong! 8 Wrong way to do batch update. //turn off autocommit con.setAutoCommit(false); Statement stmt = con.createStatement(); stmt.addBatch("INSERT INTO employees VALUES (1000, 'Joe Jones')"); stmt.addBatch("INSERT INTO departments VALUES (260, 'Shoe')"); stmt.addBatch("INSERT INTO emp_dept VALUES (1000, 260)"); // submit a batch of update commands for execution int[] updateCounts = stmt.executeBatch();

©2010 PROS Holdings, Inc. All rights reserved. Batch Insert Example 9 Right way to do batch update using PreparedStatement // turn off autocommit con.setAutoCommit(false); PreparedStatement stmt = con.prepareStatement( "INSERT INTO employees VALUES (?, ?)"); stmt.setInt(1, 2000); stmt.setString(2, "Kelly Kaufmann"); stmt.addBatch(); stmt.setInt(1, 3000); stmt.setString(2, "Bill Barnes"); stmt.addBatch(); // submit the batch for execution int[] updateCounts = stmt.executeBatch();

©2010 PROS Holdings, Inc. All rights reserved. Batch Query Example 10 Right way to do batch query using CallableStatement // turn off autocommit con.setAutoCommit(false); CallableStatement stmt = con.prepareCall("sp_GetEmployees (?, ?)"); stmt.setInt(1, 2000); stmt.setString(2, "Kelly Kaufmann"); ResultSet rs = stmt.executeQuery()

©2010 PROS Holdings, Inc. All rights reserved. Batch Insert Test Results 11 Test 1: Statement – No Batch Test 2: PreparedStatement – No batch/no reuse Test 3: PreparedStatement – No batch/reuse Test 4: PreparedStatement – With batch/reuse

©2010 PROS Holdings, Inc. All rights reserved. Batch Pitfalls 12 Forgetting to set autocommit to FALSE Excessively large batch size Writer blocks readers Use up more memory on database server ORA snapshot too old

©2010 PROS Holdings, Inc. All rights reserved. Query Performance 13 Set fetch size Settable java.sql.ResultSet java.sql.Statement java.sql.(Prepared/Callable)Statement Oracle 11.2g driver defaults to 10 SQL Server v3.0 driver defaults to 128 Reduces Physical/Logical I/O Uses more memory in client to hold data

©2010 PROS Holdings, Inc. All rights reserved. Query Performance 14 Fetch only data that is needed SQL Optimization – Physical/Logical IOs Reduce Network Use appropriate getXXX() method for retrieving data Minimize data conversion guessing Eliminates nasty conversion bug

©2010 PROS Holdings, Inc. All rights reserved. Microsoft Query Set Fetch Size Test Results 15 Test 1: Query 1M rows – fetch size = 10 Test 2: Query 1M rows – fetch size = 128 (default) Test 3: Query 1M rows – fetch size = 1M

©2010 PROS Holdings, Inc. All rights reserved. Oracle Query Set Fetch Size Test Results 16 Test 1: Query 1M rows – fetch size = 10 (default) Test 2: Query 1M rows – fetch size = 500 Test 3: Query 1M rows – fetch size = 1M

©2010 PROS Holdings, Inc. All rights reserved. Parsing 17 Parsing is expensive! Scalability killer for high volume OLTP applications Not as big of concern for OLAP applications Consumes CPU intensive Database engine not doing real-work Increase latch contention Increase memory usage SQL statement cache is a singleton Oracle: Shared Pool SQL Server: Plan Cache

©2010 PROS Holdings, Inc. All rights reserved. Parsing 18 Best place to fix is in the application code Use PreparedStatement or CallableStatement No Parse – Predicates uses bind variables and Statement reused Soft Parse – Predicate uses bind variables but Statement not reused Hard Parse – Predicate with literal values Parse TypePerformance Parse OnceBest Soft ParseGood Hard ParseBad

©2010 PROS Holdings, Inc. All rights reserved. Parse Fix - Hack 19 Do not have access to application code May select a suboptimal query plan Buggy

©2010 PROS Holdings, Inc. All rights reserved. Oracle Parsing Example 20 Oracle 11.2g JDBC Driver Test 1: Hard Parse (using Statement with literals) Test 2: Soft Parse Test 3: Parse Once

©2010 PROS Holdings, Inc. All rights reserved. Microsoft Parsing Example 21 Microsoft JDBC Driver version 3.0 Test 1: Hard Parse (using Statement with literals) Test 2: Soft Parse Test 3: Parse Once

©2010 PROS Holdings, Inc. All rights reserved. jTDS Parsing Example for SQL Server 22 jTDS JDBC Driver version Test 1: Hard Parse (using Statement with literals) Test 2: Soft Parse Test 3: Parse Once

©2010 PROS Holdings, Inc. All rights reserved. Parse Fix - Hack 23 SQL Server Use PARAMETERIZATION = FORCED Set at database level (Database Properties -> Options) Restrictive: Check Books-On-Line (BOL) YMMV – 5% improvements

©2010 PROS Holdings, Inc. All rights reserved. Parse Fix - Hack 24 Oracle CURSOR_SHARING = SIMILAR | FORCED Instance level or session level Use trigger to hack session level for each login CREATE OR REPLACE TRIGGER after_usr_logon AFTER LOGON ON.SCHEMA BEGIN EXECUTE IMMEDIATE 'ALTER SESSION SET CURSOR_SHARING=FORCE'; END; Super buggy Use SIMILAR – mixed work-load OLTP/OLAP Use FORCE – pure OLTP Use EXACT (default) – DSS/DW OLAP

©2010 PROS Holdings, Inc. All rights reserved. Driver Specific Improvements 25 Requires casting to drive specific implementation Your Performance May Vary (YPMV) Microsoft JDBC Driver version 3.0 Adaptive Buffer – reduces memory footprint in client to minimize Out of Memory but requires more round-trip to database (default=adaptive as of version 2.0) Oracle 11.2g JDBC Driver Statement and ResultSet Caching Connection Caching jTDS JDBC Driver version Implicitly caches PreparedStatement – not a new feature

©2010 PROS Holdings, Inc. All rights reserved. Database JDBC Driver Tuning Parameters 26 SQL Server Implicit Unicode conversion Use setStringParametersAsUnicodesetStringParametersAsUnicode Bypass index and use table scan

©2010 PROS Holdings, Inc. All rights reserved. My JDBC Performance Workout Plan 27 Maximize the usage of batching Set appropriate fetch size for queries Set the appropriate isolation level Reduce the hard/soft parses Before After

©2010 PROS Holdings, Inc. All rights reserved. 28 Q&A