如果speed
列不在這些表中唯一的(而且可能它不是,因爲你說你添加一個索引與speed
作爲國內領先的列...
如果t這裏有多個行的值爲speed
,在這些表中,那麼您的查詢可能會創建一箇中間集合。
讓我們來做一些簡單的數學。如果每個表中有兩行具有相同的速度值,則a和b之間的JOIN操作將爲該速度創建4行。當我們將連接添加到c時,還有另外兩行,這總共有8行。當我們把所有22個表連接起來,每個表都有兩行時,我們在2^22或超過400萬行。然後,需要在GROUP BY操作中處理所有具有speed
的所有相同值的整行行以消除重複項。
(當然,如果表中的任何一個不具有一排同樣speed
值,那麼查詢會產生零行爲speed
。)
就個人而言,我會溝老用於JOIN操作的-school逗號語法,並改爲使用JOIN關鍵字。我將WHERE子句中的連接謂詞移動到適當的ON子句中。
我也想讓其中一個表作爲所有連接的「驅動程序」,我會在每個連接中使用對同一個表的引用。 (我們知道,如果a=b
和b=c
,然後a=c
。但我不知道MySQL優化,無論任何區別,我們是否到位a=b and b=c
指定a=b and a=c
。
如果有不同的值相對較少的數量在每個表中有speed
,但是很多具有相同值的行,我會考慮使用內聯視圖爲每個表的每個速度獲取一行。MySQL可以使用合適的索引來優化GROUP BY在每個單獨的表上操作...我會選擇覆蓋索引...例如
ON wspeed20 (speed, pid, `or`)
ON wspeed24 (speed, pid, `or`)
Unfor可調節地,派生表(內聯視圖查詢的結果)未被索引,所以JOIN操作可能很昂貴(對於來自每個內聯視圖查詢的許多行)。
SELECT a.month,a.roadTypeID,a.speed,a.pid,a.or, b.pid, b.or, c.pid, c.or, d.pid, d.or,
e.pid, e.or, f.pid, f.or, g.pid, g.or, h.pid, h.or, i.pid, i.or, j.pid, j.or,
k.pid, k.or, l.pid, l.or, m.pid, m.or, n.pid, n.or, o.pid, o.or, p.pid, p.or,
q.pid, q.or, r.pid, r.or, s.pid, s.or, t.pid, t.or, u.pid, u.or, v.pid, v.or
FROM (SELECT speed, pid, `or` FROM wspeed2 GROUP BY speed) a
JOIN (SELECT speed, pid, `or` FROM wspeed3 GROUP BY speed) b ON b.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed20 GROUP BY speed) c ON c.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed24 GROUP BY speed) d ON d.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed25 GROUP BY speed) e ON e.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed26 GROUP BY speed) f ON f.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed27 GROUP BY speed) g ON g.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed63 GROUP BY speed) h ON h.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed65 GROUP BY speed) i ON i.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed68 GROUP BY speed) j ON j.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed69 GROUP BY speed) k ON k.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed70 GROUP BY speed) l ON l.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed71 GROUP BY speed) m ON m.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed72 GROUP BY speed) n ON n.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed73 GROUP BY speed) o ON o.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed74 GROUP BY speed) p ON p.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed75 GROUP BY speed) q ON q.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed76 GROUP BY speed) r ON r.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed77 GROUP BY speed) s ON s.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed78 GROUP BY speed) t ON t.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed81 GROUP BY speed) u ON u.speed = a.speed
JOIN (SELECT speed, pid, `or` FROM wspeed82 GROUP BY speed) v ON v.speed = a.speed
這不得不減少對需要連接的行數的潛力(同樣,如果有大量重複值的speed
,併爲speed
少數不同的值)。但是同樣,派生表之間的JOIN操作不會有任何可用的索引。 (至少,在MySQL版本中不能高達5.6)。
我來自Microsoft的後臺;但是,一般來說,當我有5個以上的表加入時,我會使用臨時表或其他臨時表。 – 2014-08-28 18:39:46
這是SQL數據庫的主要問題。有沒有更好的方式來做到這一點,我知道 – ControlAltDel 2014-08-28 18:40:33