GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Documentation
  • Privacy Statement
  • License Agreement
  • Contact Us

This view

  • Original page
  • New address

GeezerVison is showing docs.voltactivedata.com — 3.3. Reading the Execution Plan and Optimizing Your SQL Statements

  • Volt Active Data
  • Developer Hub
  • Books
    • VoltDB Tutorial
    • Using VoltDB
    • VoltDB Guide to Performance and Customization
    • VoltDB Administrator's Guide
    • Volt Kubernetes Administrator's Guide
    • VoltDB Quick Reference
  • VoltDB V16
    • VoltDB V16
    • VoltDB V15.3 LTS
    • VoltDB V14.3 LTS
    • VoltDB V13.3 LTS
    • VoltDB V12.3 LTS
    • VoltDB V11.4
    • VoltDB V10.2
    • VoltDB V9.3
3.3. Reading the Execution Plan and Optimizing Your SQL Statements

Guide to Performance and Customization

  • Preface▶
    • 1. Organization of this Manual
    • 2. Other Resources
  • 1. Introduction▶
    • 1.1. What Affects Performance?
    • 1.2. How to Use This Book
  • 2. Hello, World! Revisited▶
    • 2.1. Optimizing your Application for VoltDB
    • 2.2. Applying Hello World to a Practical Problem
    • 2.3. Partitioned vs. Replicated Tables
      • 2.3.1. Defining the Partitioning Column
      • 2.3.2. Creating the Stored Procedures
    • 2.4. Using Asynchronous Stored Procedure Calls
      • 2.4.1. Understanding Asynchronous Programming
      • 2.4.2. The Callback Procedure
      • 2.4.3. Making an Asynchronous Procedure Call
    • 2.5. Connecting to all Servers
    • 2.6. Putting it All Together
    • 2.7. Next Steps
  • 3. Understanding VoltDB Execution Plans▶
    • 3.1. How VoltDB Selects Execution Plans for Individual SQL Statements
    • 3.2. Understanding VoltDB Execution Plans
    • 3.3. Reading the Execution Plan and Optimizing Your SQL Statements
      • 3.3.1. Evaluating the Use of Indexes
      • 3.3.2. Evaluating the Table Order for Joins
  • 4. Using Indexes Effectively▶
    • 4.1. Basic Principles for Effective Indexing
    • 4.2. Defining Indexes
    • 4.3. The Goals for Effective Indexing
    • 4.4. How Indexes Work
    • 4.5. Summary
  • 5. Client2, A Modern Java Client Programming Interface (API)▶
    • 5.1. Configuring the Client
    • 5.2. Connecting to the Database
    • 5.3. Calling Procedures
      • 5.3.1. Handling Backpressure
      • 5.3.2. Handling Request Timeouts
      • 5.3.3. Defining Options on a Per-Call Basis
  • 6. Creating Flexible Schemas With JSON▶
    • 6.1. Using JSON Data Structures as VoltDB Content
    • 6.2. Querying JSON Data
    • 6.3. Updating JSON Data
    • 6.4. Indexing JSON Fields
    • 6.5. Summary: Using JSON in VoltDB
  • 7. Creating Geospatial Applications▶
    • 7.1. The Geospatial Datatypes
      • 7.1.1. The GEOGRAPHY_POINT Datatype
      • 7.1.2. The GEOGRAPHY Datatype
      • 7.1.3. Sizing GEOGRAPHY Columns
      • 7.1.4. How Geospatial Values are Interpreted
    • 7.2. Entering Geospatial Data
    • 7.3. Working With Geospatial Data
      • 7.3.1. Working With Locations
      • 7.3.2. Working With Regions
  • 8. Creating Custom Importers, Exporters, and Formatters▶
    • 8.1. Writing a Custom Exporter
      • 8.1.1. The Structure and Workflow of the Export Client
      • 8.1.2. How to Use Custom Properties to Configure the Client
      • 8.1.3. How to Compile and Install the Client
      • 8.1.4. How to Configure the Export Client
    • 8.2. Writing a Custom Importer
      • 8.2.1. Designing and Coding a Custom Importer
      • 8.2.2. Packaging and Installing a Custom Importer
      • 8.2.3. Configuring and Running a Custom Importer
    • 8.3. Writing a Custom Formatter
      • 8.3.1. The Structure of the Custom Formatter
      • 8.3.2. Compiling and Packaging Custom Formatter Bundles
      • 8.3.3. Installing and Invoking Custom Formatters
      • 8.3.4. Using Custom Formatters With the kafkaloader Utility
  • 9. Creating Custom Tasks▶
    • 9.1. Overview of How Custom Tasks Work
    • 9.2. Modifying the Procedure Call and Arguments
      • 9.2.1. Designing a Java Class That Implements ActionGenerator
      • 9.2.2. Compiling and Loading the Class into VoltDB
      • 9.2.3. Declaring the Task
    • 9.3. Modifying the Interval Between Invocations
      • 9.3.1. Designing a Java Class That Implements IntervalGenerator
      • 9.3.2. Compiling and Loading the Class into VoltDB
      • 9.3.3. Declaring the Task
    • 9.4. Modifying Both the Procedure and Interval
      • 9.4.1. Designing a Java Class That Implements ActionScheduler
      • 9.4.2. Compiling and Loading the Class into VoltDB
      • 9.4.3. Declaring the Task
  • 10. Using Compound Procedures▶
    • 10.1. How Compound Procedures Work
    • 10.2. Defining the Compound Procedure
    • 10.3. Compiling and Loading the Compound Procedure
    • 10.4. Declaring the Compound Procedure
    • 10.5. Invoking the Compound Procedure
    • 10.6. Handling Errors
    • 10.7. Managing Compound Procedures and System Resources
  • 11. Understanding Memory Usage▶
    • 11.1. How the Database Uses Memory
    • 11.2. Actions that Impact Memory Usage
    • 11.3. How the Database Manages Memory
    • 11.4. How Memory is Allocated and Deallocated
    • 11.5. Controlling How Memory is Allocated and Used
      • 11.5.1. Managing the Java Heap
      • 11.5.2. Controlling the Compaction Process for Table Data
    • 11.6. Understanding Memory Usage for Specific Applications
  • 12. Managing Time▶
    • 12.1. The Importance of Time
    • 12.2. Using NTP to Manage Time
      • 12.2.1. Basic Configuration
      • 12.2.2. Troubleshooting Issues with Time
      • 12.2.3. Correcting Common Problems with Time
      • 12.2.4. Example NTP Configuration
    • 12.3. Configuring NTP in a Hosted, Virtual, or Cloud Environment
      • 12.3.1. Considerations for Hosted Environments
      • 12.3.2. Considerations for Virtual and Cloud Environments

3.3. Reading the Execution Plan and Optimizing Your SQL Statements

The execution plan is an ordered representation of how VoltDB will execute the statement. Read the plan from bottom up to understand the order in which the plan is executed. So, for example, looking at the InsertACSStmt SQL statement in the Voter application's Initialize stored procedure, we see the following execution plan for inserting an area code into the area_code_state table:

RETURN RESULTS TO STORED PROCEDURE
 LIMIT 1
  RECEIVE FROM ALL PARTITIONS
   SEND PARTITION RESULTS TO COORDINATOR
    INSERT into "AREA_CODE_STATE"
     MATERIALIZE TUPLE from parameters and/or literals

As mentioned before it is easiest to read the plans from the bottom up. So in this instance, how the SQL statement is executed is by:

  • Constructing a record based on input parameters and/or literal values

  • Inserting the record into the table

  • Because this is a multi-partitioned procedure, each partition then sends its results to the coordinator

  • The coordinator then rolls up the results, limits the results (that is, the status of the insert) to one row, and returns that value to the stored procedure

You will notice that the lines of the execution plan are indented to indicate precedence. For example, the construction of the tuple must happen before it is inserted into the table.

Let's look at another example from the Voter sample. The checkContestantStmt in the Vote stored procedure performs a read operation:

RETURN RESULTS TO STORED PROCEDURE
 INDEX SCAN of "CONTESTANTS" using its primary key index
  uniquely match (CONTESTANT_NUMBER = ?0)

You can see from the plan that the scan of the CONTESTANTS table uses the primary key index. It is also a partitioned table and procedure so the results can be sent directly back to the stored procedure.

Of course, planning for a SQL statement accessing one table with only one condition is not very difficult. The execution plan becomes far more interesting when evaluating more complex statements. For example, you can find a more complex execution plan in the GetStateHeatmap stored procedure:

RETURN RESULTS TO STORED PROCEDURE
 ORDER BY (SORT)
  Hash AGGREGATION ops: SUM(V_VOTES_BY_CONTESTANT_NUMBER_STATE.NUM_VOTES)
   RECEIVE FROM ALL PARTITIONS
    SEND PARTITION RESULTS TO COORDINATOR
     SEQUENTIAL SCAN of "V_VOTES_BY_CONTESTANT_NUMBER_STATE"

In this example you see an execution plan for a multi-partition stored procedure. Again, reading from the bottom up, the order of execution is:

  • At each partition, perform a sequential scan of the votes-per-contestant-and-state table.

  • Return the results from each partition to the initiator that is coordinating the multi-partition transaction.

  • Use an aggregate function to sum the votes for all partitions by contestant.

  • Sort the results

  • And finally, return the results to the stored procedure.

3.3.1. Evaluating the Use of Indexes

What makes the execution plans important is that they can help you optimize your database application by pointing out where the data access can be improved, either by modifying indexes or by changing the join order of queries. Let's start by looking at indexes.

VoltDB uses information about the partitioning column to determine what partition needs to execute a single-partitioned stored procedure. However, it does not automatically create an index for accessing records in that column. So, for example, in the Hello World example, if we remove the primary key (DIALECT) on the HELLOWORLD table, the execution plan for the Select statement also changes.

Before:

RETURN RESULTS TO STORED PROCEDURE
 INDEX SCAN of "HELLOWORLD" using its primary key index
  uniquely match (DIALECT = ?0)

After:

RETURN RESULTS TO STORED PROCEDURE
 SEQUENTIAL SCAN of "HELLOWORLD"
  filter by (DIALECT = ?0)

Note that the first operation has changed to a sequential scan of the HELLOWORLD table, rather than a indexed scan. Since the Hello World example only has a few records, it does not take very long to look through five or six records looking for the right one. But imagine doing a sequential scan of an employee table containing tens of thousands of records. It quickly becomes apparent how important having an index can be when looking for individual records in large tables.

There is an incremental cost associated with inserts or updates to tables containing an index. But the improvement on read performance often far exceeds any cost associated with writes. For example, consider the flight application that is used as an example in the Using VoltDB manual. The FLIGHT table is a replicated table with an index on the FLIGHT_ID, which helps for transactions that join the FLIGHT and RESERVATION tables looking for a specific flight.

However, one of the most common transactions associated with the FLIGHT table is customers looking for flights during a specific time period; not by flight ID. In fact, looking up flights by time period is estimated to occur at least twice as often as looking for a specific flight.

The execution plan for the LookupFlight stored procedure using the original schema looks like this:

RETURN RESULTS TO STORED PROCEDURE
 SEQUENTIAL SCAN of "FLIGHT"
  filter by ((((ORIGIN = ?0) AND (DESTINATION = ?1)) 
     AND (DEPARTTIME > ?2)) AND (DEPARTTIME < ?3))

Clearly, looking through a table of 2,000 flights without an index 10,000 times a second will impact performance. So it makes sense to add another index to improve this transaction. For example:

CREATE TABLE Flight (
   FlightID INTEGER UNIQUE NOT NULL,
   DepartTime TIMESTAMP NOT NULL,
   Origin VARCHAR(3) NOT NULL,
   Destination VARCHAR(3) NOT NULL,
   NumberOfSeats INTEGER NOT NULL,
   PRIMARY KEY(FlightID)
);
CREATE INDEX flightTimeIdx ON FLIGHT ( departtime);

After adding the index, the execution plan changes to use the index:

RETURN RESULTS TO STORED PROCEDURE
 INDEX SCAN of "FLIGHT" using "FLIGHTTIMEIDX"
 range-scan covering from (DEPARTTIME > ?2) while (DEPARTTIME < ?3), 
    filter by ((ORIGIN = ?0) AND (DESTINATION = ?1))

Indexes are not required for every database query. For very small tables or infrequent queries, an index could be unnecessary overhead. However, in most cases and especially frequent queries over large datasets, not having an applicable index can severely impact performance.

When tuning your VoltDB database application, one useful step is to review the schema for any unexpected sequential (non-indexed) scans. The Volt Management Center makes this easy because it puts the "Scans" icon in the attributes column for any stored procedures that contain sequential scans.

See the following chapter, Chapter 4, Using Indexes Effectively, for more information on tuning your database through effective use of indexes.

3.3.2. Evaluating the Table Order for Joins

The other information that the execution plans provides, in addition to the use of indexes, is the order in which the tables are joined.

Join order often impacts performance. Normally, when joining two or more tables, you want the database engine to scan the table that produces the smallest number of matching records first. That way, there are fewer comparisons to evaluate when considering the other conditions. However, at compile time, VoltDB does not have any information about the potential sizing of the individual tables and must make its best guess based solely on the table schema, query, and any indexes that are defined.

For example, assume we have a database that correlates employees to departments. There is a DEPARTMENT table and an EMPLOYEE table, with a DEPT_ID column that acts as a foreign key. But departments have managers, who are themselves employees. So there is a MANAGER table that also contains both a DEPT_ID and an EMP_ID column. The relationship of the tables looks like this:

Most transactions look up employees by their employee ID or their department ID. So indexes are created for those columns. However, say we want to look up all the employees that report to a specific manager. Now we need to join the MANAGER table (to get the department ID), the DEPARTMENT table (to get the department name), and the EMPLOYEE table (to get the employees' names). VoltDB does not know, in advance when compiling the schema, that there will be many more employees than departments or managers. As a result, the winning plan might look like the following:

RETURN RESULTS TO STORED PROCEDURE
 ORDER BY (SORT)
  NESTLOOP INDEX JOIN
  inline (INDEX SCAN of "DEPARTMENT" using "DEPTIDX" (unique-scan covering))
   NESTLOOP INDEX JOIN
   inline (INDEX SCAN of "MANAGER" using "MGRIDX" (unique-scan covering))
    RECEIVE FROM ALL PARTITIONS
     SEND PARTITION RESULTS TO COORDINATOR
      SEQUENTIAL SCAN of "EMPLOYEE"

Clearly, performing a sequential scan of the employees (since the department ID has not been identified yet) is not going to provide the best performance. What you really want to do is to join the MANAGER and DEPARTMENT tables first, to identify the department ID before joining the EMPLOYEE table so the last join can take advantage of the appropriate index.

For cases where you are joining multiple tables and know what the optimal join order would be, VoltDB lets you specify the join order as part of the SQL statement definition. Normally, you declare a new SQLstmt class by specifying the SQL query only. However, you can provide a second argument specifying the join order as a comma-separated list of table names and aliases. For example, the declaration of the preceding SQL query, including join order, would look like this:

public final SQLStmt FindEmpByMgr = new SQLStmt(
    "SELECT dept.dept_name, dept.dept_id, emp.emp_id, " +
    "emp.first_name, emp.last_name, manager.emp_id " +
    "FROM MANAGER, DEPARTMENT AS Dept, EMPLOYEE AS Emp " + 
    "WHERE manager.emp_id=? AND manager.dept_id=dept.dept_id " +
    "AND manager.dept_id=emp.dept_id " +
    "ORDER BY emp.last_name, emp.first_name",
    "manager,dept,emp");

Note that where the query defines an alias for a table — as the preceding example does for the DEPARTMENT and EMPLOYEE tables — the join order must use the alias name rather than the original table name. Also, if a query joins six or more tables, you must specify the join order or VoltDB reports an error when it compiles the project.

Having specified the join order, the chosen execution plan changes to reflect the new sequence of operations:

RETURN RESULTS TO STORED PROCEDURE
 ORDER BY (SORT)
  RECEIVE FROM ALL PARTITIONS
   SEND PARTITION RESULTS TO COORDINATOR
    NESTLOOP INDEX JOIN
    inline (INDEX SCAN of "EMPLOYEE" using "EMPDEPTIDX" (unique-scan covering))
     NESTLOOP INDEX JOIN
     inline (INDEX SCAN of "DEPARTMENT" using "DEPTIDX" (unique-scan covering))
      SEQUENTIAL SCAN of "MANAGER"

The new execution plan has at least three advantages over the default plan:

  • It starts with a sequential scan of the MANAGER table, a table with 10-20 times fewer rows than the EMPLOYEE table.

  • Because MANAGER and DEPARTMENT are replicated tables, all of the initial table scanning and filtering can occur locally within each partition, rather than returning the full EMPLOYEE data from each partition to the initiator to do the later joins and sorting.

  • Because the join order retrieves the department ID first, the execution plan can utilize the index on that column to improve the scanning of EMPLOYEE, the largest table.

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.