繁体   English   中英

SQL查询请求需要很长时间与内部联接进行特殊选择

[英]Sql query request taking long time with inner join a special select

我应该执行一个仅使用一个表的简单查询。

执行结果不是假的-但是要花很长时间。

SELECT SUM(montantCommande) as total 
    FROM export_commandes 
    INNER JOIN (SELECT ID FROM export_adherent) AS table_adherent
    WHERE export_commandes.client_id=table_adherent.ID

这样简化

SELECT SUM(montantCommande) as total 
FROM export_commandes 
INNER JOIN export_adherent AS table_adherent on export_commandes.client_id=table_adherent.ID

这是您的查询:

SELECT SUM(montantCommande) as total 
FROM export_commandes c INNER JOIN
     (SELECT ID FROM export_adherent) a
     ON c.client_id = a.ID;

当您在MySQL中使用子查询(而不是在大多数其他数据库中)时,它实际上是在创建派生表。 这不仅增加了额外的开销,而且还干扰了优化索引。

相反,将查询的短语设置为:

SELECT SUM(montantCommande) as total 
FROM export_commandes c INNER JOIN
     export_adherent a
     ON c.client_id = a.ID;

然后,您需要在export_commands(client_id)export_adherent(id)export_commands(client_id)索引(仅使用一个,但尚不清楚)。 您可以在适当的索引中将montantCommande添加为第二个键,以进行进一步的优化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM