Problem Statement
A selective query uses an index sometimes but scans at other times. How do statistics influence this and how would you fix it?
Explanation
The optimizer estimates row counts from column and histogram statistics. If stats are stale or too generic, it may think many rows match and pick a scan. Update statistics, increase histogram buckets for skewed columns, or create filtered stats if supported.
Also check parameter sniffing and ensure literals or sniffed values reflect common cases. Verify that predicates are SARGable.
Code Solution
SolutionRead Only
-- Postgres ANALYZE users; -- SQL Server UPDATE STATISTICS users WITH FULLSCAN;
Practice Sets
This question appears in the following practice sets:
