一旦表被完全填充,我很關心下面的查詢的性能。到目前爲止,它正在開發中,並且與虛擬數據很好地配合。MySQL在連接選擇查詢中的500萬行表
表「adress_zoo」一旦完全填充後將包含大約5億條記錄。 「adress_zoo」表格如下所示:
CREATE TABLE `adress_zoo`
(`adress_id` int(11) NOT NULL, `zoo_id` int(11) NOT NULL,
UNIQUE KEY `pk` (`adress_id`,`zoo_id`),
KEY `adress_id` (`adress_id`))
ENGINE=InnoDB DEFAULT CHARSET=latin1;
其他表格每個最多包含500條記錄。
完整的查詢看起來是這樣的:
SELECT a.* FROM jos_zoo_item AS a
JOIN jos_zoo_search_index AS zsi2 ON zsi2.item_id = a.id
WHERE a.id IN (
SELECT r.id FROM (
SELECT zi.id AS id, Max(zi.priority) as prio
FROM jos_zoo_item AS zi
JOIN jos_zoo_search_index AS zsi ON zsi.item_id = zi.id
LEFT JOIN jos_zoo_tag AS zt ON zt.item_id = zi.id
JOIN jos_zoo_category_item AS zci ON zci.item_id = zi.id
**JOIN adress_zoo AS az ON az.zoo_id = zi.id**
WHERE 1=1
AND ((zci.category_id != 0 AND (zt.name != 'prolong' OR zt.name is NULL))
OR (zci.category_id = 0 AND zt.name = 'prolong'))
AND zi.type = 'telefoni'
AND zsi.element_id = '44d3b1fd-40f6-4fd7-9444-7e11643e2cef'
AND zsi.value = 'Small'
AND zci.category_id > 15
**AND az.adress_id = 5**
GROUP BY zci.category_id) AS r
)
AND a.application_id = 6
AND a.access IN (1,1)
AND a.state = 1
AND (a.publish_up = '0000-00-00 00:00:00' OR a.publish_up <= '2012-06-07 07:51:26')
AND (a.publish_down = '0000-00-00 00:00:00' OR a.publish_down >= '2012-06-07 07:51:26')
AND zsi2.element_id = '1c3cd26e-666d-4f8f-a465-b74fffb4cb14'
GROUP BY a.id
ORDER BY zsi2.value ASC
查詢通常會返回25條記錄。
根據你的經驗,這個查詢是否可以接受(在3秒內響應)? 我可以做些什麼來優化這個?
正如勸通過@Jack我跑瞭解釋查詢並得到這個:
偏題:這個問題在下次有人問:「MySQL會在我的龐大的100k記錄表中表現不錯嗎?」時,這個問題將成爲一個很好的參考資料。 – lanzz