california Canada conference Conferences database db db2 DB2 pureXML development eclipse fall flickr google ibm Internet it java jdbc Joomla Linux mapping Open Source Other perl Personal Photography Portugal programming purexml rails rogers ruby shipment Software software-testing sql sqlj Tech toronto tpmg Travel twiki USA xml yahoo

JDBC performance tips

Tags: , , , , ,

If you are into java and database development, you will find this article to be a gold mine: http://www.javaperformancetuning.com/tips/jdbc.shtml

It contains links and summary to tens of other performance articles related with java database application development.

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)

Popularity: 10% [?]

Comments Off

SQLJ and JDBC

Tags: , , , , , , , , , , , , , , , , ,

As a follow up on my last post comparing Static SQL with Dynamic SQL, I will now post an example of how to run the same code using static and dynamic SQL.

One of my visitors left a comment saying that the scope of static and dynamic SQL in Oracle is different than the one I mentioned. I am not familiar at all with Oracle, but was able to find some information on their documentation where they compare JDBC and SQLJ. Since their concept of static vs dynamic SQL is different from the concept in DB2, so my examples may not make sense for Oracle users. I also found out that although Oracle has had plans to desupport SQLJ in its data server, that support has been reinstated in their 10g release.

The two code samples I will show next are shipped with DB2 (get your free copy of DB2 Express-C) and can be found in the file %DB2FOLDER%/samples/java/sqlj/TbRead.java. I’ll just use one of the several examples in that file, that executes a sub-select statement in the employee table.

Sample code in SQLJ:

#sql cur7 = {SELECT job, edlevel, SUM(comm)
	FROM employeeWHERE job IN('DESIGNER', 'FIELDREP')GROUP BY ROLLUP(job, edlevel)};
while (true){
	#sql {FETCH :cur7 INTO :job, :edlevel, :commSum};
	if (cur7.endFetch()){
		break;
	}
	System.out.print("Job: " + job + " Ed Level: " + edlevel + " Tot Comm: " +commSum);
}

Sample code in JDBC:

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT job, edlevel, SUM(comm) "
	+"  FROM employee "
	+"  WHERE job IN('DESIGNER','FIELDREP') "
	+"  GROUP BY ROLLUP(job, edlevel)");
	while (rs.next())
	{
	if (rs.getString(1) != null)
		{
		job = rs.getString(1);
		edlevel = rs.getString(1);
		commSum = rs.getString(1);
		System.out.print("Job: " + job + " Ed Level: " + edlevel + " Tot Comm: " +commSum);
		}
	}

Although both styles present different syntax, from a developer’s perspective, the only main difference is than when using JDBC one needs to explicitly fetch the row values into Java variables one by one. A common comment from Java developers is that SQLJ is not really Java (one needs to use annotations instead of java method calls), so they prefer to stick with JDBC.

Like I explained in my previous post, the biggest difference between these two styles (static SQL using SQLJ and dynamic SQL using JDBC) is that the SQL statements in the SQLJ files need to be compiled and bound to the database ahead of runtime. The following diagram illustrates this process:

staticSQL.jpg

After the deployment process, SQLJ execution is simpler than JDBC. While JDBC statements need to be prepared at execution time, SQLJ statements are already compiled and ready to use. The two following diagrams illustrate these differences:

jdbcstatement.jpgsqljstatement.jpg

As you can see, static SQL execution process is much simpler, but it requires a complex deployment process. This is an aspect of database development where there is a clash between DBAs and Developers. While ones – the DBAs – prefer the much more refined security and execution control provided by SQLJ and static SQL, others – the Developers – prefer the easier development process of dynamic SQL in the form of JDBC.

Soon, I will talk here about a new Java Data Access platform that supports the usage of both static and dynamic SQL at runtime (through a JVM property), allowing DBAs and Developers to use dynamic SQL on development and test environments and going with static SQL on the production environment. This way, the development community will get the best of both worlds: ease of deployment during development and testing phase and greater performance and control on the production environment.

If you are looking for a data management and application development tool, you should take a look at the new IBM Data Studio. It is an eclipse-based development environment, free to download and to use and with support to all major RDBMS. Download IBM Data Studio.

VN:F [1.9.11_1134]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)

Popularity: 12% [?]

3 Comments »

Static SQL vs Dynamic SQL

Tags: , , , , , , , , , ,

I have been wanting to write a few technical articles here on the blog, so I’ll start with something that is related with what I have been looking into at work: Static and Dynamic SQL.

From the conversations I have had with both DBAs and developers, it is clear that DBAs prefer static SQL, while developers prefer Dynamic SQL.

The difference between static and dynamic SQL is that static SQL needs to be compiled and bound to the database before application runtime, while dynamic SQL is compiled during runtime. Next, I’ll show a list of pros and cons regarding each one. 

Static SQL

Pros:

  • compile at bind time. Since the statement is compiled only once and before we run our workload, we have all the database resources in order to generate the most optimal query execution plan. In DB2, there are 9 levels of optimization, being 5 the default one. When we bing our application package, we can pick the highest optimization level – 9 – and get the most optimal execution plan. Using a higher optimization level requires more resources for the compile phase, but since our workload is not yet running, we can afford this high resources requirement.
  • security. Security is probably the most common reason why people use static SQL instead of dynamic SQL. Static SQL allows the DBA to set authorization at the package level. For example, consider an application package app1, that provides SQL functionality to select employee’s name and address from the table employees. The DBA can five user JOHN execution privileges on package app1, even if user JOHN does not have SELECT authority on table employees. Static SQL provides a much finer layer of security.

Cons:

  • need to bind before runtime. Although binding before runtime usually allows for more optimized access plans, doing this in a test or development environment can be cumbersome.
  • lack of tooling support. most of current IDEs provide coding assistance with support for APIs like JDBC. The lack of support from development tools discourages the use of static SQL.

Dynamic SQL

Pros:

  • IDEs and APIs: using eclipse to develop Java code that interacts with the database using JDBC or JPA is much simpler than developing a SQLJ application.
  • statement caching. Dynamic statement caching avoids the need to compile the same statement multiple times, increasing the performance to values close to static SQL. However, bear in mind that a cache miss will be extremely expensive.
  • better statistics. Because the statement is compiled at runtime, it uses the latest statistics available, contributing to a better execution plan.

Cons:

  • compile at runtime. There are a few reasons why compile at runtime can be a bad thing:

    • every time a statement is executed, it needs to be compiled, increasing the total statement execution time
    • the compile time will account for the total execution time, so using higher optimization levels may slow down the overall performance instead of improving it.
    • because the statement is only compiled at runtime, errors in the SQL statement won’t be detected until runtime.

As you can see, there are several reasons why you would choose one over the other. There is no perfect solution! But if you ask me, I would suggest the following: use Static SQL if security is your main concern and use Dynamic SQL if ease of development is your main concern.

VN:F [1.9.11_1134]
Rating: 5.9/10 (7 votes cast)
VN:F [1.9.11_1134]
Rating: +5 (from 5 votes)

Popularity: 24% [?]

9 Comments »