我想提高 SQL 查询的性能。我有表 'tblEntries' 和列'sTag':
+----+------+-------+------+---------+
| Id | sTag | sPath | lVer | bActive |
+====+======+=======+======+=========+
| 1 | NULL | t1 | 100 | + |
| 2 | NULL | t2 | 110 | + |
| 3 | x1 | t4 | 110 | + |
| 4 | x1 | t3 | 120 | + |
| 5 | x2 | t7 | 100 | + |
+----+------+-------+------+---------+
客户端查询具有指定标记的路径,查询应返回具有下一个条件的指定条目:
- 如果有一个带有指定标签的条目,它应该返回具有最大 lVer 值的条目,并且 bActive 应该是 TRUE。
- 如果没有带有指定标签的条目,它应该返回带有 NULL sTag 值和最大 lVer 值的条目,并且 bActive 应该是 TRUE。
“标记”条目比“未标记”条目具有更高的优先级。
当前的 SQL 查询是:
SELECT lVer, sPath
FROM tblEntries
INNER JOIN
(SELECT MAX(lVer) AS V, sTag AS T
FROM tblEntries
WHERE bActive = TRUE
GROUP BY sTag)
ON lVer = V
WHERE T IS NULL OR T = 'user_tag'
ORDER BY T DESC
然后我可以选择满足条件的第一个条目。我可以避免子查询吗?
谢谢!