Chapter 1 - Database Performance Tuning and Query Optimization
Chapter 1 - Database Performance Tuning and Query Optimization
Artificial Intelligence
DATABASES
Chapter 7 : Database Performance Tuning and
Query Optimization
th 2
Database
16/10/2022
Systems, 8 Edition
Objectives (continued)
• In this chapter, you will learn: (continued)
– About the types of decisions the query optimizer
has to make
– Some common practices used to write efficient
SQL code
– How to formulate queries and tune the DBMS for
optimal performance
th 3
Database
16/10/2022
Systems, 8 Edition
Database Performance-Tuning
Concepts
• Goal of database performance is to execute
queries as fast as possible
• Database performance tuning
– Set of activities and procedures designed to
reduce response time of database system
• All factors must operate at optimum level with
minimal bottlenecks
• Good database performance starts with
good database design
th 4
Database
16/10/2022
Systems, 8 Edition
th 5
Database
16/10/2022
Systems, 8 Edition
Performance Tuning:
Client and Server
• Client side
– Generate SQL query that returns correct answer
in least amount of time
• Using minimum amount of resources at server
– SQL performance tuning
• Server side
– DBMS environment configured to respond to
clients’ requests as fast as possible
• Optimum use of existing resources
– DBMS performance tuning
th 6
Database
16/10/2022
Systems, 8 Edition
DBMS Architecture
• All data in database are stored in data files
• Data files
– Automatically expand in predefined increments
known as extends
– Grouped in file groups or table spaces
• Table space or file group:
– Logical grouping of several data files that store
data with similar characteristics
th 7
Database
16/10/2022
Systems, 8 Edition
th 8
Database
16/10/2022
Systems, 8 Edition
DBMS Architecture (continued)
th 9
Database
16/10/2022
Systems, 8 Edition
DBMS Architecture (continued)
• Input/output request: low-level data access
operation to/from computer devices
• Data cache is faster than data in data files
– DBMS does not wait for hard disk to retrieve data
• Majority of performance-tuning activities focus on
minimizing I/O operations
• Typical DBMS processes:
– Listener, User, Scheduler, Lock manager, Optimizer
th 10
Database
16/10/2022
Systems, 8 Edition
Database Statistics
• Measurements about database objects and
available resources
– Tables
– Indexes
– Number of processors used
– Processor speed
– Temporary space available
th 11
Database
16/10/2022
Systems, 8 Edition
Database Statistics (continued)
• Make critical decisions about improving query
processing efficiency
• Can be gathered manually by DBA or
automatically by DBMS
th 12
Database
16/10/2022
Systems, 8 Edition
th 13
Database
16/10/2022
Systems, 8 Edition
Query Processing
th 14
Database
16/10/2022
Systems, 8 Edition
th 15
Database
16/10/2022
Systems, 8 Edition
SQL Parsing Phase
th 16
Database
16/10/2022
Systems, 8 Edition
SQL Parsing Phase (continued)
th 17
Database
16/10/2022
Systems, 8 Edition
SQL Parsing Phase (continued)
• Access plans are DBMS-specific
– Translate client’s SQL query into series of
complex I/O operations
– Required to read the data from the physical data
files and generate result set
• DBMS checks if access plan already exists for
query in SQL cache
• DBMS reuses the access plan to save time
• If not, optimizer evaluates various plans
– Chosen plan placed in SQL cache
th 18
Database
16/10/2022
Systems, 8 Edition
th 19
Database
16/10/2022
Systems, 8 Edition
SQL Execution Phase
SQL Fetching Phase
• All I/O operations indicated in access plan are
executed
– Locks acquired
– Data retrieved and placed in data cache
– Transaction management commands processed
• Rows of resulting query result set are returned
to client
• DBMS may use temporary table space to store
temporary data
th 20
Database
16/10/2022
Systems, 8 Edition
Query Processing Bottlenecks
th 21
Database
16/10/2022
Systems, 8 Edition
Indexes and Query Optimization
• Indexes
– Crucial in speeding up data access
– Facilitate searching, sorting, and using
aggregate functions as well as join operations
– Ordered set of values that contains index key
and pointers
• More efficient to use index to access table than
to scan all rows in table sequentially
th 22
Database
16/10/2022
Systems, 8 Edition
Indexes and Query Optimization
(continued)
• Data sparsity: number of different values a
column could possibly have
• Indexes implemented using:
– Hash indexes
– B-tree indexes
– Bitmap indexes
• DBMSs determine best type of index to use
th 23
Database
16/10/2022
Systems, 8 Edition
th 24
Database
16/10/2022
Systems, 8 Edition
Optimizer Choices
• Rule-based optimizer
– Preset rules and points
– Rules assign a fixed cost to each operation
• Cost-based optimizer
– Algorithms based on statistics about objects
being accessed
– Adds up processing cost, I/O costs, resource
costs to derive total cost
th 25
Database
16/10/2022
Systems, 8 Edition
th 26
Database
16/10/2022
Systems, 8 Edition
Using Hints to Affect
Optimizer Choices
• Optimizer might not choose best plan
• Makes decisions based on existing statistics
– Statistics may be old
– Might choose less efficient decisions
• Optimizer hints: special instructions for the
optimizer embedded in the SQL command text
th 27
Database
16/10/2022
Systems, 8 Edition
th 28
Database
16/10/2022
Systems, 8 Edition
SQL Performance Tuning
• Evaluated from client perspective
– Most current relational DBMSs perform
automatic query optimization at the server end
– Most SQL performance optimization techniques
are DBMS-specific
• Rarely portable
• Majority of performance problems related to
poorly written SQL code
• Carefully written query usually outperforms a
poorly written query
th 29
Database
16/10/2022
Systems, 8 Edition
Index Selectivity
th 30
Database
16/10/2022
Systems, 8 Edition
Index Selectivity (continued)
th 31
Database
16/10/2022
Systems, 8 Edition
Conditional Expressions
th 32
Database
16/10/2022
Systems, 8 Edition
Conditional Expressions (continued)
th 33
Database
16/10/2022
Systems, 8 Edition
Query Formulation
th 34
Database
16/10/2022
Systems, 8 Edition
DBMS Performance Tuning
th 35
Database
16/10/2022
Systems, 8 Edition
DBMS Performance Tuning
(continued)
• Some general recommendations for creation of
databases:
– Use RAID (Redundant Array of Independent
Disks) to provide balance between performance
and fault tolerance
– Minimize disk contention
– Put high-usage tables in their own table spaces
– Assign separate data files in separate storage
volumes for indexes, system, high-usage tables
th 36
Database
16/10/2022
Systems, 8 Edition
DBMS Performance Tuning
(continued)
• Some general recommendations for creation of
databases: (continued)
– Take advantage of table storage organizations in
database
– Partition tables based on usage
– Use denormalized tables where appropriate
– Store computed and aggregate attributes in
tables
th 37
Database
16/10/2022
Systems, 8 Edition
Query Optimization Example
th 38
Database
16/10/2022
Systems, 8 Edition
th 39
Database
16/10/2022
Systems, 8 Edition
th 40
Database
16/10/2022
Systems, 8 Edition
th 41
Database
16/10/2022
Systems, 8 Edition
th 42
Database
16/10/2022
Systems, 8 Edition
th 43
Database
16/10/2022
Systems, 8 Edition
th 44
Database
16/10/2022
Systems, 8 Edition
th 45
Database
16/10/2022
Systems, 8 Edition
th 46
Database
16/10/2022
Systems, 8 Edition
th 47
Database
16/10/2022
Systems, 8 Edition
Summary
• Database performance tuning
– Refers to activities to ensure query is processed
in minimum amount of time
• SQL performance tuning
– Refers to activities on client side to generate
SQL code
• Returns correct answer in least amount of time
• Uses minimum amount of resources at server end
• DBMS architecture represented by processes
and structures used to manage a database
th 48
Database
16/10/2022
Systems, 8 Edition
Summary (continued)
th 49
Database
16/10/2022
Systems, 8 Edition
Summary (continued)
th 50
Database
16/10/2022
Systems, 8 Edition