How does SQL server work out the estimated number of rows?

后端 未结 4 1945
有刺的猬
有刺的猬 2020-12-20 23:19

I\'m trying to debug a fairly complex stored procedure that joins across many tabls (10-11). I\'m seeing that for a part of the tree the estimated number of rows drasticly d

4条回答
  •  既然无缘
    2020-12-20 23:52

    It uses statistics, which it keeps for each index.

    (You can also create statistics on non-indexed columns)

    To update all your statistics on every table in a Database (WARNING: will take some time on very large databases. Don't do this on Production servers without checking with your DBA...):

    exec sp_msforeachtable 'UPDATE STATISTICS ?'
    

    If you don't have a regular scheduled job to rebuild your most active indexes (i.e. lots of INSERTS or DELETES), you should consider rebuilding your indexes (same caveat as above applies):

    exec sp_msforeachtable "DBCC DBREINDEX('?')"
    
    • Statistics Used by the Query Optimizer in Microsoft SQL Server 2008

提交回复
热议问题