August 25, 2010 at 7:37 am
Hi,
This might have been addressed already, but I am in hurry so didnt have a chance to browse through forum.I'm new to SQL server. Apologies if this is already covered and would appreciate if someone can point me to that link. Here's the problem description
I have the below query. The problem is that this query is doing a lot of logical reads which is slowing it down. Not quite sure, but I feel a self join would save lot of reads. (Correct me if I am wrong and suggest a self join query even if it might not reduce the number of reads.).
DELETE FROM RPF WITH (ROWLOCK)
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND e_id NOT IN ( SELECT DISTINCT e_id
FROM RPF WITH (NOLOCK)
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND r_f = 'N')
I tried the below, but apparently, there are certain e_ids which have both 'Y' and 'N' in the R_F flag. so obviously the results differ.
DELETE FROM RPF WITH (ROWLOCK)
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND R_F <> 'N'
Any help is appreciated.
-Chintan
August 25, 2010 at 11:35 am
Try using NOT EXISTS. It might reduce the Number Of Reads.
DELETE
FROM RPF RO WITH (ROWLOCK)
WHERE RO.U_S = 'PPP'
AND RO.e_d = '20100811'
AND NOT EXISTS ( SELECT * FROM RPF RI WITH (NOLOCK) WHERE RO.U_S = 'PPP' AND RO.e_d = '20100811' AND RO.r_f = 'N' AND RI.e_id = RO.e_id )
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
August 25, 2010 at 11:47 am
Thanks, I'm not at work now. I'll try that first thing I'm in and will post the results.
August 25, 2010 at 11:00 pm
The NOT EXISTS doesn't help much. It mnages to reduce the reads but only by a very very tiny number. Any other suggestions? Appreciate the help.
August 26, 2010 at 2:28 pm
I don't think you'd need the DISTINCT in your NOT IN clause. It won't matter anyway if there'd be more than one row returned per id. This would eliminate a usually expensive SORT DISTINCT operation.
Side note: I also vote against the heavy usage of all those locking hints. There a just few scenarios where such a hint is helpful (not that I can think of one right now though...).
So I would try:
DELETE FROM RPF
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND e_id NOT IN ( SELECT e_id
FROM RPF
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND r_f = 'N')
Another question would be: Are there any supporting indexes for the select query?
Or: Can you post the execution plan for that query?
August 27, 2010 at 9:31 am
This would be the join syntax (not tested as I dont have ddl) if you dont want a subquery.
WITH rpfnot (e_id) AS
(
SELECT DISTINCT e_id
FROM RPF
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND r_f = 'N'
)
DELETE RPF
FROM RPF
LEFT OUTER JOIN rpfnot ON RPF.e_id = rpfnot.e_id
WHERE rpfnot.e_id IS NULL
September 7, 2010 at 12:41 am
Thanks. I tried this, but there appears to be some syntax error with it. Have never seen this kind of statement before so couldnt fix it somehow.
any more suggestions?
September 7, 2010 at 12:59 am
Not a suggestion, but...
http://sqlinthewild.co.za/index.php/2010/03/23/left-outer-join-vs-not-exists/
Please post table definitions, index definitions and execution plan, as per http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
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
September 7, 2010 at 1:39 am
somaiya.chintan (8/25/2010)
DELETE FROM RPF WITH (ROWLOCK)
any specific reason to use ROWLOCK ? ...i think sql server itself decides to use locking mechanism.. i am just curious:-)
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 7, 2010 at 1:41 am
somaiya.chintan (9/7/2010)
Thanks. I tried this, but there appears to be some syntax error with it. Have never seen this kind of statement before so couldnt fix it somehow.any more suggestions?
use semicolon like ;WITH rpfnot (e_id) AS
(
SELECT DISTINCT e_id
FROM RPF
WHERE U_S = 'PPP'
AND e_d = '20100811'
AND r_f = 'N'
)
DELETE RPF
FROM RPF
LEFT OUTER JOIN rpfnot ON RPF.e_id = rpfnot.e_id
WHERE rpfnot.e_id IS NULL
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 7, 2010 at 1:49 am
Just note that it is not a requirement that a CTE start with a ;. It's a requirement that the statement before be terminated with a ;. Since this termination of statement with; is becoming more and more necessary on various statements, it will be useful to get into the the habit of always terminating every statement with a ;
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
September 7, 2010 at 1:58 am
GilaMonster (9/7/2010)
Just note that it is not a requirement that a CTE start with a ;. It's a requirement that the statement before be terminated with a ;. Since this termination of statement with; is becoming more and more necessary on various statements, it will be useful to get into the the habit of always terminating every statement with a ;
thanks Gail..i already know that but i posted it as i was assuming that the sql code is a intermediate part of any script or SP.
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply