【问题标题】:DBMS using index to increase performance (with example)DBMS 使用索引来提高性能(示例)
【发布时间】:2014-02-03 22:55:36
【问题描述】:
select s.name,c.name,r.edition,r.grade
from students s, courses c, registrations r
where s.student_id=r.student_id
and r.course_id=c.course_id
and r.grade > 10
order by 1,2,3;

在“等级”列中创建和索引会提高这个查询的性能吗?

【问题讨论】:

  • 我可能会回答自己,但如果我们在“等级”中使用索引(我想象一个简单的二叉树),我们不需要扫描所有表来获取大于的值,因为例如,10. 使用索引,我们可以评估很多行,因为数据是“排序的”并且很容易获得特定值。对吗?

标签: sql performance indexing database


【解决方案1】:

在“等级”列中创建和索引会提高这个查询的性能吗?

可能 - 这取决于 grade 列中的值的分散程度 - 如果 99%(为了论证)的值是 > 10,编译器可能会认为不值得使用无论如何,它必须基本上扫描表时的索引。

【讨论】:

    【解决方案2】:

    几乎任何用于过滤查询结果的字段都将受益于索引,所以是的,grade 上的索引很可能会有所帮助。

    摆脱旧式联接不会提高性能,但这是个好主意:

    SELECT s.name,c.name,r.edition,r.grade
    FROM registrations r
    JOIN courses c
     ON r.course_id=c.course_id
    JOIN students s
     ON s.student_id=r.student_id
    WHERE r.grade > 10
    ORDER BY s.name,c.name,r.edition
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-19
      • 2013-12-05
      • 2011-09-04
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多