WHERE子句和ON子句中的条件

psy*_*psy 5 mysql sql

假设我有两张桌子,

   Student                           Test

Id    Name                   TestId   Type  StudentId
--    ----                   ------   ----  --------- 
1     Mark                     774    Hard     1  
2     Sam                      774    Hard     2 
3     John                     775    Easy     3
Run Code Online (Sandbox Code Playgroud)

现在我必须找到那些在MySql中进行"硬"测试的学生(学生ID,姓名和证词).

哪一个更好(在性能方面)?

1.Select student.id,student.name,test.testid 
  from student 
  join test 
  on test.studentid=student.id and test.type='hard'

2.Select student.id,student.name,test.testid 
  from student 
  join test 
  on test.studentid=student.id 
  where test.type='hard'
Run Code Online (Sandbox Code Playgroud)

我可以知道原因吗?(假设有数百万学生和百万种类型的考试)

Sim*_*iak 6

两者都相同但性能不同.EXPLAIN PLAN如果您想选择具有更好性能的查询,我建议您使用查询.

看看了解查询执行计划.

此外,您可以INDEXES使用最常用的WHERE子句中的列来改进查询,也可以查看关系代数.