Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to Eliminate 'IN' Expand / Collapse
Author
Message
Posted Thursday, March 1, 2012 1:18 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, November 20, 2014 12:36 AM
Points: 65, Visits: 205
Hi,

How can i eliminate IN from following query ?

select * from idata a where isnull(validationstatus,'Y')='Y'
and srno not in (select srno from IFDetails where eby =1 )
and edate between '20.02.2012' and '20.02.2012'

this query takes too long time due to both tables having more then 1.5 million rows.

thanks
Post #1259943
Posted Thursday, March 1, 2012 1:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:21 AM
Points: 5,490, Visits: 10,345
You could use an OUTER JOIN instead, although I suspect that you may end up with the same execution plan. Have you considered indexes on the srno column in both tables? Also, the BETWEEN operator is not needed since the beginning and end of the range are identical. Use "=" instead.

John
Post #1259952
Posted Thursday, March 1, 2012 2:57 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:13 AM
Points: 40,627, Visits: 37,092
Why do you want to eliminate IN? It's usually more efficient than an equivalent join.

If srno in IFDetails is nullable, you may want to use EXISTS instead. Also your first predicate is not SARGable, so that may result in the query being a little slower than it could be.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1259985
Posted Friday, March 2, 2012 12:54 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, November 20, 2014 12:36 AM
Points: 65, Visits: 205
thanks for reply,

as far as I know that, Join is much faster then IN.

i am sorry but i can't understand what you mean by saying :

'Also your first predicate is not SARGable, so that may result in the query being a little slower than it could be.'

thanks for giving me time.
Post #1260650
Posted Friday, March 2, 2012 1:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:21 AM
Points: 5,490, Visits: 10,345
There's plenty of information out there. Use your favourite search engine to search for "sargable predicates".

John
Post #1260669
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse