ATM我正在努力学习如何有效地使用数据库知识,并希望获得一些专家意见.我目前没有任何性能问题.我想知道,你将如何使用此查询处理索引:
SELECT B.event,COALESCE(B.system,C.surname || ' ' || C.forename) AS name,C.label,B.timestamp FROM A INNER JOIN B ON A.event=B.event INNER JOIN C ON B.state=C.id LEFT OUTER JOIN D ON B.hur=D.id WHERE A.id IN(12,13,14,15,...) ORDER BY B.event,B.timestamp
A.id,C.id和D.id已经是主键
UPDATE
通常我会把INDEX(A.event)和INDEX(B.event,B.timestamp).它是否正确?
那么B.event,B.state和B.hur呢?
解决方法
重写您的查询,如下所示:
SELECT B.event,B.timestamp FROM B INNER JOIN C ON C.id = B.state LEFT OUTER JOIN D ON D.id = B.hur WHERE B.event IN ( SELECT event FROM A WHERE A.id IN (12,15) ) ORDER BY B.event,B.timestamp
,并在B上创建一个复合索引(事件,时间戳)