我正在使用MySQL查询在四个表上执行三个表连接 . 其中两个连接在一对MUL键上 . 第三个连接位于一对主键上 . 查询需要一分钟才能获得100行 .
我相信两对MUL键都是独一无二的;所以我想知道,如果这些密钥被索引为唯一的密钥,或者做成外键,我会注意到显着的性能提升吗?
MUL键上的连接可能是罪魁祸首吗?
Edit
好的,这是架构 . 我用字母代替实际的表/列名称 .
mysql> describe table A;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| pkey | int(10) unsigned | NO | PRI | NULL | auto_increment |
| mkey | int(10) unsigned | NO | MUL | NULL | |
| a | int(10) unsigned | NO | | NULL | |
| b | int(10) unsigned | NO | | NULL | |
mysql> describe table B;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| pkey | int(10) unsigned | NO | PRI | NULL | auto_increment |
| mkey1 | int(10) unsigned | NO | MUL | NULL | |
| mkey2 | int(10) unsigned | NO | MUL | NULL | |
| a | int(10) unsigned | NO | | NULL | |
| b | int(10) unsigned | NO | | NULL | |
| c | int(10) unsigned | NO | | NULL | |
mysql> describe table C;
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| pkey | int(10) unsigned | NO | PRI | NULL | |
| mkey | varchar(128) | NO | MUL | NULL | |
mysql> describe table D;
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| pkey | int(10) unsigned | NO | PRI | NULL | |
| mkey | varchar(128) | NO | MUL | NULL | |
查询:
select
A.a, A.b, B.c, A.mkey, C.pkey, D.mkey
from
A, B, C, D
where
A.pkey=C.pkey and
A.mkey=B.mkey1 and
B.mkey2=D.pkey and
B.a <= A.a and
B.b >= A.b
D.mkey in ('str1', 'str2', ...);
返回77行 .