我有两张桌子:
第一个table1是动态创建的(当用户从Web服务器提交数据时),通常是~50K行.第二个表是查找表table2,有~10Mil行.
我正在尝试将两个表连接到四列,如下所示:
SELECT t.id FROM table1 t
JOIN table2 m
ON (t.name = m.name AND t.pos = m.pos AND t.ref = m.ref AND t.alt = m.alt);
我已经将table2中的列名称(VARCHAR),pos(INT),ref(CHAR)和alt(CHAR)编入索引,但查询仍然需要很长时间才能完成.
关于这里可能出现什么问题的任何指示?
谢谢
EXPLAIN的输出:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 NULL ALL NULL NULL NULL NULL 49329 100.00 Using where
1 SIMPLE t2 NULL ref table2_name,table2_pos,table2_ref,table2_alt table2_name 32 my_db.t1.NAME 2488 0.00 Using index condition; Using where
解决方法:
在name,pos,ref,alt上创建复合索引
喜欢
INDEX theIndex (name,pos,ref, alt)
标签:sql,mysql,join,database
来源: https://codeday.me/bug/20190722/1503254.html