Performance Analysis of statement object and preparedstatement object

Source: Internet
Author: User

 

1. Use statement instead of preparedstatement object
The JDBC driver is optimized based on what features are used. choosing preparedstatement or statement depends on how you use them. it is best to select statement for SQL statements that only run once. on the contrary, it is best to choose preparedstatement if the SQL statement is executed multiple times.

The first execution consumption of preparedstatement is very high. its performance is reflected in the subsequent repeated execution. for example, assume that I use the employee ID and prepared to execute a query for the employee table. the JDBC driver sends a network request to parse the data and optimize the query. the execution will generate another network request. in the JDBC driver, reducing network communication is the ultimate goal. if my program only needs one request during running, use statement. for statement, the same query only produces one network-to-database communication.

This guiding principle is somewhat complicated when the preparedstatement pool is used. when you use the preparedstatement pool, if a query is special and will not be executed again, you can use statement. if a query is rarely executed, but the statement pool in the connection pool may be executed again, use preparedstatement. use statement if it is not a statement pool.

Ii. Use the batch function of preparedstatement
When updating a large amount of data, prepare an insert statement is executed multiple times, which may lead to many network connections. to reduce the number of JDBC calls and improve performance, you can use the addbatch () method of preparedstatement to send multiple queries to the database at a time. for example, let's compare the following example.

Example 1: Execute prepared statement multiple times 
  1. Preparedstatement PS = conn. preparestatement (
  2. "Insert into employees values (?, ?, ?) ");
  3. For(N = 0; n <100; n ++ ){
  4. PS. setstring (name [N]);
  5. PS. setlong (ID [N]);
  6. PS. setint (salary [N]);
  7. Ps.exe cuteupdate ();
  8. }
Example 2: Use batch 
  1. Preparedstatement PS = conn. preparestatement (
  2. "Insert into employees values (?, ?, ?) ");
  3. For(N = 0; n <100; n ++ ){
  4. PS. setstring (name [N]);
  5. PS. setlong (ID [N]);
  6. PS. setint (salary [N]);
  7. PS. addbatch ();
  8. }
  9. Ps.exe cutebatch ();

In example 1, preparedstatement is used to execute the insert statement multiple times. here, 100 insert operations are performed, with a total of 101 network round-trips. one round trip is the pre-storage statement, and the other 100 round trips are executed for each iteration. in Example 2, when the addbatch () method is used in 100 insert operations, only two network round trips are performed. one round trip is the pre-storage statement, and the other is the execution of the batch command. although batch commands use more CPU cycles for databases, performance is improved by reducing network round-trips. remember, the biggest improvement of JDBC performance is to reduce network communication between JDBC drivers and databases.
Note: The maximum batch size of the JDBC driver of volume el 10g is 16383. If addbatch exceeds this limit, an "invalid batch value" (invalid batch value) exception occurs during executebatch. Therefore, if Oracle10g is used, the batch size must be limited before the bug is reduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.