<p>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 &amp; 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). </p>

<p>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?</p>

<pre><code>SELECT email FROM tClient WHERE timezoneID in (1, 4, 9)
</code></pre>

<p>or</p>

<pre><code>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
</code></pre>

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

<p><em>Edit 2: <strong>Query processes 200k rows in under 100 ms, so at this point I'm done.</strong></em></p>

[{'answer_id': 25186, 'author': 'Michał Piaskowski', 'author_id': 1534, 'author_profile': 'https://Stackoverflow.com/users/1534', 'pm_score': 1, 'selected': False, 'text': '<p>My first guess would be that <pre>SELECT email FROM tClient WHERE timezoneID in (1, 4, 9)</pre> will be faster as it requires only single scan of the table to find the results, but I suggest checking the execution plan for both queries.</p>\n'}, {'answer_id': 25188, 'author': 'David Schlosnagle', 'author_id': 1750, 'author_profile': 'https://Stackoverflow.com/users/1750', 'pm_score': 2, 'selected': False, 'text': '<p>For most database related performance questions, the real answer is to run it and analyze what the DB does for your dataset. Run an explain plan or trace to see if your query is hitting the proper indexes or create indexes if necessary.</p>\n\n<p>I would likely go with the first using the IN clause since that carries the most semantics of what you want. The timezoneID seems like a primary key on some timezone table, so it should be a foreign key on email and indexed. Depending on the DB optimizer, I would think it should do an index scan on the foreign key index.</p>\n'}, {'answer_id': 25190, 'author': 'Dima Malenko', 'author_id': 2586, 'author_profile': 'https://Stackoverflow.com/users/2586', 'pm_score': 1, 'selected': False, 'text': '<p>I do not have MS SQL Query Analyzer at hand to actually check my hypothesis, but think that WHERE IN variant would be faster because with UNION server will have to do 3 table scans whereas with WHERE IN  will need only one. If you have Query Analyzer check execution plans for both queries.</p>\n\n<p>On the Internet you may often encounter suggestions to avoid using WHERE IN, but that refers to cases where subqueries a used. So this case is out of scope of this recommendation and additionally is easier for reading and understanding.</p>\n'}, {'answer_id': 25212, 'author': 'ANaimi', 'author_id': 2721, 'author_profile': 'https://Stackoverflow.com/users/2721', 'pm_score': 0, 'selected': False, 'text': "<p>Some DBMS's Query Optimizers modify your query to make it more efficient, so depending on the DBMS your using, you probably shouldn't care.</p>\n"}, {'answer_id': 25244, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>I think that there are several very important information missing in the question.  First of all, it is of great importance weather timezoneID is indexed or not, is it part of the primary key etc.  I would advice everyone to have a look at the analyzer, but in my experience the WHERE clause should be faster, especially with an index.  The logic is something like, there is an additional overhead in the union query, checking types, column numbers in each etc. </p>\n'}, {'answer_id': 25284, 'author': 'Matt Rogish', 'author_id': 2590, 'author_profile': 'https://Stackoverflow.com/users/2590', 'pm_score': 2, 'selected': False, 'text': '<p>In the book "SQL Performance Tuning", the authors found that the UNION queries were slower in all 7 DBMS\' that they tested (SQL Server 2000, Sybase ASE 12.5, Oracle 9i, DB2, etc.): <a href="http://books.google.com/books?id=3H9CC54qYeEC&amp;pg=PA32&amp;vq=UNION&amp;dq=sql+performance+tuning&amp;source=gbs_search_s&amp;sig=ACfU3U18uYZWYVHxr2I3uUj8kmPz9RpmiA#PPA33,M1" rel="nofollow noreferrer">http://books.google.com/books?id=3H9CC54qYeEC&amp;pg=PA32&amp;vq=UNION&amp;dq=sql+performance+tuning&amp;source=gbs_search_s&amp;sig=ACfU3U18uYZWYVHxr2I3uUj8kmPz9RpmiA#PPA33,M1</a></p>\n\n<p>The later DBMS\' may have optimized that difference away, but it\'s doubtful. Also, the UNION method is much longer and more difficult to maintain (what if you want a third?) vs. the IN.</p>\n\n<p>Unless you have good reason to use UNION, stick with the OR/IN method.</p>\n'}, {'answer_id': 25293, 'author': 'Jakub Šturc', 'author_id': 2361, 'author_profile': 'https://Stackoverflow.com/users/2361', 'pm_score': 3, 'selected': True, 'text': '<h1>Hey! These queries are not equivalent.</h1>\n\n<p>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.</p>\n\n<p>Always use UNION ALL, unless you know why you want to use UNION.</p>\n\n<p>If you are not sure what is difference see <a href="https://stackoverflow.com/questions/49925/what-is-the-difference-between-union-and-union-all-in-oracle">this</a> SO question.</p>\n\n<p><em>Note: that yell belongs to <a href="https://stackoverflow.com/revisions/viewmarkup/31874">previous version</a> of question.</em></p>\n'}]
['https://Stackoverflow.com/questions/25182', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/26/']