【发布时间】:2015-12-12 06:12:44
【问题描述】:
假设你有两个表:
Student(id, class) // 100 rows
Course(id, course) // 100 rows
最初假设两个表上都没有索引。现在假设我们有一个查询:-
select id, course
from Student join course
on student.id = Course.id and student.id = 20
由于你没有任何索引,所以你需要遍历两个表中的所有行。
Time complexity - O(100 x 100)
现在我们更新了表,Student.id 是主键。将在其上创建聚集索引,现在整体复杂度为
Time complexity - O(log 100) // Nested loop join
你认为我的假设是正确的吗?有人可以帮我吗?
嵌套循环连接算法在这里:
【问题讨论】:
-
我认为你不应该混合join + where。
select id, course from Student join Course ON student.id = Course.id WHERE student.id = 20 -
请不要使用这种过时的连接语法。它可以工作,但 SQL 标准为此提供了
JOIN .. ON...。
标签: mysql sql indexing time-complexity clustered-index