如何优化以下查询

时间:2012-08-27 06:30:13

标签: mysql sql optimization query-optimization

SELECT
    t1.theater_id,
    sum(t2.full_sale_price * t1.full_tickets + 
        t2.half_sale_price * t1.half_tickets) as Gross,
    sum(t2.full_cost_price * t1.full_tickets +
        t2.half_cost_price * t1.half_tickets) as Cost,
    Round(sum((t2.full_sale_price * t1.full_tickets + 
               t2.half_sale_price * t1.half_tickets) * t3.rate) / 100,
         2) as IPG,
         (sum(t2.full_sale_price * t1.full_tickets + 
              t2.half_sale_price * t1.half_tickets) 
              - Round(sum((t2.full_sale_price * t1.full_tickets + 
              t2.half_sale_price * t1.half_tickets) * (CASE
                WHEN ISNULL(t3.rate) THEN 0
                ELSE t3.rate
                END)) / 100,
            2)) as NetRev,
    CASE
        WHEN (t1.method = '2') THEN sum(0.00)
        ELSE 0
    END as sms,
    sum((CASE
        WHEN (t4.type = '1') THEN ((t1.full_tickets * 2) + (t1.half_tickets))
        ELSE (t1.full_tickets + t1.half_tickets)
    END) * 4.00) as maintenance,
    sum((((t2.full_sale_price * t1.full_tickets + 
           t2.half_sale_price * t1.half_tickets) 
           - (Round((t2.full_sale_price * t1.full_tickets + 
                     t2.half_sale_price * t1.half_tickets) * (CASE
                WHEN ISNULL(t3.rate) THEN 0
                ELSE t3.rate
            END) / 100,
            2))) - (t2.full_cost_price * t1.full_tickets + 
                    t2.half_cost_price * t1.half_tickets)) 
                    - (CASE
                       WHEN (t1.method = '2') THEN 0.00
                       ELSE 0
                       END) 
                    - (CASE
                       WHEN (t4.type = '1') THEN ((t1.full_tickets * 2) +
                                                  (t1.half_tickets))
                       ELSE (t1.full_tickets + t1.half_tickets)
    END) * 4.00) as admincost,
    sum(CASE
        WHEN (t4.type = '1') THEN ((t1.full_tickets * 2) + (t1.half_tickets))
        ELSE (t1.full_tickets + t1.half_tickets)
    END) as totTk
FROM
    `ratecard_rates` as t2 INNER JOIN
    `reservation` as t1 ON t1.movie_id = t2.movie_id
        AND t1.theater_id = t2.theater_id
        AND t1.showtime_id = t2.showtime_id
        AND t1.category_id = t2.category_id
        AND t1.bx_date = t2.date_apply
        LEFT JOIN
    `paymentgateway` as t3 ON t1.paymentgateway_id = t3.id
        LEFT JOIN
    `theatercategories` as t4 ON t1.category_id = t4.id
WHERE
    t1.status = '1' AND t1.method = '1'
        AND t1.reservation_type = '1'
        AND DATE(`tx_date`) BETWEEN DATE('2012-08-27') AND DATE('2012-08-27')
GROUP BY t1.theater_id

这是EXPLAIN EXPLAIN

2 个答案:

答案 0 :(得分:2)

"Using temporary; using filesort"通常意味着在查询计划的这一点上,MySsql决定将临时集写入磁盘,遗憾的是,它并不总是能够使用您可能放在相关列上的索引。

根据我的经验,解决这个问题的唯一方法是尝试减少中期结果的设置大小。尝试并隔离发生这种情况的点:例如:用SELECT替换整个COUNT(*)以消除CASE位等,看看是什么产生了MySql想要“缓存”的2779行在磁盘上。

有关详细信息,请参阅this answer

答案 1 :(得分:0)

您是盲目索引还是先检查了执行计划? CASE..END子句有很多帮助但有时可能会破坏很多执行计划。检查this link它可以帮助您理解我在说什么。一种选择是创建存储过程并在较小的查询上拆分此查询。有时子查询可能会有所帮助,这是尝试的另一种选择。