Chapter 11
Database Performance Tuning and Query Optimization
Discussion Focus
This chapter focuses on the factors that directly affect database performance. Because performance-
tuning techniques can be DBMS-specific, the material in this chapter may not be applicable under all
circumstances, nor will it necessarily pertain to all DBMS types.
This chapter is designed to build a foundation for the general understanding of database performance-
tuning issues and to help you choose appropriate performance-tuning strategies. (For the most current
information about tuning your database, consult the vendor’s documentation.)
Start by covering the basic database performance-tuning concepts. Encourage students to use the
web to search for information about the internal architecture (internal process and database
storage formats) of various database systems. Focus on the similarities to lay a common
foundation.
Explain how a DBMS processes SQL queries in general terms and stress the importance of
indexes in query processing. Emphasize the generation of database statistics for optimum query
processing.
Step through the query processing example in section 11-4, Optimizer Choices.
Discuss the common practices used to write more efficient SQL code. Emphasize that some
practices are DBMS-specific. As technology advances, the query optimization logic becomes
increasingly sophisticated and effective. Therefore, some of the SQL practices illustrated in this
chapter may not improve query performance as dramatically as it does in older systems.
Finally, illustrate the chapter material using the query optimization example in section 11-8.
401
, Chapter 11
Database Performance Tuning and Query Optimization
Answers to Review Questions
1. What is SQL performance tuning?
SQL performance tuning describes a process – on the client side – that will generate an SQL query
to return the correct answer in the least amount of time, using the minimum amount of resources at
the server end.
2. What is database performance tuning?
DBMS performance tuning describes a process – on the server side – that will properly configure the
DBMS environment to respond to clients’ requests in the fastest way possible, while making
optimum use of existing resources.
3. What is the focus of most performance tuning activities, and why does that focus exist?
Most performance-tuning activities focus on minimizing the number of I/O operations, because the
I/O operations are much slower than reading data from the data cache.
4. What are database statistics, and why are they important?
The term database statistics refers to a number of measurements gathered by the DBMS to describe
a snapshot of the database objects’ characteristics. The DBMS gathers statistics about objects such
as tables, indexes, and available resources -- such as number of processors used, processor speed,
temporary space available, and so on. Such statistics are used to make critical decisions about
improving the query processing efficiency.
5. How are database statistics obtained?
Database statistics can be gathered manually by the DBA or automatically by the DBMS. For
example, many DBMS vendors support the SQL’s ANALYZE command to gather statistics. In
addition, many vendors have their own routines to gather statistics. For example, IBM’s DB2 uses
the RUNSTATS procedure, while Microsoft’s SQL Server uses the UPDATE STATISTICS
procedure and provides the Auto-Update and Auto-Create Statistics options in its initialization
parameters.
6. What database statistics measurements are typical of tables, indexes, and resources?
For tables, typical measurements include the number of rows, the number of disk blocks used, row
length, the number of columns in each row, the number of distinct values in each column, the
maximum value in each column, the minimum value in each column, what columns have indexes,
and so on.
402
, Chapter 11
Database Performance Tuning and Query Optimization
For indexes, typical measurements include the number and name of columns in the index key, the
number of key values in the index, the number of distinct key values in the index key, histogram of
key values in an index, etc.
For resources, typical measurements include the logical and physical disk block size, the location
and size of data files, the number of extends per data file, and so on.
7. How is the processing of SQL DDL statements (such as CREATE TABLE) different from the
processing required by DML statements?
A DDL statement actually updates the data dictionary tables or system catalog, while a DML
statement (SELECT, INSERT, UPDATE and DELETE) mostly manipulates end user data.
8. In simple terms, the DBMS processes queries in three phases. What are those phases, and what
is accomplished in each phase?
The three phases are:
1. Parsing. The DBMS parses the SQL query and chooses the most efficient access/execution
plan.
2. Execution. The DBMS executes the SQL query using the chosen execution plan.
3. Fetching. The DBMS fetches the data and sends the result set back to the client.
Parsing involves breaking the query into smaller units and transforming the original SQL query into
a slightly different version of the original SQL code -- but one that is “fully equivalent” and more
efficient. Fully equivalent means that the optimized query results are always the same as the original
query. More efficient means that the optimized query will, almost always, execute faster than the
original query. (Note that we say almost always because many factors affect the performance of a
database. These factors include the network, the client’s computer resources, and even other queries
running concurrently in the same database.)
After the parsing and execution phases are completed, all rows that match the specified condition(s)
have been retrieved, sorted, grouped, and/or – if required – aggregated. During the fetching phase,
the rows of the resulting query result set are returned to the client. During this phase, the DBMS may
use temporary table space to store temporary data.
9. If indexes are so important, why not index every column in every table? (Include a brief
discussion of the role played by data sparsity.)
Indexing every column in every table will tax the DBMS too much in terms of index-maintenance
processing, especially if the table has many attributes, many rows, and/or requires many inserts,
updates, and/or deletes.
403
Database Performance Tuning and Query Optimization
Discussion Focus
This chapter focuses on the factors that directly affect database performance. Because performance-
tuning techniques can be DBMS-specific, the material in this chapter may not be applicable under all
circumstances, nor will it necessarily pertain to all DBMS types.
This chapter is designed to build a foundation for the general understanding of database performance-
tuning issues and to help you choose appropriate performance-tuning strategies. (For the most current
information about tuning your database, consult the vendor’s documentation.)
Start by covering the basic database performance-tuning concepts. Encourage students to use the
web to search for information about the internal architecture (internal process and database
storage formats) of various database systems. Focus on the similarities to lay a common
foundation.
Explain how a DBMS processes SQL queries in general terms and stress the importance of
indexes in query processing. Emphasize the generation of database statistics for optimum query
processing.
Step through the query processing example in section 11-4, Optimizer Choices.
Discuss the common practices used to write more efficient SQL code. Emphasize that some
practices are DBMS-specific. As technology advances, the query optimization logic becomes
increasingly sophisticated and effective. Therefore, some of the SQL practices illustrated in this
chapter may not improve query performance as dramatically as it does in older systems.
Finally, illustrate the chapter material using the query optimization example in section 11-8.
401
, Chapter 11
Database Performance Tuning and Query Optimization
Answers to Review Questions
1. What is SQL performance tuning?
SQL performance tuning describes a process – on the client side – that will generate an SQL query
to return the correct answer in the least amount of time, using the minimum amount of resources at
the server end.
2. What is database performance tuning?
DBMS performance tuning describes a process – on the server side – that will properly configure the
DBMS environment to respond to clients’ requests in the fastest way possible, while making
optimum use of existing resources.
3. What is the focus of most performance tuning activities, and why does that focus exist?
Most performance-tuning activities focus on minimizing the number of I/O operations, because the
I/O operations are much slower than reading data from the data cache.
4. What are database statistics, and why are they important?
The term database statistics refers to a number of measurements gathered by the DBMS to describe
a snapshot of the database objects’ characteristics. The DBMS gathers statistics about objects such
as tables, indexes, and available resources -- such as number of processors used, processor speed,
temporary space available, and so on. Such statistics are used to make critical decisions about
improving the query processing efficiency.
5. How are database statistics obtained?
Database statistics can be gathered manually by the DBA or automatically by the DBMS. For
example, many DBMS vendors support the SQL’s ANALYZE command to gather statistics. In
addition, many vendors have their own routines to gather statistics. For example, IBM’s DB2 uses
the RUNSTATS procedure, while Microsoft’s SQL Server uses the UPDATE STATISTICS
procedure and provides the Auto-Update and Auto-Create Statistics options in its initialization
parameters.
6. What database statistics measurements are typical of tables, indexes, and resources?
For tables, typical measurements include the number of rows, the number of disk blocks used, row
length, the number of columns in each row, the number of distinct values in each column, the
maximum value in each column, the minimum value in each column, what columns have indexes,
and so on.
402
, Chapter 11
Database Performance Tuning and Query Optimization
For indexes, typical measurements include the number and name of columns in the index key, the
number of key values in the index, the number of distinct key values in the index key, histogram of
key values in an index, etc.
For resources, typical measurements include the logical and physical disk block size, the location
and size of data files, the number of extends per data file, and so on.
7. How is the processing of SQL DDL statements (such as CREATE TABLE) different from the
processing required by DML statements?
A DDL statement actually updates the data dictionary tables or system catalog, while a DML
statement (SELECT, INSERT, UPDATE and DELETE) mostly manipulates end user data.
8. In simple terms, the DBMS processes queries in three phases. What are those phases, and what
is accomplished in each phase?
The three phases are:
1. Parsing. The DBMS parses the SQL query and chooses the most efficient access/execution
plan.
2. Execution. The DBMS executes the SQL query using the chosen execution plan.
3. Fetching. The DBMS fetches the data and sends the result set back to the client.
Parsing involves breaking the query into smaller units and transforming the original SQL query into
a slightly different version of the original SQL code -- but one that is “fully equivalent” and more
efficient. Fully equivalent means that the optimized query results are always the same as the original
query. More efficient means that the optimized query will, almost always, execute faster than the
original query. (Note that we say almost always because many factors affect the performance of a
database. These factors include the network, the client’s computer resources, and even other queries
running concurrently in the same database.)
After the parsing and execution phases are completed, all rows that match the specified condition(s)
have been retrieved, sorted, grouped, and/or – if required – aggregated. During the fetching phase,
the rows of the resulting query result set are returned to the client. During this phase, the DBMS may
use temporary table space to store temporary data.
9. If indexes are so important, why not index every column in every table? (Include a brief
discussion of the role played by data sparsity.)
Indexing every column in every table will tax the DBMS too much in terms of index-maintenance
processing, especially if the table has many attributes, many rows, and/or requires many inserts,
updates, and/or deletes.
403