QUERY:MySQL的:很慢使用嵌套子查詢的查詢
SELECT
(
SELECT COUNT(cleanpoi.ID) FROM cleanpoi
WHERE cleanpoi.EstablishmentID=parent.ID
) AS POIs,
(
SELECT COUNT(ID) FROM cleanamenitymappings WHERE CleanPOIID IN
(
SELECT cleanpoi.ID FROM cleanpoi
WHERE cleanpoi.EstablishmentID=parent.ID
)
) AS Amenities,
(
SELECT COUNT(ID) FROM cleanamenityvalues WHERE CleanPOIID IN
(
SELECT cleanpoi.ID FROM cleanpoi
WHERE cleanpoi.EstablishmentID=parent.ID
)
) AS AmenityValues
FROM establishment parent
WHERE parent.ID=3
EXPLAIN結果:
id select_type table type possible_keys key key_len ref rows Extra
------ ------------------ -------------------- --------------- ----------------------------------------- --------------- ------- ------ ------ --------------------------
1 PRIMARY parent const PRIMARY PRIMARY 4 const 1 Using index
5 DEPENDENT SUBQUERY cleanamenityvalues ALL (NULL) (NULL) (NULL) (NULL) 31778 Using where
6 DEPENDENT SUBQUERY cleanpoi unique_subquery PRIMARY,EstablishmentID_2,EstablishmentID PRIMARY 4 func 1 Using where
3 DEPENDENT SUBQUERY cleanamenitymappings index (NULL) CleanPOIID 4 (NULL) 673591 Using where; Using index
4 DEPENDENT SUBQUERY cleanpoi unique_subquery PRIMARY,EstablishmentID_2,EstablishmentID PRIMARY 4 func 1 Using where
2 DEPENDENT SUBQUERY cleanpoi ref EstablishmentID_2,EstablishmentID EstablishmentID 4 const 181
與此查詢有何建議?你的答案真的很感謝!
爲了提高性能,刪除嵌套查詢或增加你的'innodb_buffer_pool_size' – 2013-03-21 06:44:01
爲什麼你發佈一個答案,一個評論? – 2013-03-21 06:45:55