【发布时间】:2012-07-05 11:17:49
【问题描述】:
假设我有两张桌子,
Student Test
Id Name TestId Type StudentId
-- ---- ------ ---- ---------
1 Mark 774 Hard 1
2 Sam 774 Hard 2
3 John 775 Easy 3
现在我必须找到那些在 MySql 中参加过“硬”类型测试的学生(学生 id、姓名和 testid)。
哪个更好(在性能方面)?
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'
我可以知道原因吗?(假设有数百万学生和数百万种测试)
【问题讨论】:
-
如果通过分析器运行查询,执行计划有什么不同? (附带问题...
Test与User真的是多对一的吗?也许我不知道您的域,但我认为会有有限数量的测试实体和有限数量的用户实体以及两者之间的多对多关系。真的有百万个唯一测试实体吗?) -
@David 是的,这是一个多对多的关系,但我的例子并没有显示出来......是的,也许我夸大了几百万个独特的实体。但是我说有许多独特的测试类型的原因是我认为很少有独特的实体,两个查询具有相同的性能,但没有很大。的实体,差异可能是巨大的。
-
@vyegorov 请正确阅读问题...我不是在谈论左外连接。我也知道 where 子句在连接后执行...我只想知道性能