【问题标题】:Why is this MySQL query poor performance (DEPENDENT_SUBQUERY)为什么这个 MySQL 查询性能不佳(DEPENDENT_SUBQUERY)
【发布时间】:2023-03-23 22:57:01
【问题描述】:
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 |

【问题讨论】:

  • 我用解释结果更新了问题
  • id IN ... GROUP_CONCAT(...) -- 你确定要那个吗? 123 IN ('123,456,789') 成功。确实如此:123 IN ('123','456','789'),但这不是你所拥有的,你也不能得到它。重新开始。
  • 因为这只是数字,我相信您可以选择退出引号...
  • 但是GROUP_CONCAT 会生成一个字符串。需要的是几个数字。它不会产生这种情况,除非您在代码或存储过程中构造 IN 子句。

标签: mysql performance mysql-dependent-subquery


【解决方案1】:

如果没有数据,显然很难测试,但如下所示。 子查询在 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
)

【讨论】:

  • 该表有 3k 条记录,并且这些字段已经被索引。它确实是搞砸的子查询,我确实通过连接解决了.. 但那是错误的,mysql 应该接受这样的逻辑查询。优化引擎真的优化错了不是我的错。
  • 在你回答的查询中,提醒 group_concat 已经返回一个逗号 id 列表,所以你不能使用 t1.list=t2.id 因为这会匹配一个完整的字符串和一个 ids t2.id 表中的 id。在该连接中,必须删除 group_concat 才能使其工作。但问题是我想做的查询对我来说看起来很合乎逻辑,悲伤的 mysql 无法处理它。
  • 很好发现 re group_concat。我完全同意你关于子查询的看法,但我们坚持下去!!!
猜你喜欢
  • 2012-03-20
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 2012-02-18
  • 2011-05-04
  • 1970-01-01
  • 2014-06-04
  • 2012-06-13
相关资源
最近更新 更多