我有一個主表,它有詳細信息。自我加入一個巨大的桌子,條件是需要花費很多時間,優化查詢
我想在該會話中找到該會話中產品的所有組合,並在該會話中查找該特定會話中的所有其他產品。
create table combinations as
select
a.main_id,
a.sub_id as sub_id_x,
b.sub_id as sub_id_y,
count(*) as count1,
a.dates as rundate
from
master_table a
left join
master_table b
on a.session_id = b.session_id
and a.visit_number = b.visit_number
and a.main_id = b.main_id
and a.sub_id != b.sub_id
where
a.sub_id is not null
and b.sub_id is not null
group by
a.main_id,
a.sub_id,
b.sub_id,
rundate;
我做了查詢
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| 1 | SIMPLE | a | NULL | ALL | NULL | NULL | NULL | NULL | 298148 | 90.00 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | b | NULL | ALL | NULL | NULL | NULL | NULL | 298148 | 0.08 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
主要的問題是一個解釋,我的主表由80萬行。此查詢需要超過24小時才能執行。
所有列都被編入索引,我正在進行自聯接。
會先創建一個類似的表'master_table_2'
,然後進行連接會使我的查詢更快?
有沒有什麼辦法來優化查詢時間?
你試過在這個查詢上運行'EXPLAIN'嗎?它告訴你什麼,這些指標真的被使用? –
@TimBiegeleisen檢查編輯 – Shubham