使用左联接时无法启用复合索引

kxe2p93d  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(367)

这个问题在这里已经有答案了

当使用左连接时,我能有一个复合索引吗(2个答案)
12个月前关门了。
我的目标是在student表上使用一个复合索引。学生表将内部连接到报名表。我在student表上创建了索引,如下所示:

  1. CREATE INDEX email_phonenumber_student_idx
  2. ON student(phonenumber, email);

当我运行查询时

  1. SELECT Phonenumber, email from student
  2. left join enrolment on enrolment.studentnumber = student.studentnumber
  3. where months_between(SYSDATE, dateofbirth)/12 >= 18 and
  4. enrolment.studentnumber is null and
  5. student.phonenumber = '07123456788' and student.email = 'Chris@Lailasman.com’;

它按预期工作,但是索引没有被使用,因为当我为查询“解释计划”时,我只能将主键视为索引。我是否在错误的表上创建了索引?问题是,我想使用复合键,但是,联接表不包含任何用于复合索引的列。

  1. PLAN_TABLE_OUTPUT
  2. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  3. Plan hash value: 1388008413
  4. ---------------------------------------------------------------------------------------------
  5. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
  6. ---------------------------------------------------------------------------------------------
  7. | 0 | SELECT STATEMENT | | 1 | 63 | 0 (0)| 00:00:01 |
  8. | 1 | NESTED LOOPS ANTI | | 1 | 63 | 0 (0)| 00:00:01 |
  9. |* 2 | TABLE ACCESS BY INDEX ROWID| STUDENT | 1 | 50 | 0 (0)| 00:00:01 |
  10. |* 3 | INDEX UNIQUE SCAN | SYS_C0022463 | 1 | | 0 (0)| 00:00:01 |
  11. |* 4 | INDEX RANGE SCAN | SYS_C0022468 | 1 | 13 | 0 (0)| 00:00:01 |
  12. ---------------------------------------------------------------------------------------------
  13. PLAN_TABLE_OUTPUT
  14. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  15. Predicate Information (identified by operation id):
  16. ---------------------------------------------------
  17. 2 - filter("STUDENT"."EMAIL"='Chris@Lailasman.com' AND
  18. MONTHS_BETWEEN(SYSDATE@!,INTERNAL_FUNCTION("STUDENT"."DATEOFBIRTH"))/12>=18)
  19. 3 - access("STUDENT"."PHONENUMBER"='07123456788')
  20. 4 - access("ENROLMENT"."STUDENTNUMBER"="STUDENT"."STUDENTNUMBER")
6vl6ewon

6vl6ewon1#

这个 explain 显示在给定数据的当前“形状”(大小、分布等)的情况下,优化器将实际执行的操作。
数据库最昂贵的操作是磁盘i/o。由于只有17行,整个表很可能通过一个i/o操作(通常是2k页)读入内存,因此将整个表读入内存并扫描内存中的所有行要比通过索引更快,因为索引需要多次i/o操作。
您可能会发现索引开始用于1000行,但这取决于您的模式。

相关问题