Database Performance Tuning and Query Optimization
Database Systems, 8th Edition 2
Objectives
to make – Some common practices used to write efficient SQL
code – How to formulate queries and tune the DBMS for
optimal performance – Performance tuning in SQL Server 2005
Database Systems, 8th Edition 3
11.1 Database Performance-Tuning Concepts
reduce response time of database system
Database Systems, 8th Edition 4
Database Systems, 8th Edition 5
Performance Tuning: Client and Server
in least amount of time • Using minimum amount of resources at server
– SQL performance tuning
clients’ requests as fast as possible • Optimum use of existing resources
– DBMS performance tuning
Database Systems, 8th Edition 6
DBMS Architecture
– 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
Database Systems, 8th Edition 7
Basic DBMS architecture
Database Systems, 8th Edition 8
DBMS Architecture (continued)
functions
Database Systems, 8th Edition 9
DBMS Architecture (continued)
Database Systems, 8th Edition 10
Database Statistics
Processor speed, Temporary space available • Make critical decisions about improving query
processing efficiency • Can be gathered manually by DBA or automatically by
DBMS – UPDATE STATISTICS table_name [index_name] – Auto-Update and Auto-Create Statistics option
Database Systems, 8th Edition 11
Database Systems, 8th Edition 12
Ch08: dbcc show_statistics (customer, PK__CUSTOMER__24927208 )
Ch08: dbcc show_statistics (customer, CUS_UI1)
補充 SQL Server 2005
Database Systems, 8th Edition 13
11.2 Query Processing
– Execution • DBMS executes the query using chosen
execution plan
– Fetching • DBMS fetches the data and sends the result back
to the client
Database Systems, 8th Edition 14 Query Processing
Database Systems, 8th Edition 15
SQL Parsing Phase
different version of original SQL code – Fully equivalent
– More efficient • Optimized query will almost always execute faster
than original query
Database Systems, 8th Edition 16
SQL Parsing Phase (continued) • Query optimizer analyzes SQL query and finds most
efficient way to access data – Validated for syntax compliance
– Validated against data dictionary • Tables, column names are correct • User has proper access rights
– Analyzed and decomposed into more atomic components
– Optimized through transforming into a fully equivalent but more efficient SQL query
– Prepared for execution by determining the execution or access plan
Database Systems, 8th Edition 17
SQL Parsing Phase (continued)
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
Database Systems, 8th Edition 18
Database Systems, 8th Edition 19
SQL Execution and Fetching Phase
Database Systems, 8th Edition 20
Query Processing Bottlenecks
– RAM
– Hard disk
– Network
– Application code
Database Systems, 8th Edition 21
SQL 敘述 輸入完成 後先不要 執行查 詢 , 請按 下工具列 的顯示估 計執行計
劃鈕 :
Database Systems, 8th Edition 22
11.3 Indexes and Query Optimization
– Facilitate searching, sorting, and using aggregate functions as well as join operations
– Ordered set of values that contains index key and pointers
Database Systems, 8th Edition 23
Indexes and Query Optimization • Data sparsity: number of different values a column
could possibly have • Indexes implemented using: ( 課本 p. 453)
– Hash indexes
– B-tree indexes: most common index type. Used in tables in which column values repeat a small number of times. The leaves contain pointers to records It is self-balanced.
– Bitmap indexes: 0/1
Bitmap indexes
Database Systems, 8th Edition 24B-tree and bitmap index representation
25
Index Representation for the CUSTOMER table
SELECT CUS_NAME FROM CUSTOMER WHERE CUS_STATE=‘FL’ Requires only 5 accesses to STATE_INDEX, 5 accesses to CUSTOMER
Database Systems, 8th Edition 26
11.4 Optimizer Choices
– Rules assign a fixed cost to each operation
being accessed
– Adds up processing cost, I/O costs, resource costs to derive total cost
Example
Database Systems, 8th Edition 27
SELECT P_CODE, P_DESCRIPT, P_PRICE, V_NAME, V_STATE FROM PRODUCT P, VENDOR V WHERE P.V_CODE=V.V_CODE AND V.V_STATE=‘FL’;
Database Systems, 8th Edition 28
Example
Database Systems, 8th Edition 29
SELECT MIN(P_QOH) FROM PRODUCT
could be resolved by reading only the first entry in the PQOH_NDX index
Database Systems, 8th Edition 30
Using Hints to Affect Optimizer Choices
– Statistics may be old – Might choose less efficient decisions
Database Systems, 8th Edition 31
Oracle 版本
Database Systems, 8th Edition 32
MS SQL Server 的語法請參考:
http://msdn.microsoft.com/en-us/library/ms187713.aspx
SQL Server Query Hints Example
select o.customerid,companyname from orders as o inner MERGE join customers as c on o.customerid = c.customerid
select o.customerid,companyname from orders as o inner HASH join customers as c on o.customerid = c.customerid
select o.customerid,companyname from orders as o inner LOOP join customers as c on o.customerid = c.customerid
select city, count(*) from customers group by city OPTION (HASH GROUP)