Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What generic techniques can be applied to optimize SQL queries?

What techniques can be applied effectively to improve the performance of SQL queries? Are there any general rules that apply?

like image 278
Niyaz Avatar asked Sep 02 '08 11:09

Niyaz


2 Answers

  • Use primary keys
  • Avoid select *
  • Be as specific as you can when building your conditional statements
  • De-normalisation can often be more efficient
  • Table variables and temporary tables (where available) will often be better than using a large source table
  • Partitioned views
  • Employ indices and constraints
like image 144
Unsliced Avatar answered Oct 11 '22 00:10

Unsliced


Learn what's really going on under the hood - you should be able to understand the following concepts in detail:

  • Indexes (not just what they are but actually how they work).
  • Clustered indexes vs heap allocated tables.
  • Text and binary lookups and when they can be in-lined.
  • Fill factor.
  • How records are ghosted for update/delete.
  • When page splits happen and why.
  • Statistics, and how they effect various query speeds.
  • The query planner, and how it works for your specific database (for instance on some systems "select *" is slow, on modern MS-Sql DBs the planner can handle it).
like image 24
Keith Avatar answered Oct 10 '22 23:10

Keith