我有下面的查询(用于相同的结果),我想优化查询,以减少数据库服务器的CPU使用率。
我在下面尝试了两个不同的查询,似乎两个查询具有相似的性能。 (使用SHOW PROFILES
检查)
任何建议或建议都将不胜感激。
提前谢谢你。
信息:
Table A has 7333 rows
Table B has 5656 rows
Table C has 1108 rows
Table D has ~150 rows (keep changing)
Table E has 18576 rows
查询1:
SELECT A.ct, A.res, A.ctype, A.cweight, B.lat, B.lng, C.first, C.second, C.third
FROM A
JOIN B
ON A.ct= B.dct
JOIN C
ON A.ct = C.dct
LEFT JOIN
(
SELECT D.dct
FROM D
JOIN E
ON D.oct = E.oct
WHERE E.oct is null
OR (E.oct is not null
AND E.wua <> '')
) as K
ON A.ct = K.dct
WHERE A.type = 1 AND B.lng != '' AND B.lat != '' AND A.black != 1 AND B.modi_time > (NOW() + INTERVAL 8 HOUR)
ORDER BY A.ct DESC
QUERY2:
SELECT A.ct, A.res, A.ctype, A.cweight, B.lat, B.lng, C.first, C.second, C.third
FROM A
JOIN B
ON A.ct = B.ct
JOIN C
ON A.ct = C.dct
WHERE A.type = 1 AND B.lng != '' AND B.lat != '' AND A.black != 1 AND B.modi_time > (NOW() + INTERVAL 8 HOUR)
AND A.ct NOT IN (
SELECT D.dct
FROM D
JOIN E
ON D.oct = E.oct
WHERE E.wua <> ''
)
ORDER BY A.ct DESC;
答案 0 :(得分:0)
根据我的经验, 尝试声明值的变量(NOW()+ INTERVAL 8 HOUR)然后在查询中使用变量,如:
AND B.modi_time>@variable
这可能会提高您的查询效果。