<p>For example which is better:</p>

<pre><code>select * from t1, t2 where t1.country='US' and t2.country=t1.country and t1.id=t2.id
</code></pre>

<p>or</p>

<pre><code>select * from t1, t2 where t1.country'US' and t2.country='US' and t1.id=t2.id
</code></pre>

<p>better as in less work for the database, faster results.</p>

<p><strong>Note:</strong> Sybase, and there's an index on both tables of <code>country+id</code>.</p>

[{'answer_id': 72738, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': "<p>I'd lean towards only including your constant in the code once.  There might be a performance advantage one way or the other, but it's probably so small the maintenance advantage of only one parameter trumps it.</p>\n"}, {'answer_id': 72743, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>The correct answer probably depends on your SQL engine. For MS SQL Server, the first approach is clearly the better because the statistical optimizer is given an additional clue which may help it find a better (more optimal) resolution path.</p>\n'}, {'answer_id': 72745, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>I think it depends on the library and database engine.  Each one will execute the SQL differently, and there's no telling which one will be optimized.</p>\n"}, {'answer_id': 72746, 'author': 'Alan', 'author_id': 5878, 'author_profile': 'https://Stackoverflow.com/users/5878', 'pm_score': 0, 'selected': False, 'text': "<p>If you ever wish to make the query more general, perhaps substituting a parameter for the target country, then I'd go with your first example, as it requires only a single change. That's less to worry about getting wrong in the future.</p>\n"}, {'answer_id': 72754, 'author': 'mmaibaum', 'author_id': 12213, 'author_profile': 'https://Stackoverflow.com/users/12213', 'pm_score': 0, 'selected': False, 'text': '<p>I suspect this is going to depend on the tables, the data and the meta-data. I expect I could work up examples that would show results both ways - benchmark!</p>\n'}, {'answer_id': 72764, 'author': 'Dave Costa', 'author_id': 6568, 'author_profile': 'https://Stackoverflow.com/users/6568', 'pm_score': 2, 'selected': False, 'text': "<p>I don't think there is a global answer to your question.  It depends on the specific query.  You would have to compare the execution plans for the two queries to see if there are significant differences.</p>\n\n<p>I personally prefer the first form:</p>\n\n<p>select * from t1, t2 where t1.country='US' and t2.country=t1.country and t1.id=t2.id </p>\n\n<p>because if I want to change the literal there is only one change needed.</p>\n"}, {'answer_id': 72774, 'author': 'aggergren', 'author_id': 7742, 'author_profile': 'https://Stackoverflow.com/users/7742', 'pm_score': 0, 'selected': False, 'text': "<p>The extressions should be equivalent with any decent optimizer, but it depends on which database you're using and what indexes are defined on your table. </p>\n\n<p>I would suggest using the EXPLAIN feature to figure out which of expressions is the most optimal.</p>\n"}, {'answer_id': 72804, 'author': 'Clinton Pierce', 'author_id': 8173, 'author_profile': 'https://Stackoverflow.com/users/8173', 'pm_score': 2, 'selected': False, 'text': '<p>There are a lot of factors at play here that you\'ve left out.  What kind of database is it?  Are those tables indexed?  How are they indexed?  How large are those tables?</p>\n\n<p>(Premature optimization is the root of all evil!)</p>\n\n<p>It could be that if "t1.id" and "t2.id" are indexed, the database engine joins them together based on those fields, and then uses the rest of the WHERE clause to filter out rows.</p>\n\n<p>They could be indexed but incredibly small tables, and both fit in a page of memory.  In which case the database engine might just do a full scan of both rather than bother loading up the index.</p>\n\n<p>You just don\'t know, really, until you try.</p>\n'}, {'answer_id': 73172, 'author': 'Lost in Alabama', 'author_id': 5285, 'author_profile': 'https://Stackoverflow.com/users/5285', 'pm_score': 0, 'selected': False, 'text': "<p>I think a better SQL would be:</p>\n\n<p>select * from t1, t2 where t1.id=t2.id \nand t1.country ='US'</p>\n\n<p>There's no need to use the second comparison to 'US' unless it's possisble that the country in t2 could be different than t1 for the same id.</p>\n"}, {'answer_id': 73890, 'author': 'Jeremiah Peschka', 'author_id': 11780, 'author_profile': 'https://Stackoverflow.com/users/11780', 'pm_score': 0, 'selected': False, 'text': "<p>Rather than use an implicit inner join, I would explicitly join the tables.  </p>\n\n<p>Since you want both the id fields and country fields to be the same, and you mentioned that both are indexed (I'm presuming in the same index), I would include both columns in the join so you can make use of an index seek instead of a scan.  Finally, add your where clause.</p>\n\n<pre><code>SELECT *\n  FROM t1\n  JOIN t2 ON t1.id = t2.id AND t1.country = t2.country\n WHERE t1.country = 'US'\n</code>\n</pre>\n"}, {'answer_id': 74676, 'author': 'Arthur Miller', 'author_id': 13085, 'author_profile': 'https://Stackoverflow.com/users/13085', 'pm_score': 2, 'selected': True, 'text': "<p>I had a situation similar to this and this was the solution I resorted to:</p>\n\n<p>Select * \nFROM t1 \nINNER JOIN t2 ON t1.id = t2.id AND t1.country = t2.country AND t1.country = 'US'</p>\n\n<p>I noticed that my query ran faster in this scenario.  I made the assumption that joining on the constant saved the engine time because the WHERE clause will execute at the end.  Joining and then filtering by 'US' means you still pulled all the other countries from your table and then had to filter out the ones you wanted.  This method pulls less records in the end, because it will only find US records.</p>\n"}]
['https://Stackoverflow.com/questions/72699', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12386/']