两个查询分别快速,而作为子查询联接时则较慢

问题描述

| 我有两个查询,每个查询都运行得非常快(不到2秒)。但是,当我尝试将它们作为子查询加入时,它运行起来非常缓慢。我上次跑步大约花了68秒。这是完整的查询:
SELECT t.count,t.total
  FROM (SELECT t.account_number,COUNT(t.id) count,SUM(t.amount) total,ib.id import_bundle_id
          FROM import_bundle ib
          JOIN generic_import gi ON gi.import_bundle_id = ib.id
          JOIN transaction_import ti ON ti.generic_import_id = gi.id
          JOIN account_transaction t ON t.transaction_import_id = ti.id
          JOIN transaction_code tc ON t.transaction_code_id = tc.id
         WHERE tc.code IN (0,20,40)
      GROUP BY t.account_number) t
  JOIN (SELECT a.account_number,np.code
          FROM import_bundle ib
          JOIN generic_import gi ON gi.import_bundle_id = ib.id
          JOIN account_import ai ON ai.generic_import_id = gi.id
          JOIN account a ON a.account_import_id = ai.id
          JOIN account_northway_product anp ON anp.account_id = a.id
          JOIN northway_product np ON anp.northway_product_id = np.id
         WHERE np.code != \'O1\') a ON t.account_number = a.account_number
该查询应缓慢运行并不令人意外。如果这些是两个单独的表而不是子查询,则将索引放在它们的“ 1”列上。但是,显然不可能在查询结果上放置索引,所以我不能这样做。我怀疑这是问题的一部分。 除此之外,除了添加两个汇总表(我不愿意这样做)之外,我不理解查询为什么会变慢并且对如何加快查询没有任何想法不必。 顺便说一句,此查询的英文内容可能是“为不是透支保护帐户的帐户(代码O1)获取所有POS交易(代码0、20和40)。”

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)