Transforming and converting an index column

A slow query despite an index. Transforming the date column with CONVERT rules out an Index Seek and reads the whole table (the clustered index).

SQL

Situation

A settlement screen queries one day of paid orders. Code that compares the date after converting it to a yyyyMMdd string has been in use for a long time. Finding 140 rows for one day reads the whole table.

Tables

TableColumnTypeDescription
ordersstatusvarchar(10)DONE 90%, PAID 7%, CANCELED 3%
ordersord_dtdatetime2(0)Order date and time
IndexColumns
orders_pk (clustered)ord_id
orders_x2status, ord_dt, ord_id

One day of paid orders

BEFORE · Transformed date

SELECT *
FROM orders
WHERE status = 'PAID'
  AND CONVERT(char(8), ord_dt, 112) = '20250915'

Run it and check which operator read the data (Seek or Scan) and how many rows it read.

Approach

1. Check whether it is a Seek or a Scan

An Index Seek reads one range of the index, and a Clustered Index Scan reads the whole table. If the result is 140 rows but the plan shows a Scan, the condition failed to define a search range.

2. Find whether the condition transforms an index column

Wrapping the column in a function, as in CONVERT(char(8), ord_dt, 112), makes the compared value differ from the value stored in the index, so the start point of the Seek cannot be determined. Rather than Seek the status = 'PAID' range and then do 140,000 Key Lookups, the optimizer chooses to read the whole table.

3. Move the transformation to the value side

Leave the column as is and change the value being compared, as in "at or after midnight that day, before midnight the next day".

Explanation

Transformed date → range condition

AFTER · Range condition

SELECT *
FROM orders
WHERE status = 'PAID'
  AND ord_dt >= CAST('2025-09-15' AS datetime2(0))
  AND ord_dt <  CAST('2025-09-16' AS datetime2(0))

Why it got faster

  • Before reads the whole table with a Clustered Index Scan and filters it down to 140 rows. Buffers about 13,200.
  • After reads orders_x2 with an Index Seek only from ('PAID', 2025-09-15) to the end of that day (Buffers 4), and fetches the remaining columns 140 times with a Clustered Index Seek (Key Lookup). Total Buffers about 390.

Why this is optimal

  • The index finds only the result rows. The remaining cost is the Key Lookup that looks up the table again per row because of SELECT * (140 times, about 386 pages). Selecting only the needed columns reduces this too (scenario 15).
  • Write the date literal in the same type as the column (datetime2(0)). If a plain string is used, SQL Server adds operators that convert the value (Merge Interval, Constant Scan) to the plan. The result and the read count are the same, but the plan becomes more complex.

Key points

  • Do not transform the index column; change the value being compared (a SARGable condition).
  • Distinguish the Seek Predicate (search range) from the Predicate (condition filtered after reading) in the execution plan.
  • DB differences: the principle is the same, but in SQL Server the table is a clustered index, so when the index cannot be used it appears as a Clustered Index Scan. The type conversion problem of comparing a character column with a different type is covered separately in scenario 21.