Mysql子查询好慢

问题描述 投票:0回答:1

这是我的主查询中的查询。

SELECT 
count(IF(source='L',1,NULL)) as L,  
count(IF(source='B',1,NULL)) as B, 
count(IF(source='M',1,NULL)) as M 
FROM table_name WHERE tax_idno=main_query_id

除此之外还有更快的子查询吗?请帮忙,谢谢。

php mysql performance subquery countif
1个回答
0
投票

您可以使用 sum 函数代替 count 来优化查询:

SELECT 
    SUM(source='L') as L,  
    SUM(source='B') as B, 
    SUM(source='M') as M 
FROM table_name 
WHERE tax_idno=main_query_id;

并确保您对“source”和“tax_idno”有正确的索引

© www.soinside.com 2019 - 2024. All rights reserved.