2016-04-27 134 views
0
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中,认为这将是一个简单的“字符串”,以避免扫描的结果。为什么这个MySQL查询性能很差(DEPENDENT_SUBQUERY)

我认为这不会是一个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 | 
+1

我更新了问题的扩展结果 – Miguel

+0

'id IN ... GROUP_CONCAT(...)' - 你确定你想要吗? 123 IN('123,456,789')'不会成功。这样做:'123 IN('123','456','789')',但这不是你有的,也不能得到它。重来。 –

+0

因为这只是数字我相信你可以选择退出引号...... – Miguel

回答

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 
) 
+0

该表有3个记录,并且这些字段已被索引。它真的是子查询弄乱了,我用连接解决了..但那是错误的,mysql应该接受这样的逻辑查询。它不是我的错,优化引擎真正优化错误。 – Miguel

+0

然后在查询中回答,请注意,group_concat已经在返回一个昏迷ID列表,所以你不能使用t1.list = t2.id,因为这样会匹配一个完整的字符串和id在t2.id表中的id 。在那个连接中,必须删除group_concat才能使其工作。但事情是,我想要做的查询看起来非常符合逻辑,悲伤的mysql无法处理它。 – Miguel

+0

有斑点re group_concat。我完全同意你关于子查询,但我们坚持下去! – Datadimension