連接時速度很慢,我們有兩個表:MySQL在複合鍵
SELECT B.x
FROM B
INNER JOIN A
ON A.b = B.b
AND A.c = B.c
WHERE A.a IN (...a values...);
a values
從客戶端語言給(我們的情況:紅寶石)
create table A (
id int,
a int,
b int,
c varchar(255),
unique key K1 (a,c),
key K2 (b,c),
key K3 (a,b,c)
);
create table B (
id int,
b int,
c varchar(255),
x varchar(255),
unique key K (b,c)
);
像運行查詢,以及有關10-100,000個項目。
解釋是這樣的。
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: A
type: range
key: K
key_len: 4
ref: NULL
rows: 100
Extra: Using where; Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: app_devices
type: ref
key: K
key_len: 4
ref: A.b
rows: 213
Extra: Using index condition
2 rows in set (0.00 sec)
當A和B很小時,這個查詢很好用,但當表大小超過2000萬行時,這個查詢變得很慢。 我懷疑組合鍵在加入時工作不正常。我該如何解決這個問題?
你應該閱讀MySQL索引的[** ** TIPS(http://mysql.rjweb.org/doc.php/index_cookbook_mysql) –
你的意思是'WHERE的AA(一...值...);'可以是100K的值?這對於索引搜索來說似乎並不理想。就像你加入'表A'和'Avalues'一樣,但'Avalues'沒有索引。你也沒有A'(b,c)'的合成索引,所以你的連接'ON'沒有使用正確的索引 –
'(... a values ...)'實際上就是'(12,16,295,1053) '。 '(b,c)'存在,但我省略了,因爲優化器沒有使用它,對不起。我已編輯添加所有索引。 – mtomita