You are previewing SQL Performance Tuning.
O'Reilly logo
SQL Performance Tuning

Book Description

SQL Performance Tuning is a handbook of practical solutions for busy database professionals charged with managing an organization's critically important data. Covering today's most popular and widely installed database environments, this book is an indispensable resource for managing and tuning SQL across multiple platforms.

Packed with tips, techniques, and best practices, this hands-on guide covers SQL commands and queries over a wide range of conditions and environments. Every facet of the SQL environment is covered, from concurrency control to optimization—and every issue in between. The book includes an overview of each of the most widely used database management systems (DBMS) and provides tuning tips for common day-to-day situations.

Topics covered include:

  • General and specific tuning

  • General sort considerations and options

  • Joins and subqueries

  • Columns and tables

  • Indexes, including B-trees and bitmaps

  • Constraints and stored procedures

  • ODBC and JDBC

  • Data changes and concurrency control (locking)

  • Clients, servers, and middleware

Whether you are a programmer, consultant, or user, you will find SQL Performance Tuning an indispensable guide to optimizing SQL performance across today's popular database environments.

Throughout SQL Performance Tuning, ideas and techniques are tested on a variety of popular SQL environments, including IBM DB2, Informix, Ingres, InterBase, Microsoft SQL Server, MySQL, Oracle, and Sybase ASE. The book's practical examples are written in ANSI/ISO Standard SQL: 1999.



0201791692B08082002

Table of Contents

  1. Copyright
  2. Preface
    1. You Don't Know that Your DBMS Is Good …
    2. You Know that Even Good Tools Work Better in Skilled Hands …
  3. Acknowledgments
  4. 1. Facilis Descensus Averni
    1. This Subject Is Important
    2. The Big Eight
      1. Installation Parameters
    3. Test Results
    4. Portability
    5. Terminology and Expectations
      1. Conventions
    6. Generalities
  5. 2. Simple Searches
    1. General Tuning
      1. Code for Points
      2. Constant Propagation
      3. Dead Code Elimination
      4. Ensure You Use the Right DBMS
      5. Constant Folding
      6. Case-Insensitive Searches
        1. Sargability
      7. The Bottom Line: General Tuning
    2. Specific Tuning
      1. AND
      2. OR
      3. AND Plus OR
      4. NOT
      5. IN
      6. LIKE
      7. SIMILAR
      8. UNION
      9. EXCEPT
      10. INTERSECT
      11. CASE
      12. The Bottom Line: Specific Tuning
    3. Style Notes
    4. Parting Shots
  6. 3. ORDER BY
    1. General Sort Considerations
      1. Partial duplicates slow sorts
      2. Presorting speeds sorts
      3. It's the defined length that matters
      4. INTEGERs beat SMALLINTs
      5. INTEGERs beat CHARs
      6. Sets beat multisets
      7. Conclusion
      8. The ORDER BY Clause
      9. To Sort or Not to Sort
      10. The Bottom Line: General Sorts
    2. Character Sorts
      1. Character Sort Support
      2. Collations
      3. The Bottom Line: Character Sorts
    3. Other Options
      1. Sort Keys
      2. Encouraging Index Use
      3. Preordering
      4. The Bottom Line: Other Options
    4. Parting Shots
  7. 4. GROUP BY
    1. Refresher
    2. Optimal GROUP BY Clauses
      1. Reduce before you expand
      2. GROUP on the same table
      3. HAVING
      4. Alternatives to GROUP BY
      5. The Bottom Line: Optimal GROUP BY Clauses
    3. Sorting
      1. Indexes
        1. GROUP BY alone
        2. MIN/MAX functions
        3. COUNT functions
        4. SUM/AVG functions
      2. The Bottom Line: Sorting
    4. Set Functions and Summary Aggregates
      1. COUNT
      2. SUM Trouble
      3. Multiple Aggregation Levels
      4. Expressions
      5. The Bottom Line: Aggregates
    5. Parting Shots
  8. 5. Joins
    1. Join Plan Strategies
      1. Nested-Loop Joins
      2. The Bottom Line: Nested-Loop Join Plans
      3. Sort-Merge Joins
        1. The Bottom Line: Sort-Merge Join Plans
      4. Hash Joins
        1. The Bottom Line: Hash Join Plans
    2. Avoid the Join Strategies
      1. Join Indexes
      2. Composite Tables
      3. The Bottom Line: Avoiding Joins
    3. Three-Way Joins and Beyond
    4. Old Style versus ANSI Style
      1. Outer Joins
    5. Parting Shots
  9. 6. Subqueries
    1. Refresher
    2. Join versus Subquery
      1. Pro join arguments
      2. Pro subquery arguments
      3. Flattening
        1. To flatten IN
        2. To flatten NOT IN
      4. The Bottom Line: Join versus Subquery
    3. Syntax Choices
      1. IN
      2. DISTINCT
      3. EXISTS
      4. IN or EXISTS?
      5. Double INs
      6. TOP
      7. > ALL
      8. Set Operations
      9. The Bottom Line: Syntax Choices
    4. Parting Shots
  10. 7. Columns
    1. How Big Is the Size Factor?
      1. Fixed or Variable?
      2. The Bottom Line: The Size Factor
    2. Characters
      1. One: Changes to maximum size
      2. Two: Trailing spaces
      3. Three: Concatenation
      4. Four: Data type conversion
      5. Five: Truncation
      6. Length Specification
      7. Variant Character Sets
      8. The Bottom Line: Characters
    3. Temporals
      1. The Bottom Line: Temporals
    4. Numbers
      1. Integers
      2. Floats
      3. Decimals
      4. Serials
      5. The Bottom Line: Numbers
    5. Bits
      1. The Bottom Line: Bits
    6. Large Objects
      1. The Bottom Line: LOBs
    7. NULLs
      1. The Bottom Line: NULLs
    8. Column Order Within Rows
      1. The Bottom Line: Column Order
    9. Parting Shots
  11. 8. Tables
    1. The Storage Hierarchy
      1. Pages
        1. LOB Pages
      2. Extents
        1. Read groups
      3. Files
      4. Partitions
      5. Tablespaces
      6. The Bottom Line: Storage Hierarchy
    2. Heaps
      1. ROWID
      2. Migration
      3. Fragmentation
      4. Free Page Space
      5. The Bottom Line: Heaps
    3. Clusters
      1. The Bottom Line: Clusters
    4. The Normal Forms
      1. Breaking Normalization Rules
        1. Scenario #1: BREAK 1NF
        2. Scenario #2: BREAK 2NF
      2. The Bottom Line: Normalization
    5. Views
      1. Proposition one
      2. Proposition two
      3. Proposition three
      4. The Bottom Line: Views
    6. Parting Shots
  12. 9. Indexes
    1. Refresher
    2. B-trees
      1. Searching a B-tree
      2. Inserting into a B-tree
      3. Deleting from a B-tree
      4. Fragmentation
      5. Rebuilding a B-tree
        1. ALTER INDEX/REBUILD
        2. DROP INDEX/RECREATE
      6. The Bottom Line: B-trees
    3. Types of Indexes
      1. Compound Indexes
      2. Covering Indexes
      3. Unique Indexes
      4. Clustered Indexes
        1. Strong-clustered indexes
        2. Choice of clustered key
        3. Secondary indexes to a strong-clustered index
      5. The Bottom Line: Types of Indexes
    4. Bitmap Indexes
      1. The Bottom Line: Bitmap Indexes
    5. Other Index Variations
    6. Index Key Values
      1. The Bottom Line: Index Key Values
    7. Parting Shots
  13. 10. Constraints
    1. NOT NULL
      1. NULL Indicators
      2. The Bottom Line: NOT NULL Constraints
    2. CHECK
      1. The Bottom Line: CHECK Constraints
    3. FOREIGN KEY
      1. The Bottom Line: FOREIGN KEY Constraints
    4. PRIMARY KEY
      1. The Bottom Line: PRIMARY KEY Constraints
    5. UNIQUE
      1. The Bottom Line: UNIQUE Constraints
    6. Triggers
      1. The Bottom Line: Triggers
    7. Disabling Constraints
      1. Defer
      2. Disable
      3. Drop
      4. The Bottom Line: Disabling Constraints
    8. Client Validations
      1. The Bottom Line: Client Validations
    9. Redundant SELECT Clauses
      1. The Bottom Line: Redundant SELECTs
    10. Parting Shots
  14. 11. Stored Procedures
    1. Refresher
      1. Determinism
    2. Advantages of Stored Procedures
      1. Less Traffic
      2. Semiprecompilation
      3. Parameters
      4. Other Tips
      5. The Bottom Line: Stored Procedures
    3. Parting Shots
  15. 12. ODBC
    1. Refresher
      1. Tracing MS Query
    2. SQLPrepare
      1. The Bottom Line: SQLPrepare
    3. Fetch Loops
      1. The Bottom Line: Fetch Loops
    4. Data-Change Statements
      1. The Bottom Line: Data-Change Statements
    5. Catalog Functions
      1. The Bottom Line: Catalog Functions
    6. Parting Shots
  16. 13. JDBC
    1. Connections
      1. Connection Pooling
      2. Connection Settings
        1. Auto-commit
        2. Isolation level
      3. Connections and DBMS Info
      4. The Bottom Line: Connections
    2. Query Prepping
      1. Query Statement
      2. Query Syntax
      3. getBestRowIdentifier
      4. ResultSetMetaData
      5. Query Settings
        1. Scroll Type
        2. Concurrency type
        3. FetchSize
      6. The Bottom Line: Query Prepping
    3. Result Sets
      1. How Many Rows?
      2. getXXX methods
        1. Impedance
      3. Close
      4. The Bottom Line: Result Sets
    4. Data Changes
      1. The Bottom Line: Data Changes
    5. Parting Shots
  17. 14. Data Changes
    1. Logs
      1. The Bottom Line: Logs
    2. INSERT
      1. Bulk INSERT
      2. The Bottom Line: INSERT
    3. UPDATE
      1. Dependent UPDATE
      2. Batch UPDATE
      3. The Bottom Line: UPDATE
    4. DELETE
      1. The Bottom Line: DELETE
    5. Ugly Updates
      1. The Bottom Line: Ugly Updates
    6. FETCH and Data Changes
      1. The Bottom Line: FETCH and Data Changes
    7. COMMIT and ROLLBACK
      1. The Bottom Line: COMMIT and ROLLBACK
    8. Parting Shots
  18. 15. Locks
    1. What Is a Lock?
      1. Latches
      2. Marks on the wall
      3. RAM records
      4. Lock Modes
      5. Granularity
      6. Escalation
      7. Intent Locks
      8. The Bottom Line: Locks
    2. Isolation Levels
      1. Concurrency Problems and Isolation Levels
        1. Lost Update
        2. Dirty read
        3. Non-repeatable read
        4. Phantom
      2. READ UNCOMMITTED Transactions
      3. READ COMMITTED Transactions
      4. REPEATABLE READ Transactions
      5. SERIALIZABLE Transactions
      6. READ ONLY or FOR UPDATE
      7. Deadlocks
      8. The Bottom Line: Isolation Levels
    3. Index Locks
      1. The Bottom Line: Index Locks
    4. Hot Spots
      1. The Bottom Line: Hot Spots
    5. Optimistic Locking
      1. The Bottom Line: Optimistic Locking
    6. The Scan Trick
    7. Parting Shots
  19. 16. Clients and Servers
    1. Middleware
      1. The Bottom Line: Middleware
    2. Server Processes and Threads
      1. System Processes/Threads
      2. Per-Client Processes/Threads
      3. Separateness and Parallelism
      4. The Bottom Line: Server Processes and Threads
    3. What Should the Client Do?
      1. Client Model
      2. The Bottom Line: Client Tips
    4. Parting Shots
  20. 17. Cost-Based Optimizers
    1. Cost
    2. Statistics and Histograms
      1. Analyzing Statistics
    3. EXPLAIN
    4. Hints
    5. Parting Shots
  21. Further Reading
  22. B. Glossary