DB2 Explains Itself: A Roadmap to Faster Query Runtime

DB2 Explains Itself: A Roadmap to Faster Query Runtime

ave you ever wondered why some of your queries take so long to run? Or how DB2 interprets your requests?

As with all high-level programming languages, SQL must be translated into a set of smaller operations, which can be performed in turn. Because the efficiency of each operation can vary greatly with the nature of the data, considerable attention should be paid to selecting the best order of operations.

You can compare this with shopping. There may be many paths from your home to the shop you need to visit. You might want to park as close to the shop as you can, or perhaps park further away to avoid parking station costs and queuing to leave. There may be a smaller shop closer to home that has what you want. You may want to avoid a busy road. The best shopping path isn’t always obvious, and you could waste hours though poor planning.

To shorten a query’s runtime, you need to locate any weaknesses in the operation planning. How? IBM’s online documentation boasts: “DB2 provides the most comprehensive explain facility in the industry with detailed optimizer information on the access plan chosen for an explained SQL statement.” The explain facility outlines how the database will go about your work. This allows you to identify and correct any inefficiencies in your query planning.



How can I make my queries faster?



Use DB2’s EXPLAIN feature to access and evaluate how it plans to access your data.

Background: Plans and Costs
DB2 builds plans. A plan is basically the directions that will be followed to the data (or to the shops). To save time, some items on the plan can be performed in parallel (if you have a spouse, they might buy meat while you buy fruit).

Static plans are built once, and used many times?DB2 stores them when you BIND a collection of static SQL. It’s like establishing a favorite path for your shopping. You don’t have to think about it every time, as you have already decided on a good plan. Plans for dynamic SQL, however, must be evaluated each time. Each plan can be evaluated for its overall cost, which takes into account the nature of the data, and DB2’s understanding of the system on which it’s running. Critically important in this evaluation are the runtime statistics?the size of your tables, the cardinality of various columns, and the clustering in the indexes.

Figure 1. Color Coding: Here is a sample tree from “Visual Explain.”
Figure 2.
Drilling Down: Here is a sample details of a node from “Visual Explain.”


Show Me the Plan
There are several tools centered around explaining DB2’s plans:

  • Visual Explain is a versatile graphical tool available in the Control Center. It is well suited to examining single queries, whether static or dynamic. Each operation shows as a color-coded node in a tree structure. Clicking on a node allows you to view the arguments, statistics, and cost estimate of the node. You can also jump directly to DB2’s documentation on that node type.
  • Visual Explain can also be run from the command line as db2vexp.exe, though it then lacks static SQL ability.
  • Db2expln is the “bare bones” tool, giving text output from static SQL packages only. This can optionally include a character mode graph.
  • Dynexpln gives a text-mode analysis of a dynamic SQL query. It actually packages the dynamic query and calls db2expln to do the work. Listing 1 shows an example of the text output from dynexpln, including a graph.
  • Db2exfmt is a formatter for previously stored explain data.
All these tools use a set of data known as the “explain tables.” Visual Explain is capable of creating these tables on demand. With the other tools, you’ll need to run a script such as in Listing 2:



C:PROGRA~1SQLLIBin>cd ..miscC:PROGRA~1SQLLIBmisc>db2 connect to sample   Database Connection Information Database server        = DB2/NT 7.2.2 SQL authorization ID   = GRNASH Local database alias   = SAMPLEC:PROGRA~1SQLLIBmisc>db2 -tf explain.ddl******* IMPORTANT **********USAGE: db2 -tf EXPLAIN.DDL******* IMPORTANT **********DB20000I  The UPDATE COMMAND OPTIONS command completed successfully.

Now you can assess the results.The Plan Explained

Figure 3. Examining the Nodes: This larger graph shows some nodes feeding multiple higher nodes (using temporary tables).

Whichever explain tool you use, you will have an estimate of the cost of the query in “timerons”. A “timeron” is an abstract unit of measure. While it doesn’t directly equate to any actual elapsed time, it does give a rough relative estimate of the resources (cost) required by a database manager to execute an access plan. You can try different variations of a query, different optimization levels or other changes, and look for a lower cost to indicate a faster result. Working from the bottom up, examining the entire plan may help you find specific inefficiencies. The explain lists and graphs show how the required data is located, collated, and prepared for return. Note that the graph isn’t always a tree – it takes sensible shortcuts if it saves performing the same operation twice. At each node, you can examine the arguments to the operator, the estimated cost of the node, and the cumulative cost of all operations up to this point.

Here are the main node types:

  • UPDATE, DELETE, INSERT: perform the operations named.
  • FETCH: retrieves data from a table, given a specific pointer to the record.
  • TBSCAN: A table scan is examination of an entire table. It’s a bit like visiting every aisle in a supermarket and examining every item. It’s necessary at times, but can be most inefficient when you want a small portion of a large table (or shop).
  • IXSCAN, EISCAN, RIDSCN: Index Scanning. Indexes are pointers to rows, sorted for fast access. It’s like having a store directory, so you can go directly to the products you want. EISCAN operates with multiple ranges of values. RIDSCN retrieves the row pointers (Row IDs) from a set of index records.
  • FILTER: reduces a set of records to only those required
  • UNIQUE: removes duplicates
  • Joins – NLJOIN, MSJOIN, HSJOIN, UNION, IXAND: Join two record sets. The method chosen depends on the query and on the relationships between the tables involved. IXAND joins the results of index scans
  • GRPBY: Group By. Groups rows, and performs column function calculations on the groups.
  • TEMP: stores a set of results for re-use elsewhere in the query.

That covers the basic operators. There’s a lot more information than just the node types and estimated cost. You can find it in IBM’s “Description of db2expln and dynexpln Output.”

Reducing the Cost
Now that you can read the price tags, it’s time to reduce the cost. Here are some guidelines:

1.) Better-informed decisions. DB2’s optimizer must be well informed to make the best decisions.

  • Use the RUNSTATS to update the statistics. If your data has grown, or you’ve added or removed indexes since the last RUNSTATS, DB2’s estimates will be inaccurate.
  • If your tables aren’t fully populated, the runtime statistics will be inappropriate. Under such conditions you’ll often find tablescans in your plans, in spite of sensible indexes. You can tell the optimizer to anticipate extra data by marking certain tables as VOLATILE. (Use the Control Center, or the ALTER TABLE command).

2.) Don’t buy junk. It’s a simple rule – don’t buy what you don’t need. This includes:

  • Don’t use “SELECT * ” when you don’t need all the columns.
  • Use a WHERE clause when possible. A common beginner’s mistake is to use HAVING instead of WHERE. “HAVING” is like analyzing every item in a supermarket, then discarding those you don’t want. Sometimes that’s what you need, but it’s quicker if you can say “I only want to look at the breakfast cereals.”
  • If you only want the first row(s) of a large result set, use FETCH FIRST (see my previous solution “Mastering Top-N and OLAP Queries in DB2“).
  • Look carefully at your joins. The sooner (in the plan) you can narrow down the records you want, the less work it will be. Compare Listing 3 and Listing 4. They represent the same query, but by re-ordering the joins, the cost has been halved. (The reason for the cost difference relates to how many rows are in each table).

3.) Keep an index handy. Many columns can be used in an index, and the order of the columns is significant. You might want to use more than one index.

  • DB2 provides a clever tool, the SQL Advise Facility. It’s available from the Control Center (Indexes – Create – Index using wizard), and from the command line as db2advis. The advisor takes a query, examines the generated plan, attempts to identify additional indexes that may help, and re-evaluates the cost with and without each of the indexes. The end result is a list of potentially helpful indexes, with estimates of their size and helpfulness.
  • You can also INCLUDE[] unsorted data in an index, to completely avoid fetching from the table in some instances.

4.) Use a Summary Table. See my solution “Tame Beastly Data with Summary Tables.”

While there’s much more that can be learned on your way to writing efficient queries, you’ve got a good start in knowing how to measure the impact of your efforts.

devx-admin

devx-admin

Share the Post:
Apple Tech

Apple’s Search Engine Disruptor Brewing?

As the fourth quarter of 2023 kicks off, the technology sphere is abuzz with assorted news and advancements. Global stocks exhibit mixed results, whereas cryptocurrency

Revolutionary Job Market

AI is Reshaping the Tech Job Market

The tech industry is facing significant layoffs in 2023, with over 224,503 workers in the U.S losing their jobs. However, experts maintain that job security

Foreign Relations

US-China Trade War: Who’s Winning?

The August 2023 visit of Gina Raimondo, the U.S. Secretary of Commerce, to China demonstrated the progress being made in dialogue between the two nations.

Pandemic Recovery

Conquering Pandemic Supply Chain Struggles

The worldwide coronavirus pandemic has underscored supply chain challenges that resulted in billions of dollars in losses for automakers in 2021. Consequently, several firms are

Game Changer

How ChatGPT is Changing the Game

The AI-powered tool ChatGPT has taken the computing world by storm, receiving high praise from experts like Brex design lead, Pietro Schirano. Developed by OpenAI,

Apple Tech

Apple’s Search Engine Disruptor Brewing?

As the fourth quarter of 2023 kicks off, the technology sphere is abuzz with assorted news and advancements. Global stocks exhibit mixed results, whereas cryptocurrency tokens have seen a substantial

GlobalFoundries Titan

GlobalFoundries: Semiconductor Industry Titan

GlobalFoundries, a company that might not be a household name but has managed to make enormous strides in its relatively short 14-year history. As the third-largest semiconductor foundry in the

Revolutionary Job Market

AI is Reshaping the Tech Job Market

The tech industry is facing significant layoffs in 2023, with over 224,503 workers in the U.S losing their jobs. However, experts maintain that job security in the sector remains strong.

Foreign Relations

US-China Trade War: Who’s Winning?

The August 2023 visit of Gina Raimondo, the U.S. Secretary of Commerce, to China demonstrated the progress being made in dialogue between the two nations. However, the United States’ stance

Pandemic Recovery

Conquering Pandemic Supply Chain Struggles

The worldwide coronavirus pandemic has underscored supply chain challenges that resulted in billions of dollars in losses for automakers in 2021. Consequently, several firms are now contemplating constructing domestic manufacturing

Game Changer

How ChatGPT is Changing the Game

The AI-powered tool ChatGPT has taken the computing world by storm, receiving high praise from experts like Brex design lead, Pietro Schirano. Developed by OpenAI, ChatGPT is known for its

Future of Cybersecurity

Cybersecurity Battles: Lapsus$ Era Unfolds

In 2023, the cybersecurity field faces significant challenges due to the continuous transformation of threats and the increasing abilities of hackers. A prime example of this is the group of

Apple's AI Future

Inside Apple’s AI Expansion Plans

Rather than following the widespread pattern of job cuts in the tech sector, Apple’s CEO Tim Cook disclosed plans to increase the company’s UK workforce. The main area of focus

AI Finance

AI Stocks to Watch

As investor interest in artificial intelligence (AI) grows, many companies are highlighting their AI product plans. However, discovering AI stocks that already generate revenue from generative AI, such as OpenAI,

Web App Security

Web Application Supply Chain Security

Today’s web applications depend on a wide array of third-party components and open-source tools to function effectively. This reliance on external resources poses significant security risks, as malicious actors can

Thrilling Battle

Thrilling Battle: Germany Versus Huawei

The German interior ministry has put forward suggestions that would oblige telecommunications operators to decrease their reliance on equipment manufactured by Chinese firms Huawei and ZTE. This development comes after

iPhone 15 Unveiling

The iPhone 15’s Secrets and Surprises

As we dive into the most frequently asked questions and intriguing features, let us reiterate that the iPhone 15 brings substantial advancements in technology and design compared to its predecessors.

Chip Overcoming

iPhone 15 Pro Max: Overcoming Chip Setbacks

Apple recently faced a significant challenge in the development of a key component for its latest iPhone series, the iPhone 15 Pro Max, which was unveiled just a week ago.

Performance Camera

iPhone 15: Performance, Camera, Battery

Apple’s highly anticipated iPhone 15 has finally hit the market, sending ripples of excitement across the tech industry. For those considering upgrading to this new model, three essential features come

Battery Breakthrough

Electric Vehicle Battery Breakthrough

The prices of lithium-ion batteries have seen a considerable reduction, with the cost per kilowatt-hour dipping under $100 for the first occasion in two years, as reported by energy analytics

Economy Act Soars

Virginia’s Clean Economy Act Soars Ahead

Virginia has made significant strides towards achieving its short-term carbon-free objectives as outlined in the Clean Economy Act of 2020. Currently, about 44,000 megawatts (MW) of wind, solar, and energy

Renewable Storage Innovation

Innovative Energy Storage Solutions

The Department of Energy recently revealed a significant investment of $325 million in advanced battery technologies to store excess renewable energy produced by solar and wind sources. This funding will

Renesas Tech Revolution

Revolutionizing India’s Tech Sector with Renesas

Tushar Sharma, a semiconductor engineer at Renesas Electronics, met with Indian Prime Minister Narendra Modi to discuss the company’s support for India’s “Make in India” initiative. This initiative focuses on

Development Project

Thrilling East Windsor Mixed-Use Development

Real estate developer James Cormier, in collaboration with a partnership, has purchased 137 acres of land in Connecticut for $1.15 million with the intention of constructing residential and commercial buildings.

USA Companies

Top Software Development Companies in USA

Navigating the tech landscape to find the right partner is crucial yet challenging. This article offers a comparative glimpse into the top software development companies in the USA. Through a

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its