Sql-server – Estimated number of rows is inaccurate

cardinality-estimatessql serverstatistics

I have a table with a field that has a very large variance on a particular field that is often searched. The number of records associated with any value in the field highly differs.

My estimated number of rows are always off, before refreshing statistics it's highly overestimated after refreshing it's highly underestimated. What can I do to improve it?

I update the statistics with full scan. The code is usually called from within a procedure but now I tested it outside.

Best Answer

You could create additional statistics for the data. Include a WHERE clause so each new statistics object references only a subset of the table. By bucketing the rows appropriately (big, medium, small?) the filtered statistics will be less bad than the whole-table ones, leading to better plans.

The predicate must match that used in queries. Depending on your settings there may be more system activity for stats maintenance. Of course this assumes the table contains (or could change to contain) some combination of columns on which rows can be meaningfully aggregated.