Is a JOIN faster than a WHERE?

前端 未结 10 1072
渐次进展
渐次进展 2020-11-29 05:44

Suppose I have two tables that are linked (one has a foreign key to the other):

CREATE TABLE Document (
  Id INT PRIMARY KEY,
  Name VARCHAR 255
)

CREATE TAB         


        
10条回答
  •  余生分开走
    2020-11-29 06:08

    I guess that it doesn't make a difference too. To be sure you can check if the explain plan of those two queries is identical. In order to look at the explain plan in MySQL you have to put the "explain" keyword before the statement, eg:

    EXPLAIN
    SELECT *
    FROM Document, DocumentStats
    WHERE DocumentStats.Id = Document.Id
      AND DocumentStats.NbViews > 500
    

    I'm sure there exists an equivalent in MSSQL too.

    By the way: This looks like this is a 1:1 relationship so I'd just include the nbviews attribute directly in the Document table, therefore you can save a join.

提交回复
热议问题