以下 MySQL 查询有什么地方这么慢?
我尝试向 DocumentRevision.document 和 Document.status 添加索引,但执行查询通常需要很长时间(似乎如果按顺序进行此查询,查询速度会加快)。是否有另一种更有效的方法来实现相同的结果?该查询获取数据库中所有可用文档的所有最新版本。有两张桌子。文档和文档修订。文档表只包含 id 和 status 字段,而 DocumentRevision 包含所有数据和“文档” id 字段,以便它知道它是哪个文档的修订版。
SELECT rev.document as documentId, rev.id as revId, rev.name as name,
rev.dateCreated as dateCreated, rev.documentOrder as documentOrder
FROM (
SELECT Document.id as docId, MAX(DocumentRevision.id) as maxRevId
FROM Document, DocumentRevision
WHERE Document.id = DocumentRevision.document AND Document.status = 0
GROUP BY Document.id
) AS x
INNER JOIN DocumentRevision as rev on rev.document = x.docId
AND rev.id = x.maxRevId
ORDER BY dateCreated DESC;