简体   繁体   English

避免全表扫描

[英]Avoid full table scan

I have an SQL select query to be tuned. 我有一个需要调整的SQL选择查询。 In the query there is a View in from clause which has been formed through 4 tables. 在查询中,有一个View in from子句,该子句是通过4个表形成的。 When this query is executed Full table scan takes place on all these 4 tables which causes CPU spikes. 执行此查询时,将对所有这4个表进行全表扫描,这会导致CPU峰值。 The four tables have valid indexes built on them. 这四个表具有有效的索引。

The query looks similar to this: 该查询类似于以下内容:

SELECT DISTINCT ID, TITLE,...... 
FROM FINDSCHEDULEDTESTCASE 
WHERE STEP_PASS_INDEX = 1  AND LOWER(COMPAREANAME) ='abc'   ORDER BY ID;

The dots indicate that there are many more columns. 点表示还有更多列。 Here FINDSCHEDULEDTESTCASE is a view on four tables. 这里FINDSCHEDULEDTESTCASE是四个表的视图。

Can someone guide me how to avoid full table scan on those four tables. 有人可以指导我如何避免对这四个表进行全表扫描。

In any case using your condition 无论如何使用您的条件

AND LOWER(COMPAREANAME) ='abc'

you'll have the full scan of COMPAREANAME values because for each value function LOWER must be calculated. 您将对COMPAREANAME值进行全面扫描,因为必须为每个值函数计算LOWER。

It depends on so many things! 这取决于很多事情!

SELECT DISTINCTG ID, TITLE, ......

Depending on how many columns you SELECT, it is possible that SQL Server decides to do a table scan instead of using your indexes. 根据您选择的列数,SQL Server可能决定执行表扫描而不使用索引。

Also, depending on your "WHERE" conditions, SQL Server can also decides to do a table scan instead of using your indexes. 另外,根据您的“ WHERE”条件,SQL Server还可以决定执行表扫描而不是使用索引。

Which version of SQL Server are you using? 您正在使用哪个版本的SQL Server?

There can be ways to improve the indexes on the tables, if, for an example, the conditions in the "WHERE" represents less than 50% of the rows, and if you are using SQL 2008. (With filtered indexes http://msdn.microsoft.com/en-us/library/ms188783.aspx ) 可以有办法改善表上的索引,如果,例如,在“WHERE”代表行的不到50%,如果你使用的是SQL 2008(带过滤索引条件的http:// msdn.microsoft.com/zh-CN/library/ms188783.aspx

Or you can create indexes on views ( http://msdn.microsoft.com/en-us/library/ms191432.aspx ) 或者,您可以在视图上创建索引( http://msdn.microsoft.com/zh-cn/library/ms191432.aspx

There really is not enough details in your question to be able to really help you. 您的问题中确实没有足够的细节能够真正为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM