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;
For better assistance in answering your questions, please read this[/url].
Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]