Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Data Architect: Applied intelligence

Are you developing smart and putting DB2 to work?

Robert Catterall (rfcatter@us.ibm.com), IBM DB2 Specialist, IBM
Robert Catterall is an IBM DB2 specialist.

Summary:  Robert Catterall talks about how doing the wrong kind of work can reduce the value that your programmers deliver to the organization.This article showcases the sophisticated SQL optimization technology built into DB2® and illustrates how DB2 can simplify complex work for you. This content is part of the IBM Data Management magazine.

View more content in this series

Date:  21 Oct 2011
Level:  Introductory

Activity:  6609 views
Comments:  

Read this article in our interactive digital edition format!
Subscribe to IBM Data Management magazine

Even if you have a team of very talented application programmers, they can still be doing the wrong kind of work and reducing the value that they deliver to the organization. One glaring example that I often see: using application code to do things that DB2 can do more effectively.

In the 1980s, when DB2 was brand new, a lot of application developers were learning how to program using SQL (an IBM invention, by the way). Mastery takes time, but we're now 27 years after the introduction of DB2, and there are still programmers who are joining tables in their application code and failing to take advantage of the set-oriented nature of SQL. In other words, they're doing in their application code what DB2 should be doing. In this column I'll try to make the case for allowing DB2 to do all the work that it can for you.

Getting more value from programmers' time

Nobody likes to see a development group reinventing the wheel. When a programmer codes a useful routine in such a way that other programmers can't readily discover its functionality or easily use it, the result is needless duplication of effort as the others end up coding the same logic in their programs from scratch. It can also expand application maintenance time down the line—which then cuts into time for new application development—because similar functionality has to be updated in different programs.

DB2 can be a big help here. There are multiple ways to place logic within the DB2 level of an application, making the logic available to any program that accesses the database. For example, a trigger can cause an action (most anything that can be expressed in SQL) to be taken automatically in response to a row being inserted into, deleted from, or updated in a table. A trigger could be used to ensure that an employee's salary is within an allowable range by signaling an error if an update is outside that range. Triggers can also invoke DB2 stored procedures and user-defined functions, which are themselves other ways of encapsulating logic in the form of DB2 objects. A trigger could invoke a stored procedure that would issue a reorder for an item in inventory if an update of a row in a PARTS table were to take the number of in-stock items below a specified threshold.

The more data-centric logic is implemented in the DB2 database, the more programmers can focus on writing code that directly addresses the business needs of the organization. If there is data-related logic that you think could be broadly applicable in your enterprise, talk to a DB2 DBA about getting that deployed in the database layer of the application system. You could end up making every programmer in your organization more efficient and productive.


Future-proofing your applications

Over time, databases change. They frequently get larger, and characteristics of the data stored in the database shift. For example, a once almost-unique column in a table might come to have a large number of duplicate values. You want your applications to perform consistently through such changes, but this is tough to achieve if your programmers are doing work that DB2 should be doing.

Take the example of joining tables in application code. This means that the programmers are determining the data access path. Instantly, you have a problem: how do you know that they've made the right decision? Coding a cursor on table A, fetching a qualifying row, and looking for a match in table B is, in essence, what we call a nested loop join. How do you know that the nested loop method is the right way to join these tables? What about a merge join, a hybrid join (DB2 for z/OS), or a hash join (DB2 for Linux®, UNIX®, and Windows®)?

When DB2 performs the table join, the SQL optimizer determines the lowest-cost means of generating the result set, based on statistical information stored in the DB2 catalog. The DB2 Optimizer is very good at what it does, having been continually enhanced over the 30 years since IBM invented cost-based SQL statement optimization.

When the database changes over time, you get a second problem. Perhaps the database grows substantially, the query result set is much larger than before, and a different table join method will deliver better performance than the method initially selected. If DB2 is handling the join, the optimizer will automatically adjust the access path. Do the join in application code, and you either live with deteriorating performance, or you rewrite the program.

Similarly, programmatic joins can prevent—or complicate—database maintenance. Suppose that a table join performed through program code depends on the existence of a specific index. And suppose that index needs to be removed because the table has accumulated so many indexes that insert and delete operations are costing too much. (This happens all the time in database-land.) If DB2 were handling the join, it could switch to a different join method and deliver acceptable performance. But a hard-coded join needs to be rewritten. You can't change a join method hard-coded into a program (not without rewrite effort), so the programmatic join may stand in the way of DBAs making a physical database change that could reduce run times for critical data-change operations.

Here's the bottom line: when you let DB2 do as much as it can, you leave the access path selection process up to DB2. That, in turn, helps keep application performance consistent as the database changes over time. On top of that, consider that your organization is paying for the sophisticated SQL optimization technology built into DB2. Use it!


Enhancing CPU efficiency of applications

When a program issues an SQL statement, the statement must get from the program to DB2, and after the statement is executed, control must return to the application code. This program-to-DB2 round-trip is not free. Reducing the number of SQL statements in application code reduces the cumulative CPU cost of trips across the application program–DB2 boundary. The effect of getting the job done with fewer trips to DB2 can be significant, and here it's important to focus not on a single unit of work but on an overall workload. A seemingly small difference in CPU time per transaction can end up being a big deal when hundreds of transactions execute per second, or when a batch job has to work through hundreds of thousands of records in an input file.

To really get things tight from a CPU efficiency perspective, developers need to keep up with, and be prepared to take advantage of, new DB2 features and functions that can reduce chattiness between an application program and DB2. Consider, for example, use of the MERGE statement, with which one can make changes to a table based on a set of input records, updating target table data when there is a match with an input record and inserting a new row into the table when there is not. That's a CPU saver compared to the old methodology of doing a SELECT against the target table to see if there is a row that matches an input record and then driving an UPDATE (if there is a match) or an INSERT (if there's no match). Similarly, using multi-row INSERT (sometimes called block INSERT) to place several new rows in a table with one INSERTis a CPU saver, compared to the one-at-a-time way of doing things. Look for ways to accomplish data retrieval and changes with fewer SQL statement executions, and you'll reduce the load on your DB2 server.


Letting DB2 deliver for you

You really do the right thing when you let DB2 do all that it can do with SQL. And remember that some of this logic-implementing SQL—creation of triggers being an example—is in a DBA's domain, so get help there when you need it. Your organization will benefit from the accessibility and reusability of capabilities built into your DB2 database, application performance can be more consistently maintained as the database changes, and programs are likely to be more CPU-efficient. Don't program hard. Program smart.

Sponsored Article
The Internet is Your Oyster Safeguarding the Smart Grid with a Tactical Appliance Does Your Storage Have the Power to Support Mixed Workloads?
IBM, Intel Post Top Results for SAP Transaction BankingIBM DB2 Advanced Enterprise Server EditionRiding the Open Social-Content Wave
Critical Modeling Strategies for Insurance CompaniesVirtualized Business Intelligence Levels the Playing Field for Small and Midsize CompaniesGrace Under Pressure: ENOVIA V6 PLM Redefines Peak Workload Performance on DB2
DB2 is Pure Power for Growing BusinessesIBM Champions Connection
Partner Resources
Advent Global Solutions, Inc.Applied Analytix, Inc.ASG Software Solutions
BMCCogitoDassault Systèmes
Daeja Image SystemsDBIFuzzy Logix
Melissa DataNECNetezza
QueBIT Quest SoftwareRelational Architects International
Safari Books Online

Resources

About the author

Robert Catterall is an IBM DB2 specialist.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Information Management
ArticleID=766653
ArticleTitle=Data Architect: Applied intelligence
publish-date=10212011

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers