Suppose I have two tables that are related (one has a foreign key):
CREATE TABLE Document ( Id INT PRIMARY KEY, Name VARCHAR 255 ) CREATE TABLE DocumentStats ( Id INT PRIMARY KEY, DocumentId INT,
I know this is not the smartest way to do something, but this is the best example I could come up with.
Now I want to get all the documents with more than 500 types. Two solutions that come to my mind:
SELECT * FROM Document, DocumentStats WHERE DocumentStats.Id = Document.Id AND DocumentStats.NbViews > 500
or:
SELECT * FROM Document INNER JOIN DocumentStats ON Document.Id = DocumentStats.Id WHERE DocumentStats.NbViews > 500
Are both queries equivalent, or is there one way that is much better than the other? If so, why?
I know that my example is not perfect, and that some tuning might be required for queries, but I hope you understand the point;)!
EDIT: on request in the answers, this question was aimed at MSSQL, but I would be interested to know if it differs from other DB mechanisms (MySQL, etc.)
performance sql join where-clause
Wookai Jul 15 '09 at 7:31 2009-07-15 07:31
source share