简体   繁体   中英

Avoid full table scan

I have an SQL select query to be tuned. In the query there is a View in from clause which has been formed through 4 tables. When this query is executed Full table scan takes place on all these 4 tables which causes CPU spikes. 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.

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.

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.

Also, depending on your "WHERE" conditions, SQL Server can also decides to do a table scan instead of using your indexes.

Which version of SQL Server are you using?

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 )

Or you can create indexes on views ( http://msdn.microsoft.com/en-us/library/ms191432.aspx )

There really is not enough details in your question to be able to really help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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