复杂的SQL查询重载服务器 - 如何简化

时间:2013-10-09 04:27:58

标签: mysql sql nested-queries

我有一个复杂的查询,它会一直执行:

SELECT tr.`Fund Name`,
       tr.`Fund ID`
FROM
  (SELECT `Fund Name`,
          `Fund ID`
   FROM (
           (SELECT *
            FROM `trendmetrics`)
         UNION
           (SELECT *
            FROM `trendmetrics_custom`
            WHERE `user_id`=361)) trendmetrics
   WHERE `FactorName`='MSCI in $'
     AND trendmetrics.`Annualmean 2`>0.061
     AND trendmetrics.`VaR 95 2`>-0.04
     AND `Fund ID` IN
       (SELECT `Fund ID`
        FROM (
                (SELECT *
                 FROM `trendmetrics`)
              UNION
                (SELECT *
                 FROM `trendmetrics_custom`
                 WHERE `user_id`=361)) trendmetrics
        WHERE `FactorName`='Oil (Spot West Texas)'
          AND trendmetrics.`Annualmean 1`>0.061
          AND trendmetrics.`VaR 95 1`>-0.04)
     AND `Fund ID` IN
       (SELECT `Fund ID`
        FROM (
                (SELECT *
                 FROM `trendmetrics`)
              UNION
                (SELECT *
                 FROM `trendmetrics_custom`
                 WHERE `user_id`=361)) trendmetrics
        WHERE `FactorName`='Credit Spread (BAA-AAA Moodys)'
          AND trendmetrics.`Annualmean 1`>0.061
          AND trendmetrics.`VaR 95 1`>-0.04)) tr
INNER JOIN (
              (SELECT *
               FROM `quant1`)
            UNION
              (SELECT *
               FROM `quant1_cust`
               WHERE `user_id`=361)) quant1 ON (`tr`.`Fund ID` = `quant1`.`Fund ID`)

在我将一个简单的表trendmetrics更改为两个表的并集后,问题就出现了:

((SELECT * FROM `trendmetrics`) UNION (SELECT * FROM `trendmetrics_custom` WHERE `user_id`=$user_id)) trendmetrics 

什么阻止它执行?我没有任何错误,但它一直在运行,直到我中止它的执行。

更新:EXPLAIN报告: enter image description here

0 个答案:

没有答案