嗨,我通过加入一些 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 毫秒
有什么办法可以优化上面的查询吗?