11.5 sql performance tuning

22
Database Systems, 8 th Edition 1 11.5 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

Upload: noel-nielsen

Post on 31-Dec-2015

311 views

Category:

Documents


34 download

DESCRIPTION

11.5 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 1

11.5 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

Page 2: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 2

Index Selectivity

• Indexes are used when:– Indexed column appears by itself in search

criteria of WHERE or HAVING clause

– Indexed column appears by itself in GROUP BY or ORDER BY clause

– MAX or MIN function is applied to indexed column

– Data sparsity on indexed column is high

• Index selectivity is a measure of how likely an index will be used in query processing

Page 3: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 3

Index Selectivity (continued)

• General guidelines for indexes:– Create indexes for each attribute in WHERE,

HAVING, ORDER BY, or GROUP BY clause

– Do not use in small tables or tables with low sparsity

– Declare primary and foreign keys so optimizer can use indexes in join operations

– Declare indexes in join columns other than PK/FK

Page 4: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 4

Conditional Expressions

• Normally expressed within WHERE or HAVING clauses of SQL statement

• Restricts output of query to only rows matching conditional criteria

Page 5: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 5

• Common practices for efficient SQL:– Use simple columns or literals in conditionals

• Avoid functions

– Numeric field comparisons are faster• than character, date, and NULL comparisons

– Equality comparisons faster than inequality• —the slowest is “LIKE” comparison

– Transform conditional expressions to use literals

– Write equality conditions first

– AND: Use condition most likely to be false first

– OR: Use condition most likely to be true first

– Avoid NOT

Page 6: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 6

11.6 Query Formulation

• Identify what columns and computations are required (p.459)– Expressions – Aggregate functions– Granularity of raw required

• Identify source tables• Determine how to join tables• Determine what selection criteria is needed

– Simple comparison? IN? Nested Comparison? HAVING

• Determine in what order to display output

Page 7: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 7

11.7 DBMS Performance Tuning

• Includes managing DBMS processes in primary memory and structures in physical storage

• DBMS performance tuning at server end focuses on setting parameters used for:– Data cache: large enough– SQL cache: same query may be submitted by many

users– Sort cache– Optimizer mode

Page 8: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 8

DBMS Performance Tuning

• 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• At least with the following table spaces: system table,

user table, index table, temporary table, rollback segment table

– Put high-usage tables in their own table spaces– Assign separate data files in separate storage

volumes for indexes, system, high-usage tables

Page 9: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 9

DBMS Performance Tuning

• Some general recommendations for creation of databases: (continued)– Take advantage of table storage organizations in

database• An indexed organized table stores the end user

table and the index table in consecutive locations on permanent storage

– Partition tables based on usage

– Use denormalized tables where appropriate

– Store computed and aggregate attributes in tables

Page 10: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 10

Common RAID Configurations

Page 11: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 11

11.8 Query Optimization Example

• Example illustrates how query optimizer works• Based on QOVENDOR and QOPRODUCT

tables• Uses Oracle SQL*Plus (Skip)

Page 12: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 12請參考以下 SQL Server 的講義

Page 13: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 13

Page 14: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 14

Check the differences in query plan:

1.Before UPDATE STATISTICS QOVENDOR

2.After UPDATE STATISTICS QOVENDOR

3. CREATE INDEX QOV_NDX1 on QOVENDOR (V_AREACODE)

UPDATE STATISTICS QOVENDOR

4. CREATE INDEX QOV_NDX2 on QOVENDOR (V_NAME)

UPDATE STATISTICS QOVENDOR

Page 15: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 15

Page 16: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 16

Page 17: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 17

Page 18: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 18

Page 19: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 19

Check the differences in query plan:

1.Before UPDATE STATISTICS QOPRODUCT

2.After UPDATE STATISTICS QOPRODUCT

3. CREATE INDEX QOP_NDX2 ON QOPRODUCT(P_PRICE)

UPDATE STATISTICS QOPRODUCT

Page 20: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 20

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

Page 21: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 21

Summary (continued)

• Database statistics refers to measurements gathered by the DBMS – Describe snapshot of database objects’

characteristics

• DBMS processes queries in three phases: parsing, execution, and fetching

• Indexes are crucial in process that speeds up data access

Page 22: 11.5 SQL Performance Tuning

Database Systems, 8th Edition 22

Summary (continued)

• During query optimization, DBMS chooses:– Indexes to use, how to perform join operations,

table to use first, etc.

• Hints change optimizer mode for current SQL statement

• SQL performance tuning deals with writing queries that make good use of statistics

• Query formulation deals with translating business questions into specific SQL code