Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's optimal? UNION vs WHERE IN (str1, str2, str3)

I'm writing a program that sends an email out at a client's specific local time. I have a .NET method that takes a timezone & time and destination timezone and returns the time in that timezone. So my method is to select every distinct timezone in the database, check if it is the correct time using the method, then select every client out of the database with that timezone(s).

The query will look like one of these. Keep in mind the order of the result set does not matter, so a union would be fine. Which runs faster, or do they really do the same thing?

SELECT email FROM tClient WHERE timezoneID in (1, 4, 9)

or

SELECT email FROM tClient WHERE timezoneID = 1
    UNION ALL SELECT email FROM tClient WHERE timezoneID = 4
    UNION ALL SELECT email FROM tCLIENT WHERE timezoneID = 9

Edit: timezoneID is a foreign key to tTimezone, a table with primary key timezoneID and varchar(20) field timezoneName. Also, I went with WHERE IN since I didn't feel like opening up the analyzer.

Edit 2: Query processes 200k rows in under 100 ms, so at this point I'm done.

like image 465
Shawn Avatar asked Aug 24 '08 16:08

Shawn


1 Answers

Hey! These queries are not equivalent.

Results will be same only if assuming that one email belongs only to the one time zone. Of course it does however SQL engine doesn't know that and tries to remove duplicities. So the first query should be faster.

Always use UNION ALL, unless you know why you want to use UNION.

If you are not sure what is difference see this SO question.

Note: that yell belongs to previous version of question.

like image 62
Jakub Šturc Avatar answered Sep 18 '22 23:09

Jakub Šturc