mysql - 为什么这个 MySQL 查询性能很差(DEPENDENT_SUBQUERY)

标签 mysql performance mysql-dependent-subquery

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,这使得速度慢得要命。问题是为什么优化器会为 client 中的每个 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 |

最佳答案

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

关于mysql - 为什么这个 MySQL 查询性能很差(DEPENDENT_SUBQUERY),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36896094/

相关文章:

php - 如何防止 PHP 中的 SQL 注入(inject)?

SQL查询vBulletin中的新线程

.net - IronPython 性能

javascript - 服务器端处理还是客户端处理?

java - LWJGL 缓慢渲染

mysql 从表中检索和操作当前行和上一行

c# - 在C#中自动神奇地添加MySQL参数

php - 如何从 Laravel 中没有标签 "stdClass Object"的查询中获取结果?

MySQL - 我可以避免这些相关/依赖子查询吗?

mysql - 子查询减慢更新速度