2012-06-11 47 views
-2

錯誤的搜索查詢正在導致我們的網站崩潰。編寫計數每個子類別的人,然後將它們加在一起以獲得主要類別的總數。 對於分頁接下來,最後,頁面,然後用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 
+2

我建議從一張紙上的流程圖​​開始。首先寫下你需要在頁面上顯示的數據。 然後寫下需要採取的相關表格。 然後製作流程圖,以便了解在此基礎上採取何種措施並隨後生成查詢。 當你採取這種方法時,你可以做一個複雜的查詢,而不會失去跟蹤什麼。 – Tschallacka

+2

如果您正確格式化查詢以便更易讀,您將更容易獲得幫助。 – hannebaumsaway

+0

作爲一點額外的提示,你的'order by'似乎是無用的;您正在使用分析功能。 – Ben

回答

0

添加在主表的索引和連接表,可加快查詢。

0

這裏是下面的步驟需要遵循,以避免慢查詢從節點,而不是從內容表開始

  1. 第一次啓動。
  2. 對於內容表連接與vid代替nid
  3. 無需在查詢中使用distinctgroup 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