Ben Reyes

Hacker / Entrepreneur

Guidelines on optimising SQL Quires

SQL Workload reduced by

  1. Efficient structuring of the data (indexes, clusters, keys
  2. Efficient structuring of queries
  3. Providing more information to the DBMS query optimiser
  • Avoid the use of *, count(*) (the system needs to work out at run time what needs to be substituted for the *)
  • Avoid very long table names (they take longer to parse)
  • Do use brief table aliases in joins
  • Do specify table.columns in joins (avoids the optimiser having to work out which one is intended).
  • Be Aware of the assumptions the optimiser in your system is making. Indexes etc. will give it more choice, check with and without processing times.


Summary

  • JOIN generally faster than nested subquery
  • Indexed columns generally faster than non-indexed columns for queries with a hit rate < 20%
  • Unique index faster than non-unique
  • != Does not use an index unlike other operators
  • Non-compressed indexes faster than compressed indexes

From revision notes on an Advance Database System and Technology module at Queen Mary, University of London.