Relative Content

Tag Archive for sql-servert-sqlquery-optimization

SQL Scan vs Seek when using OR in where criteria

Consider the following two queries from the AdventureWorks database. The where criteria is entirely on a primary key column. If you use OR criteria both tables get a full table scan on them. In the second scenario the criteria is split out over two queries that are unioned. In the second scenario both tables get index seeks. Can someone explain why SQL can’t/won’t optimize the first query to do in index seek? Are there ways to tell SQL to do a seek without rewriting as a UNION? (I’ve tried index hints with no success)