我從這個問題掙扎(再次)與表:how to optimize this query (4 mm tables involved)優化COUNT(*)查詢與4間MM的關係,更大的表
這是一個主表product_table
已經經由四個MM的關係查找表mm1
到mm4
。查找表的字段爲uid_local
,其中包含product_table
和uid_foreign
的uid,其中包含結果被過濾的類別的uid。
從提高查詢看起來像這樣性能的上述問題Quassnois建議之後:
SELECT
COUNT(*)
FROM
product_table
WHERE
(product_table.partner_id = 0 OR product_table.partner_id = 15) AND
EXISTS(SELECT NULL FROM mm1 WHERE mm1.uid_local = product_table.uid AND mm1.uid_foreign IN (10, 11, 12, 13, 14, 53)) AND
EXISTS(SELECT NULL FROM mm2 WHERE mm2.uid_local = product_table.uid AND mm2.uid_foreign IN (7, 8, 9)) AND
EXISTS(SELECT NULL FROM mm3 WHERE mm3.uid_local = product_table.uid AND mm3.uid_foreign IN (51 ,52)) AND
EXISTS(SELECT NULL FROM mm4 WHERE mm4.uid_local = product_table.uid AND mm4.uid_foreign IN (15, 16, 17, 18, 19))
這返回稍微圍繞10.000行作爲COUNT(*)
但它需要>0.5秒用於查詢來執行。這太慢了。
EXPLAIN
看起來是這樣的:
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY product_table ref NewIndex NewIndex 4 const 9430 Using where
5 DEPENDENT SUBQUERY mm1 ref uid_local,uid_foreign uid_local 4 mm1.uid 5 Using where
4 DEPENDENT SUBQUERY mm2 ref uid_local,uid_foreign uid_local 4 mm2.uid 2 Using where
3 DEPENDENT SUBQUERY mm3 ref uid_local,uid_foreign uid_local 4 mm3.uid 3 Using where
2 DEPENDENT SUBQUERY mm4 ref uid_local,uid_foreign uid_local 4 mm4.uid 6 Using where
如果我改變product_table.partner_id = 0
的東西,返回更少的行即G。幾百個左右的查詢速度非常快(0.015秒)。
所以問題是,COUNT(*)
查詢是非常快,如果它只需要計數幾行(100 - 200),但計數較大的結果集(> 10.000行)非常慢。
有關如何解決此問題的任何想法?
幾個事實:
- 的毫米表對
uid_local
索引和uid_foreign
- 的
product_table
有一個索引PK和存在的product_table
表包含圍繞partner_id
- 另一個指標30.000行
編輯
的IN
條款是不固定的,查詢可以代替IN (7, 8, 9)
樣子IN (7, 9)
或數字的其他組合。
什麼是物化視圖?查詢是否仍然可以接受參數?用戶通過Web前端選擇IN子句的值。 – Max 2009-10-20 17:46:04
從你的例子中,我收集到IN子句已修復。如果他們可以改變,那麼這是行不通的。 – 2009-10-21 08:15:07
物化視圖是複製到動態表中的視圖。當您更改基礎表中的行時,該表的內容將會改變。 – 2009-10-21 08:16:33