【发布时间】:2018-05-11 05:39:34
【问题描述】:
我正在尝试优化以下查询,该查询大约需要 1.5 秒才能执行。
SELECT id
FROM leads
left join leads_cstm ON leads.id = leads_cstm.id_c
WHERE deleted=0
and cust_temp_id_c = 'xxxx';
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: leads_cstm
partitions: NULL
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 696334
filtered: 10.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: leads
partitions: NULL
type: eq_ref
possible_keys: PRIMARY,idx_del_user,idx_leads_id_del
key: PRIMARY
key_len: 108
ref: crmsuite.leads_cstm.id_c
rows: 1
filtered: 50.00
Extra: Using where
2 rows in set, 1 warning (0.00 sec)
我尝试在leads_cstm(id_c,cust_temp_id_c) 上创建索引,但没有成功!!我也尝试使用 direct_join 并降低了成本,但查询现在需要更多时间来执行(3 秒)。
mysql> EXPLAIN SELECT id
FROM leads
STRAIGHT_JOIN leads_cstm ON leads.id = leads_cstm.id_c
WHERE deleted=0
and cust_temp_id_c = 'xxxxx';
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: leads
partitions: NULL
type: ref
possible_keys: PRIMARY,idx_del_user,idx_leads_id_del
key: idx_del_user
key_len: 2
ref: const
rows: 375820
filtered: 100.00
Extra: Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: leads_cstm
partitions: NULL
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 108
ref: crmsuite.leads.id
rows: 1
filtered: 10.00
Extra: Using where
2 rows in set, 1 warning (0.00 sec)
请说明为什么使用直接连接需要更多时间以及如何优化此查询。
两个表都包含大约 750000 行。
【问题讨论】:
-
请修正您的格式。
-
完成蒂姆........
-
deleted和cust_temp_id_c在哪些表?没有这些信息就无法帮助优化。 -
LEFT JOIN而不是JOIN的某些原因?
标签: mysql database indexing query-optimization database-administration