SQLServer优化器选择不适当的查询计划原因之一
2012-10-23 11:12
204 查看
使用复杂的SQL脚本(包含多个子查询,连接,复杂的查询条件等)做查询时,有时候会碰到SQL性能问题。
使用Set Statistics Profile on打开查询计划查看SQLServer优化器是否选择了适当的连接方式来做join,例如:如果在数据量很大的两个结果集间做连接使用了Nest Loop方式的连接则视为不适当。
如果存在上述问题,查看查询计划中连接操作所连接的两个表的EstimateRows是否和实际的结果集行数差距很大。
如果存在上述问题,使用sp_helpstats N'your_table_name', 'ALL'和DBCC SHOW_STATISTICS('your_table_name', your_statistics_name)命令查看连接操作所使用的表的统计信息是否过期,如果统计中的Rows和实际表的行数不同,则视为统计过期。
如果存在上述问题,可以:
1. 使用UPDATE STATISTICS命令更新单表的统计;
2. 使用EXEC sp_updatestats更新数据库的所有统计;
3. 使用以下脚本更新指定库的所有表:
备注1:
即使数据库的AUTO UPDATE STATISTICS是打开的(可在ssms的数据库属性中找到),SQLServer也不是每次更新数据时都会更新统计,具体算法如下:
SQL Server 2008 determines whether to update statistics based on changes to column modification counters (colmodctrs).
A statistics object is considered out of date in the following cases:
· If the statistics is defined on a regular table, it is out of date if:
o The table size has gone from 0 to >0 rows (test 1).
o The number of rows in the table when the statistics were gathered was 500 or less, and the colmodctr of the leading column of the statistics object has changed by more than 500 since then (test 2).
o The table had more than 500 rows when the statistics were gathered, and the colmodctr of the leading column of the statistics object has changed by more than 500 + 20% of the number of rows in the table when the statistics were gathered (test 3).
· For filtered statistics, the colmodctr is first adjusted by the selectivity of the filter before these conditions are tested. For example, for filtered statistics with predicate selecting 50% of the rows, the colmodctr is multiplied by 0.5.
· One limitation of the automatic update logic is that it tracks changes to columns in the statistics, but not changes to columns in the predicate. If there are many changes to the columns used in predicates of filtered statistics, consider using manual updates to keep up with the changes.
· If the statistics object is defined on a temporary table, it is out of date as discussed above, except that there is an additional threshold for recomputation at 6 rows, with a test otherwise identical to test 2 in the previous list.
参照:http://msdn.microsoft.com/en-us/library/dd535534.aspx
备注2:
rebuild索引不会更新所有的统计,只会更新跟索引相关的统计。
使用Set Statistics Profile on打开查询计划查看SQLServer优化器是否选择了适当的连接方式来做join,例如:如果在数据量很大的两个结果集间做连接使用了Nest Loop方式的连接则视为不适当。
如果存在上述问题,查看查询计划中连接操作所连接的两个表的EstimateRows是否和实际的结果集行数差距很大。
如果存在上述问题,使用sp_helpstats N'your_table_name', 'ALL'和DBCC SHOW_STATISTICS('your_table_name', your_statistics_name)命令查看连接操作所使用的表的统计信息是否过期,如果统计中的Rows和实际表的行数不同,则视为统计过期。
如果存在上述问题,可以:
1. 使用UPDATE STATISTICS命令更新单表的统计;
2. 使用EXEC sp_updatestats更新数据库的所有统计;
3. 使用以下脚本更新指定库的所有表:
SET NOCOUNT ON DECLARE @SQLcommand NVARCHAR(512), @Table SYSNAME DECLARE curAllTables CURSOR FOR SELECT table_schema + '.' + table_name FROM information_schema.tables WHERE TABLE_TYPE = 'BASE TABLE' OPEN curAllTables FETCH NEXT FROM curAllTables INTO @Table WHILE (@@FETCH_STATUS = 0) BEGIN PRINT N'UPDATING STATISTICS FOR TABLE: ' + @Table SET @SQLcommand = 'UPDATE STATISTICS ' + @Table + ' WITH FULLSCAN' EXEC sp_executesql @SQLcommand FETCH NEXT FROM curAllTables INTO @Table END CLOSE curAllTables DEALLOCATE curAllTables SET NOCOUNT OFF GO
备注1:
即使数据库的AUTO UPDATE STATISTICS是打开的(可在ssms的数据库属性中找到),SQLServer也不是每次更新数据时都会更新统计,具体算法如下:
SQL Server 2008 determines whether to update statistics based on changes to column modification counters (colmodctrs).
A statistics object is considered out of date in the following cases:
· If the statistics is defined on a regular table, it is out of date if:
o The table size has gone from 0 to >0 rows (test 1).
o The number of rows in the table when the statistics were gathered was 500 or less, and the colmodctr of the leading column of the statistics object has changed by more than 500 since then (test 2).
o The table had more than 500 rows when the statistics were gathered, and the colmodctr of the leading column of the statistics object has changed by more than 500 + 20% of the number of rows in the table when the statistics were gathered (test 3).
· For filtered statistics, the colmodctr is first adjusted by the selectivity of the filter before these conditions are tested. For example, for filtered statistics with predicate selecting 50% of the rows, the colmodctr is multiplied by 0.5.
· One limitation of the automatic update logic is that it tracks changes to columns in the statistics, but not changes to columns in the predicate. If there are many changes to the columns used in predicates of filtered statistics, consider using manual updates to keep up with the changes.
· If the statistics object is defined on a temporary table, it is out of date as discussed above, except that there is an additional threshold for recomputation at 6 rows, with a test otherwise identical to test 2 in the previous list.
参照:http://msdn.microsoft.com/en-us/library/dd535534.aspx
备注2:
rebuild索引不会更新所有的统计,只会更新跟索引相关的统计。
相关文章推荐
- Oracle EBS-SQL (MRP-6):检查MRP计划运行报错原因之超大数据查询1.sql
- Crontab计划任务未执行原因查询
- Oracle EBS-SQL (MRP-7):检查MRP计划运行报错原因之超大数据查询2.sql
- 怎样成为搜索高手——选择适当的查询词
- SQL Server 查询优化(测试02)参数嗅探-执行计划选择
- JTabel选择表格事件监听时,用鼠标点击一次,会出现两次事件的原因。
- 不选择使用Lucene的6大原因(转载) - Hubble.net 将尽可能解决这些问题
- 一个执行计划异常变更的案例 - 外传之查询执行计划的几种方法
- JavaWeb 将数据库查询结果展现到页面radio,根据页面用户选择查询数据库表结果
- WebRTC getUserMedia()获取流失败的原因查询及解决
- SQL查询出当前服务器时间是否在表T1时间段内,如果有则选择出
- SQL Server查询计划sys.dm_exec_text_query_plan
- Service Unavailable的原因 遇到两次了 记录下来 方便查询
- 看懂SqlServer查询计划
- 看懂SqlServer查询计划(转)
- SQL Server查询速度慢原因及优化方法
- 懒人摘抄(内容转自CSDN资源中教程)-数据查询02-选择查询
- 根据时间排序分页查询导致部分数据丢失原因
- SQL查询慢的48个原因分析
- oracle 当查询结果有重复记录时 选择时间最近的一条