QuestionQ88

VerifiedSecure, optimize, and deploy database solutions

You have an Azure SQL database that supports an AI-enabled product catalog API. The database includes a table named Sales.Orders. Sales.Orders contains 20 million rows and has a nonclustered index on a column named CreateDate.

The API supplies a date parameter named @OrderDate.

You have a stored procedure named get_latest_day_orders that filters by executing the following query:

WHERE CAST(CreateDate AS date) = @0rderDate.  

A review of the query execution shows that the query performs an index scan.

You need to change the predicate so that Microsoft SQL Server can use an index seek on CreateDate for a single day.

Which WHERE clause should you use?

Explanation

A half-open range predicate compares CreateDate directly to the day’s start and the following day’s start. This is SARGable for an index on CreateDate and includes every timestamp on the specified day without applying a function to the indexed column.

Community Discussion

No comments yet. Be the first to start the discussion!