explain select id, nome from bea_clientes where id in (
select group_concat(distinct(bea_clientes_id)) as list
from bea_agenda
where bea_clientes_id>0
and bea_agente_id in(300006,300007,300008,300009,300010,300011,300012,300013,300014,300018,300019,300020,300021,300022)
)
当我尝试执行上述操作(没有解释)时,MySQL只是忙着使用DEPENDENT SUBQUERY,这使得这很慢。这就是优化器为客户端中的每个id计算子查询的原因。我甚至将IN参数放在group_concat中,相信将该结果作为一个简单的“字符串”来避免扫描是相同的。
我认为对于5.5+的MySQL服务器来说这不是问题吗? MariaDb中的测试也是如此。
这是一个已知的错误吗?我知道我可以将其重写为连接,但这仍然很糟糕。
Generated by: phpMyAdmin 4.4.14 / MySQL 5.6.26
Comando SQL: explain select id, nome from bea_clientes where id in ( select group_concat(distinct(bea_clientes_id)) as list from bea_agenda where bea_clientes_id>0 and bea_agente_id in(300006,300007,300008,300009,300010,300011,300012,300013,300014,300018,300019,300020,300021,300022) );
Lines: 2
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|--------------------|--------------|-------|-------------------------------|---------------|---------|------|-------|------------------------------------|
| 1 | PRIMARY | bea_clientes | ALL | NULL | NULL | NULL | NULL | 30432 | Using where |
| 2 | DEPENDENT SUBQUERY | bea_agenda | range | bea_clientes_id,bea_agente_id | bea_agente_id | 5 | NULL | 2352 | Using index condition; Using where |
答案 0 :(得分:0)
显然很难在没有数据的情况下进行测试,但如下所示。 子查询在mysql中并不好(虽然它是我的首选引擎)。 我还建议索引相关列,这将提高两个查询的性能。 为清楚起见,我还建议扩展查询。
select t1.id,t1.nome from (
(select group_concat(distinct(bea_clientes_id)) as list from bea_agenda where bea_clientes_id>0 and bea_agente_id in (300006,300007,300008,300009,300010,300011,300012,300013,300014,300018,300019,300020,300021,300022)
) as t1
join
(select id, nome from bea_clientes) as t2
on t1.list=t2.id
)