如何创建函数索引以提高此语句的性能?
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;
答案 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
需要排在第三位。