使用连接的索引函数

时间:2015-10-10 11:32:32

标签: sql database oracle join indexing

如何创建函数索引以提高此语句的性能?

SELECT COUNT(*) 
FROM TableName as a, TableName as b
WHERE a.first_name = b.first_name 
  AND a.last_name = b.last_name 
  AND a.person_id < b.person_id;

1 个答案:

答案 0 :(得分:2)

首先,学习明确的join语法:

SELECT COUNT(*)
FROM TableName a JOIN 
     TableName b
     ON a.first_name = b.first_name AND a.last_name = b.last_name AND
        a.person_id < b.person_id;

最适合此查询的索引:

create index idx_tablename_first_last_person on tablename(first_name, last_name, person_id);

您实际上可以交换索引中的前两列,但由于不平等,person_id需要排在第三位。