Name

PER-03: Avoid accidental table scans

Synopsis

One of the most common causes of poor application performance is the “accidental” full table scan. An accidental table scan occurs when the nature of the query, or the expectations of the programmer, suggests that the query will be satisfied using an index, but instead a full table scan is performed.

Accidental table scans can occur under the following circumstances:

  • The index that you believe supports the query does not exist.

  • You have an index that includes the columns in the query, but you don’t include the foremost, “leading” columns in your query.

  • You suppress an index by enclosing the column concerned with a function or an expression.

  • You specify a nonleading substring as the search condition. For instance, you try to find all employees whose name ends in "STONE" (WHERE name LIKE '%STONE').

Most accidental table scans can be resolved by creating a new index or rewording the SQL so that the index is not suppressed. See Chapters 20 and 21 for more details.

Get MySQL Stored Procedure Programming 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.