Improving TPC-H-like queries – Q17

June 16, 2009
Author
kuszmaul
Share this Post:

Executive Summary: A query like TPC-H Query 17 can be sped up by large factors by using straight_joins and clustering indexes. (This entry posted by Dave.)

 

In a previous post, we wrote about queries like TPC-H query 2, and the use of straight_join to improve performance.
This week, we consider Query 17, described by the TPC-H documentation as

“The Small-Quantity-Order Revenue Query considers parts of a given brand and with a given container type and determines the average lineitem quantity of such parts ordered for all orders (past and pending) in the 7-year database. What would the average yearly gross (undiscounted) loss in revenue if orders for these parts with a quantity of this average were no longer taken?”

 

 

Our initial run on Q17 (same hardware as before) timed out after 3600 seconds.

 

The MySQL query is:

 

 

The query plan was:

 

 

This plan does not take advantage of the selectivity of picking a specific brand and container to reduce the search set.
Using STRAIGHT_JOIN (as described in our post on Q2) and ordering the FROM clause to select the part table first sets the plan on a better course.

 

 

However, the query bogs down when searching the lineitem table.
Although an index was available in the LINEITEM table to quickly search the table (lineitem_fk3), the subsequent data lookup (along the primary key) caused a long(!) series of point queries.

 

Unlike many storage engines, TokuDB supports clustering indexes on any index (not just the primary key), and further supports clustering indexes on more than one index per table.

 

 

This results in the following query/plan:

 

 

Although it appears to look at more rows, each table is scanned efficiently down a key that provides all of the information needed to address the query.

 

We measured 101 sec on this query. Maybe some day we’ll be patient enough to let the original plan complete, but we know this approach is at least 36 (3600/101) times faster.

 

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Far
Enough.

Said no pioneer ever.
MySQL, PostgreSQL, InnoDB, MariaDB, MongoDB and Kubernetes are trademarks for their respective owners.
© 2026 Percona All Rights Reserved