Link to home
Start Free TrialLog in
Avatar of Phman Super
Phman SuperFlag for United States of America

asked on

SQL query performance

I have 3 different but  equivalent queries. Do you mind tell me how to check and see which one has the best execution performance?

-- 1
select a from t1
where mod(a,5) = 0 and b in
( select b from t2
     where c in ( SELECT c FROM t3 where mod(d,5) = 0 ));
-- 2
select t1.a from t1, t2, t3 where
t1.b = t2.b and t2.c = t3.c and mod(t1.a,5) = 0 and mod(t3.d,5) = 0;

-- 3
select t1.a from (t1 join t2 on t1.b = t2.b ) join t3 on t2.c = t3.c
where mod(t1.a,5) = 0 and mod(t3.d,5) = 0;
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Issue following command in SSMS:

Open in new window

and then your 3 queries in one batch. Press the "Include Actual Execution plan" button before you issue the queries.

And now you have two sets of results:
The Message Tab contains the number of logical reads and the total execution time. It is the first sign of the query performance.
The Execution plan Tab shows how the query was executed in all details. The Query cost or the percentage in each query shows what of the three variants is the best one.
Avatar of PortletPaul
Flag of Australia image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Phman Super


Hey Paul, I already figure that out yesterday. Thanks any way!
Hmm... The question was "Do you mind tell me how to check and see which one has the best execution performance?"

Paul pointed to the syntax error which you've solved already but you did not tell which one of the tree queries (with respect to the equality of the two join variants) executed with the best performance? What measurement method did you use?

Of course, you have to measure it yourself because the results depend on the existing data and indexes.
PLease also note that IN and JOIN are not givng the same results. A joiin B on a.a =b.a can result in rows of A repeated if there is more than one matching row in B.
In general, IN works best if you have a lot of "duplicate" IDs, or the IN list contains a low amount of rows.

Also note that if you perform measuring now with a particular dataset, performance might vary heavily with other data. There is no reliable method to tell if a query runs faster than a different one, because it always depends on data. E.g. you can't test with 100 rows, that will always give "fast" results no matter how "bad" the query is.