views:

49

answers:

2

I have written query, I wanted to know the effect of usage of IN clause in Query.

Query I Select * from khatapayment_track Where Property_ID IN (Select Property_id from khata_header where DIV_ID=2)

Query II Select * from khatapayment_track KPT, khata_header KH Where kh.Property_id=KPT.Property_id and KH.DIV_Id=2

I would like to know 1) which one is faster 2) Any effects of using IN clause, is it advisable to use if a query has a 3 IN clause.

Can you please help me with examples

A: 

Query II is faster in your example as you are using subquery in Query I. but consider follwing example which returns similar o/p but query i will returns faster

select * from tableName  where id in (1,2,3,4)

is similar to

select * from tableName  where id =1 OR id =2 OR id =3 Or id =4
Salil
@jensgram:- yes please check my Edited answer, i realise that after.
Salil
@Salil I didn't downvote, but I removed my comment. Still don't see the relevance of the `IN()` vs. `OR-OR-OR` example, though :)
jensgram
+2  A: 

Your second query is faster, but it is better to use joins (it looks nicer and it have the same execution plan):

select
  *
from
  khatapayment_track t
    inner join khata_header h on (h.property_id = t.property_id)
where
  h.div_id = 2

Also you can use mysql profiler to compare your queries.

silent
Explicit joins don't just look nicer, they are easier to maintain (as intent is clearer) and less subject to accidental cross joins. There is no reason to ever use an implicit join. YOu gain nothing and have the potential for problems as well as using syntax that is 18 years out of date. Once you start to need outer joins then you have even more problems with the structure. It's just a poor idea all around to use implicit joins.
HLGEM
I just said it is better than using "in". And what are problems in using this synthax?
silent