Avatar of phman1275
 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;
Microsoft SQL Server

Avatar of undefined
Last Comment

8/22/2022 - Mon
Pavel Celba

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.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Hey Paul, I already figure that out yesterday. Thanks any way!
Pavel Celba

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

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.