Sometimes you HAVE to use nested queries - the logic of what you are
after simply won't allow any other choice.
But when at all possible, make it a join rather then a nested query.
When the engine goes to evaluate what you are doing, it MAY decide to
create a nested query style lookup based on what it thinks is going to
be fastest.
But, it MAY find another way.
If the nested query is indeed faster, then you can give the compiler a
hint to get the join as fast.
Not what you are after, but I'd look to indexes for even more speed.
Create an index on whatever field the '101' is equal to.
On the OTHER table, create an index on the join field.
Run those results.
Then create an index on the other fieldx field for the join. Rerun the
results.
If slower, give the compiler a hint.
A 10 second select statement on a 2 table join is generally not
acceptable.
-doug miller
> Hi,
> I'm a beginner in SQL Programming and i need to know whether
> usage of SQL Joins will improve performance of my queries. I Used the
> following queries that i find difference in Execution Time:
> Query 1:
> --------
> SELECT A.Field1, A.Field2, A.Field3 FROM Table1 A
> INNER JOIN Table2 B On A.[FieldX] = C.[FieldX] And C.FieldX = '101'
> Query 2:
> ---------
> SELECT A.Field1, A.Field2, A.Field3 FROM Table1 A
> WHERE A.[FieldX] in ( Select FieldX from Table2 where FieldX = '101')
> No.Of Records: 70000
> Query1 Execution Time : 10 Seconds
> Query2 Execution Time :14 Seconds
> Does this mean that i should always go for Joins instead of nested
> queries?
> Please help me on this.
> Thanks in Advance.
> Regards
> Sen.