连接时速度很慢,我们有两个表: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