糟糕的搜索查询导致我们的网站崩溃。写入计数每个子类别的人,然后将它们加在一起以获得主要类别的总数。 对于分页Next,Last,Pages,然后使用JOIN / INNER JOIN(该页面的所有产品)编写查询。它创造了太多的步骤,从而减慢了所有的速度,直到它超时。救命!
查询摘要:
SELECT COUNT(DISTINCT node.nid) AS cnt FROM
content_type_product cp
LEFT JOIN node node ON node.nid = cp.nid
LEFT JOIN uc_products uc_products ON node.nid = uc_products.nid
LEFT JOIN users users ON users.uid = node.uid
LEFT JOIN files ON files.fid = cp.field_product_image_1_value
LEFT JOIN content_type_product_tags ct ON ct.field_product_product_id_value = cp.nid
WHERE
(
node.status <> N
)
AND
(
ct.field_product_tag_id_value = N
)
AND
(
cp.field_deleted_value <> N
)
AND
(
cp.field_stock_level_value > N
)
AND EXISTS
(
SELECT scp.nid FROM content_type_store scp
LEFT JOIN node snode ON snode.nid = scp.nid
LEFT JOIN users susers ON susers.uid = snode.uid
WHERE susers.name = users.name
AND scp.field_shop_activated_value = 'S'
AND scp.field_shop_suspended_value = 'S')
ORDER BY cp.field_product_last_changed_value DESC;
查询示例:
SELECT count(Distinct node.nid) as cnt
FROM content_type_product cp
LEFT JOIN node node ON node.nid = cp.nid
LEFT JOIN uc_products uc_products ON node.nid = uc_products.nid
LEFT JOIN users users ON users.uid = node.uid
LEFT JOIN files ON files.fid = cp.field_product_image_1_value
LEFT JOIN content_type_product_tags ct ON ct.field_product_product_id_value = cp.nid
WHERE
(
node.status <> 0
)
AND
(
ct.field_product_tag_id_value = 478
)
AND
(
cp.field_deleted_value <> 1
)
AND
(
cp.field_stock_level_value > 0
)
AND EXISTS
(
SELECT scp.nid FROM content_type_store scp
LEFT JOIN node snode on snode.nid = scp.nid
LEFT JOIN users susers on susers.uid = snode.uid
WHERE susers.name = users.name
AND scp.field_shop_activated_value = '1'
AND scp.field_shop_suspended_value = '0'
)
ORDER BY cp.field_product_last_changed_value DESC
答案 0 :(得分:0)
在主表和连接表中添加索引将固定您的查询。
答案 1 :(得分:0)
以下步骤需要遵循以避免慢查询
vid
而非nid
distinct
和group by
。如果您使用它,您的查询有一些加入问题。 SELECT count(Distinct node.nid) as cnt
FROM node node
LEFT JOIN content_type_product cp ON cp.vid = node.vid
LEFT JOIN uc_products uc_products ON node.vid = uc_products.vid
LEFT JOIN users users ON users.uid = node.uid
LEFT JOIN files ON files.fid = cp.field_product_image_1_value
LEFT JOIN content_type_product_tags ct ON ct.field_product_product_id_value = cp.vid
WHERE
(
node.status <> 0
)
AND
(
ct.field_product_tag_id_value = 478
)
AND
(
cp.field_deleted_value <> 1
)
AND
(
cp.field_stock_level_value > 0
)
AND EXISTS
(
SELECT scp.nid FROM content_type_store scp
LEFT JOIN node snode on snode.nid = scp.nid
LEFT JOIN users susers on susers.uid = snode.uid
WHERE susers.name = users.name
AND scp.field_shop_activated_value = '1'
AND scp.field_shop_suspended_value = '0'
)
ORDER BY cp.field_product_last_changed_value DESC