33.4. Index Tables Carefully

You should create indexes on the tables of your database to optimize your query search time, but do not create any more indexes than are absolutely needed. Indexes have to be updated and possibly reorganized when you INSERT, UPDATE, or DELETE a row in a table.

Too many indexes can result in extra time spent tending indexes that are seldom used. But even worse, the presence of an index can fool the optimizer into using it when it should not. For example, let’s look at the following simple query:

SELECT *
  FROM Warehouse
 WHERE quantity = 500
   AND color = 'Purply Green';

With an index on color, but not on quantity, most optimizers will first search for rows with color = 'Purply Green' via the index, then apply the quantity ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.