0

所以有两个查询。为什么第一个比第二个运行得快得多?如何优化我的内部连接以与第一个连接一样快?

总记录超过 4 000 000

idTerminals 已编入索引


select (select name 
          from terminals 
          where terminals.idTerminals = AnalyzeProfitResults.idTerminals),
        currency, 
        count(*) 
from AnalyzeProfitResults
group by AnalyzeProfitResults.idTerminals,currency;


select name,
        currency, 
        count(*) 
from AnalyzeProfitResults
inner join terminals on terminals.idTerminals = AnalyzeProfitResults.idTerminals
group by AnalyzeProfitResults.idTerminals,currency;

    

结果是一样的:


name,currency,count(*)
"Buy or sell",EURUSD,235105
"Buy or sell",GBPUSD,14515
"Buy or sell",USDJPY,235298
"very strict",AUDJPY,234787
"very strict",AUDUSD,235298
"very strict",CHFJPY,235298
"very strict",EURJPY,235298
"very strict",EURUSD,235298
"very strict",GBPJPY,235298
"very strict",GBPUSD,235298
"very strict",NZDUSD,235298
"very strict",USDCHF,235298
"very strict",USDJPY,235298
",normal",AUDJPY,235298
",normal",AUDUSD,36391
",normal",EURUSD,234985
",normal",GBPJPY,117649
",normal",NZDUSD,198552
",normal",USDCHF,235298
",normal",USDJPY,235298
 
4

1 回答 1

1

我想在后一个查询中,MySQL 连接AnalyzeProfitResults并为操作terminals生成size(AnalyzeProfitResults) * size(terminals)group by。有 400 万行AnalyzeProfitResults,即使terminals只包含少量记录,也有很多行。在前一种情况下,MySQL 对 400 万行执行分组,产生您作为结果提供的 20 行,然后才对 20 行中的每一行执行子查询(甚至可能在内部重写为连接,这将是快点)。

编辑: 这是第一个带有显式连接的查询:

select terminals.name, APR_aggregated.*
from terminals join (
    select APR.currency, count(*)
    from AnalyzeProfitResults as APR
    group by APR.idTerminals, APR.currency
) as APR_aggregated on terminals.idTerminals = APR_aggregated.idTerminals
于 2012-07-21T17:50:09.133 回答