我在另一个内部使用mysql select时遇到了麻烦。
此查询需要0,0007秒:
SELECT node.title AS node_title
FROM node node
WHERE (( (node.status = '1') AND (node.type IN ('event')) AND (node.uid= 1) ))
但这需要9秒:
SELECT node.title AS node_title
FROM node node
WHERE (( (node.status = '1') AND (node.type IN ('event')) AND (node.uid= 1) OR node.nid IN (SELECT n.nid FROM tag n WHERE n.uid='1') ))
有人知道为什么吗?两者都非常快。
我需要做的是获取一些数据,我可以通过一些连接获得一些数据。 我也尝试过这种方式,但遇到同样的性能问题:
SELECT node.title AS node_title
FROM
node node
LEFT JOIN tag tag ON tag.uid = 4
WHERE (( (node.status = '1') AND (node.type IN ('event')) AND (node.uid = '4') OR (tag.nid = node.nid) ))
有什么想法吗?感谢
答案 0 :(得分:0)
有时or
会以不良方式影响查询计划。你可以试试这个:
SELECT n.title AS node_title
FROM node n
WHERE n.status = '1' AND n.type IN ('event') AND n.uid = 1
union
SELECT n.title
FROM node n
WHERE exists (SELECT t.nid FROM tag t WHERE t.uid = '1' and t.nid = n.nid);
为了使其发挥最佳效果,请使用以下两个索引:
node(status, type, uid)
tag(uid, nid)