Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Table scan paranoia


Table scan paranoia

Author
Message
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47415 Visits: 44405
I can't see any useful covering indexes for that query, so it's probably just a case of the indexes being insufficiently selective and not covering. Cheaper to scan the table than do all the necessary key lookups.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


sqldriver
sqldriver
SSChasing Mays
SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)

Group: General Forum Members
Points: 606 Visits: 2492
Thanks Gail. That's the much smarter way of saying what I was thinking.
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1163 Visits: 9738
sqldriver (5/9/2014)
I have a stored procedure hitting a 761969 row table and doing several table scans on it...


Properly written queries will cut down on those scans. Consider the query you posted. It can be rewritten like this:

;WITH updater AS (
SELECT TOP(@PRM_BufferSize)
TextExtractionStatus,
NEWTextExtractionStatus = (@PRM_Computer_id + 10000),
Computer_id,
NEWComputer_id = @PRM_Computer_id
FROM dbo.tbl_extr_items b
JOIN tbl_extensions d
ON b.extension_id = d.extension_id
JOIN item_types it
ON b.item_type_id = it.item_type_id
WHERE b.TextExtractionStatus IS NULL
AND collection_surr_id = @PRM_Collection_Surr_id
AND active = 1
AND process_status >= 200
ORDER BY item_idx
)
UPDATE updater SET
TextExtractionStatus = NEWTextExtractionStatus,
Computer_id = NEWComputer_id;



- because item_idx is unique. Here's a little test to show how it works:
DROP TABLE #test; DROP TABLE #Second;
CREATE TABLE #test (item_idx INT IDENTITY(1,1) PRIMARY KEY, TextExtractionStatus INT, Computer_id INT);
INSERT INTO #test (TextExtractionStatus, Computer_id)
VALUES (NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10),(NULL, 10);

CREATE TABLE #Second (item_idx INT, Something INT)
INSERT INTO #Second (item_idx, Something)
VALUES (1,1),(2,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),(10,1),(11,1);

SELECT * FROM #test;

DECLARE @PRM_BufferSize INT = 3, @PRM_Computer_id INT = 5;

WITH updater AS (
SELECT TOP(@PRM_BufferSize)
TextExtractionStatus,
NEWTextExtractionStatus = (@PRM_Computer_id + 10000),
Computer_id,
NEWComputer_id = @PRM_Computer_id
FROM #test t
INNER JOIN #Second s ON s.item_idx = t.item_idx
)
UPDATE updater SET
TextExtractionStatus = NEWTextExtractionStatus,
Computer_id = NEWComputer_id;

SELECT * FROM #test;




Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
sqldriver
sqldriver
SSChasing Mays
SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)SSChasing Mays (606 reputation)

Group: General Forum Members
Points: 606 Visits: 2492
Thanks Chris. I'll do some testing this week with that concept.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search