我有兩個表A和B.A有大約2900萬行,而B是有大約1000行的臨時表。mysql選擇連接速度很慢
查詢是如下 -
select DISTINCT Table_A.column_a from Table_B join Table_A on Table_B.ID_b = Table_A.ID_a;
我對(ID_A,column_a)
查詢大約需要20秒來運行綜合指數KEY2。
的解釋是如下 -
------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+------+-----------------------------------------+---------------------------+---------+---------------------------------------+------+-----------------+
| 1 | SIMPLE | Table_B | ALL | NULL | NULL | NULL | NULL | 1507 | Using temporary |
| 1 | SIMPLE | Table_A | ref | key1 ,key2 | key2 | 3 | DB_name.Table_B.ID_b | 963 | Using index |
+----+-------------+-------------------+------+-----------------------------------------+---------------------------+---------+---------------------------------------+------+-----------------+
如何優化這個查詢?
感謝
desc Table_A
| id | bigint(8) unsigned | NO | PRI | NULL | auto_increment |
| column_a | mediumint(8) unsigned | NO | MUL | | |
| column_1 | mediumint(8) unsigned | NO | MUL | | |
| id_a | mediumint(8) unsigned | NO | MUL | |
| column_2 | smallint(5) unsigned | NO | MUL | NULL | |
| column_3 | smallint(5) unsigned | NO | | NULL | |
| column_4 | smallint(5) unsigned | NO | MUL | NULL | |
| column_5 | smallint(5) unsigned | NO | | NULL | |
讓表索引通過ID? – Luixv 2011-05-10 07:40:06
你在Table_B.ID_b上有索引嗎? – 2011-05-10 07:40:52
請發表您的表格結構,tkx – Neo 2011-05-10 08:00:39