Using Database Metadata Methods


Because database metadata methods that generate Resultset objects are slow compared to other JDBC methods, their frequent use can impair system performance. The guidelines in this section will help you to optimize system performance when selecting and using database metadata.

Minimizing the Use of Database Metadata Methods

Compared to other JDBC methods, database metadata methods that generate Resultset objects are relatively slow. Applications should cache information returned from result sets that generate database metadata methods so that multiple executions are not needed.

While almost no JDBC application can be written without database metadata methods, you can improve system performance by minimizing their use. To return all result column information mandated by the JDBC specification, a JDBC driver may have to perform complex queries or multiple queries to return the necessary result set for a single call to a database metadata method. These particular elements of the SQL language are performance expensive.

Applications should cache information from database metadata methods. For example, call getTypeInfo once in the application and cache away the elements of the result set that your application depends on. It is unlikely that any application uses all elements of the result set generated by a database metadata method, so the cache of information should not be difficult to maintain.

Avoiding Search Patterns

Using null arguments or search patterns in database metadata methods results in generating time-consuming queries. In addition, network traffic potentially increases due to unwanted results. Always supply as many non-null arguments to result sets that generate database metadata methods as possible.

Because database metadata methods are slow, applications should invoke them as efficiently as possible. Many applications pass the fewest non-null arguments necessary for the function to return success.

For example:

ResultSet WSrs = WSc.getTables (null, null, "WSTable", null); 

should be:

ResultSet WSrs = WSc.getTables ("cat1", "johng", "WSTable", "TABLE"); 

Sometimes, little information is known about the object for which you are requesting information. Any information that the application can send the driver when calling database metadata methods can result in improved performance and reliability.

Using a Dummy Query to Determine Table Characteristics

Avoid using getColumns to determine characteristics about a table. Instead, use a dummy query with getMetadata.

Consider an application that allows the user to choose the columns that will be selected. Should the application use getColumns to return information about the columns to the user or instead prepare a dummy query and call getMetadata?

Case 1: GetColumns Method

ResultSet WSrc = WSc.getColumns (... "UnknownTable" ...); 
// This call to getColumns will generate a query to  
// the system catalogs... possibly a join 
// which must be prepared, executed, and produce 
// a result set 
. . .     
WSrc.next(); 
string Cname = getString(4); 
. . .     
// user must retrieve N rows from the server  
// N = # result columns of UnknownTable 
// result column information has now been obtained 

Case 2: GetMetadata Method

// prepare dummy query  
PreparedStatement WSps = WSc.prepareStatement 
    (... "SELECT * from UnknownTable WHERE 1 = 0" ...); 
// query is never executed on the server -  
// only prepared 
ResultSetMetaData WSsmd=wsps.getMetaData(); 
int numcols = WSrsmd.getColumnCount(); 
... 
int ctype = WSrsmd.getColumnType(n) 
... 
// result column information has now been obtained 
// Note we also know the column ordering within the  
// table!  This information cannot be 
// assumed from the getColumns example. 

In both cases, a query is sent to the server, but in Case 1 the query must be evaluated and form a result set that must be sent to the client. Clearly, Case 2 is the better performing model.

To somewhat complicate this discussion, let us consider a DBMS server that does not natively support preparing a SQL statement. The performance of Case 1 does not change but Case 2 increases minutely because the dummy query must be evaluated instead of only prepared. Because the Where clause of the query always evaluates to FALSE, the query generates no result rows and should execute without accessing table data. For this situation, method 2 still outperforms method 1.


Previous Document Previous Page Next Document Next Page Synchronize TOC Synchronize with Contents