Rule Definition
They are considered potentially costly because each one typically issues catalog queries against the database system tables to retrieve metadata (indexes, foreign keys, constraints, etc.), which can be expensive depending on the DBMS, schema size, and indexes.
and more TO ADD...
Remediation
Key tips to reduce cost:
Cache results if calling multiple times (e.g., store getPrimaryKeys/getIndexInfo results in a Map).
Limit catalog/schema scope instead of using null for all.
Use approximate=true for getIndexInfo if exact accuracy is not needed.
Avoid calling inside tight loops; batch metadata retrieval instead.
Violation Code Sample
1. For this java.sql.DatabaseMetaData.getIndexInfo
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getIndexInfo(null, null, "cars", false, false); // ⚠️ VIOLATION : As getIndexInfo(catalog, schema, table, unique, approximate=false)
2. If below methods were being used :
java.sql.DatabaseMetaData.getBestRowIdentifier // ⚠️ VIOLATION if calling method
java.sql.DatabaseMetaData.getCrossReference // ⚠️ VIOLATION if calling method
java.sql.DatabaseMetaData.getExportedKeys // ⚠️ VIOLATION if calling method
java.sql.DatabaseMetaData.getImportedKeys // ⚠️ VIOLATION if calling method
java.sql.DatabaseMetaData.getPrimaryKeys // ⚠️ VIOLATION if calling method
Fixed Code Sample
1. For this java.sql.DatabaseMetaData.getIndexInfo
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getIndexInfo(null, null, "cars", false, true); // ✅ FIXED: As getIndexInfo(catalog, schema, table, unique, approximate=true)
2. Avoid using below methods
java.sql.DatabaseMetaData.getBestRowIdentifier
java.sql.DatabaseMetaData.getCrossReference
java.sql.DatabaseMetaData.getExportedKeys
java.sql.DatabaseMetaData.getImportedKeys
java.sql.DatabaseMetaData.getPrimaryKeys
Reference
IBM native JDBC driver for Db2 for i. Performance tips for the native JDBC driver -
https://www.ibm.com/docs/en/i/7.4.0?topic=driver-performance-tips-native-jdbc#jdbcperf__datametacalls__title__1
JDK DatabaseMetaData (Java Platform SE 8)
https://docs.oracle.com/javase/8/docs/api/java/sql/DatabaseMetaData.html
Related Technologies
Technical Criterion
Efficiency - SQL and Data Handling Performance
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.