by Markus Winand.

The Where Clause


The previous chapter described the structure of indexes and explained the cause of poor index performance. In the next step we learn how to spot and avoid these problems in SQL statements. We start by looking at the where clause.

The where clause defines the search condition of an SQL statement, and it thus falls into the core functional domain of an index: finding data quickly. Although the where clause has a huge impact on performance, it is often phrased carelessly so that the database has to scan a large part of the index. The result: a poorly written where clause is the first ingredient of a slow query.

This chapter explains how different operators affect index usage and how to make sure that an index is usable for as many queries as possible. The last section shows common anti-patterns and presents alternatives that deliver better performance.

Contents

  1. The Equals Operator — Exact key lookup

    1. Primary Keys — Verifying index usage

    2. Concatenated Keys — Multi-column indexes

    3. Slow Indexes, Part II — The first ingredient, revisited

  2. Functions — Using functions in the where clause

    1. Case-Insensitive SearchUPPER and LOWER

    2. User-Defined Functions — Limitations of function-based indexes

    3. Over-Indexing — Avoid redundancy

  3. Bind Variables — For security and performance

  4. Searching for Ranges — Beyond equality

    1. Greater, Less and BETWEEN — The column order revisited

    2. Indexing SQL LIKE FiltersLIKE is not for full-text search

    3. Index Combine — Why not using one index for every column?

  5. Partial Indexes — Indexing selected rows

  6. NULL in the Oracle Database — An important curiosity

    1. NULL in Indexes — Every index is a partial index

    2. NOT NULL Constraints — affect index usage

    3. Emulating Partial Indexes — using function-based indexing

  7. Obfuscated Conditions — Common anti-patterns

    1. Dates — Pay special attention to DATE types

    2. Numeric Strings — Don’t mix types

    3. Combining Columns — use redundant where clauses

    4. Smart Logic — The smartest way to make SQL slow

    5. Math — Databases don’t solve equations

Previous pageNext page

About the Author

Photo of Markus Winand

Markus Winand is the SQL Renaissance Ambassador. He is on a mission to introduce developers to the evolution of SQL in the 21st century. Markus can be hired as trainer, speaker and consultant via winand.at.

Buy his Book on Amazon

Cover of “SQL Performance Explained”: Squirrel running on grass

The essence of SQL tuning in 200 pages

Buy on Amazon
(paperback only)

Paperback and PDF also available at Markus’ store.

Hire Markus

Markus offers SQL training and consulting for developers working at companies of any size.
Learn more »

Do not use offset for pagination

Learn why

Visit my sibling!Modern SQL logoA lot changed since SQL-92!

The Use The Index, Luke! mug

Stickers, coasters, books and coffee mugs. All you need for learning.

Shop now

“Use The Index, Luke!” by Markus Winand is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Legal | Contact | NO WARRANTY | Trademarks | Privacy and GDPR | CC-BY-NC-ND 3.0 license