您好,我正在通过连接大约 8 个表进行大型查询,我将使用该单个查询本身获取所有详细信息。
现在我想知道将所有表连接在一起是否好,或者我们可以运行单独的查询以使用主查询中的 id 获取循环内的详细信息?而且,在循环内运行查询是否好?
下面是我正在使用的查询,
SELECT node.nid AS nid,
node.uid,
node.title AS node_title,
og.og_description AS og_og_description,
node.created AS node_created,
og.og_selective AS og_og_selective,
tn.tid,
td.name as groupcategory,
(select count(ou.uid) from og_uid ou,users u where ou.uid=u.uid and u.status=1 and ou.is_active=1 and ou.nid=node.nid) as ucount,
f.filepath,
CONCAT(statusbook_users.first_name,' ',statusbook_users.last_name) AS users_name
FROM node node
LEFT JOIN og og ON node.nid = og.nid
INNER JOIN content_type_group ctg ON (node.nid = ctg.nid AND ctg.field_cn_group_type_value IS NULL)
LEFT JOIN files f ON (ctg.field_group_image_fid = f.fid)
INNER JOIN users users ON node.uid = users.uid
INNER JOIN statusbook_users statusbook_users ON users.uid= statusbook_users.uid
INNER JOIN og_uid og_uid ON og.nid=og_uid.nid
INNER JOIN term_node tn ON node.nid=tn.nid
INNER JOIN term_data td ON tn.tid=td.tid
WHERE (node.status <> 0) AND (node.type IN ('group'))
AND og_uid.is_active=1 AND og_uid.uid=65
上述查询在我的本地系统中的执行时间是 12824.59 毫秒,而当我在生产服务器中运行相同的查询时,它花费了 217.34 毫秒
还有什么方法可以优化上面的查询吗??
最佳答案
通常最好进行一次大型查询,然后让 DBMS 优化器进行艰苦的工作,找出最快的方法来完成您的查询。它通常比你在这方面做得更好。
但是要确定,您应该尝试两个版本和配置文件。
关于mysql - 大查询会影响性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825399/