Solved

how to easily find  table scan queries

Posted on 2007-11-15
9
2,656 Views
Last Modified: 2010-10-11
I have a very busy CPU and sql server.
Through Perfmon , I was able to find out that there are a lot of Full scans happening.
Now how do I identify which sql statements are doing these full scans.
I have used sql trace and captured heaps of sql statements in short periods but the problem is that there are heaps of statements.. i cant go through each of them , execute them and see execution plan to see if there is a full table scan..
Is there a easier way to trap table scan queries... ???
0
Comment
Question by:Wizilling
9 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20295173
use SQL Profiler. and add only the table scan event
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20295199
open trace properties, tick "show all events", scroll to Scans. hover over it to see the hint. add scan: stopped and scan: started. click on show all columns, tick ObjectID
0
 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
ID: 20295287
you can also add IndexID

the indexid and objectid refer to

select object_name(id) as objectname, indid as indexid, name as indexname
from sys.sysindexes where id = @objectid
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 13

Author Comment

by:Wizilling
ID: 20295335
THis only tell if a sacn is started or stopped.
within a time frame of 10 seconds , I can have at least 100-200 queries completed. all of them are either using a index scan or a table scan... How do I pinpoint which ones are just table scans.
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20295351
indexid < 2
0
 
LVL 12

Accepted Solution

by:
kselvia earned 250 total points
ID: 20296702
On SQL 2K5, this will give you the top 30 query plans with the most logical IO which will probably equate to most table scans.  If it does not, it will still probably tell you what needs attention.

Thanks to aneeshattingal.

SELECT  TOP 30 DB_NAME(dbid) AS DatabaseName
       ,OBJECT_NAME(ObjectID) AS ObjectName
       ,der.creation_time
       ,der.Last_execution_time
		,total_logical_reads , total_logical_writes, execution_count
       ,Text
FROM    sys.dm_exec_query_stats as der
CROSS APPLY sys.dm_exec_sql_text(der.sql_handle) as dest
WHERE DB_NAME(dbid) = '<MYDBNAME>'
ORDER BY total_logical_reads   DESC

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20297109
kselvia: you will have to keep running that because the query cache is a snapshot. whereas tracing is a running commentary. each for its own use. those sys tables tell you stats about the query cache, which holds x amount of precompiled plans.  fyi- plans are pushed out faster when on a system that doesn't use the sp_executesql pattern or stored procedures.
0
 
LVL 12

Expert Comment

by:kselvia
ID: 20298853
Not disagreeing, just making an observation.  Query plan cache is seperate from data cache and on a production system with enough memory I would think 99% of all plans should be cached after a little uptime.

On SQL 2K5, statements are cached, rather than batches. SQL server will generate "Auto-parameterized queries" for exec() or ad-hoc queries making them behave as if they were submitted as sp_executesql on SQL 2000. Some details are here http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx

I actually found that out because I was tuning something and rewrote an exec() to use sp_executesql only to find out it made no difference.  I searched to find out why.

SQL Server 2005 Performance Dashboard Reports is also pretty useful for identifying a variety of performance problems.

http://www.microsoft.com/downloads/details.aspx?FamilyId=1D3A4A0D-7E0C-4730-8204-E419218C1EFC&displaylang=en

It's also possible to actually get the table scans and their cost used in existing query plans.

I have been trying to figure out how to do it.

use master
GO
 
SELECT  TOP 30 
query_plan.value
('declare namespace p="http://schemas.microsoft.com/sqlserver/2004/07/showplan";
    (//p:RelOp)[1]/@PhysicalOp', 'nvarchar(1000)') ,
 der.plan_handle
,der.sql_handle
,DB_NAME(dest.dbid) AS DatabaseName
       ,OBJECT_NAME(dest.ObjectID) AS ObjectName
       ,der.creation_time
       ,der.Last_execution_time
		,der.total_logical_reads 
		,der.total_logical_writes 
		,der.execution_count
       ,dest.Text
		,pln.query_plan
FROM    sys.dm_exec_query_stats as der
CROSS APPLY sys.dm_exec_sql_text(der.sql_handle) as dest
CROSS APPLY sys.dm_exec_query_plan(der.plan_handle) as pln
WHERE DB_NAME(dest.dbid) = 'MyDatabase'
ORDER BY der.total_logical_reads   DESC 
 
 
@PhysicalOp="Table Scan" for table scan operations, but I can't figure out how to use XMLQuery correctly.  My first time to try.

Open in new window

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 20304118
Use Index tuning wizard...See what indexes it suggests to index...

Hope this helps...
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string fuctions 4 26
2016 SQL Licensing 7 41
Query Doesn't Allow New Records to be Added When Linked to SQL Server Backend 4 23
SQL server vNext 18 29
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question