Mysql总和多了一个表

时间:2017-01-11 22:13:44

标签: mysql

我有第一张带有一些统计数据的大表,第二张需求表和第三张供应表。

我必须显示统计数据。有时我需要使用一些参数(demand_tag_id,supply_tag_id,domain_id等)对其进行分组。

统计信息表中有1000万行。

这是我的SQL请求;

SELECT 
s.requests,s.impression,s.bad_requests,s.empty_body,s.bad_vast,s.exceptions,s.midpoint,s.complete,s.click,
    (s.impression / s.requests) * 100 as req_fill,
    (s.click / s.impression) * 100 as ctr,(s.complete / s.impression) * 100 as completion,
    s.media_cost,s.revenue 
    FROM ( 
        SELECT SUM(t.requests) AS requests, SUM(t.impression) AS impression, SUM(t.bad_requests) AS bad_requests, SUM(t.empty_body) AS empty_body, SUM(t.bad_vast) AS bad_vast, SUM(t.exceptions) AS exceptions, SUM(t.midpoint) AS midpoint, SUM(t.complete) AS complete, SUM(t.click) AS click, 
        SUM(IF(p.rate_type = "0", (t.impression * p.fixed_rate) / 1000, (t.impression * d.rate) / 1000 * (p.percent_rate / 100))) as media_cost, 
        SUM((t.impression * d.rate) / 1000) as revenue 
        FROM 7days_Stats t 
        JOIN Demands d ON d.id = t.demand_tag_id 
        JOIN Supplies p ON p.id = t.supply_tag_id
    ) s

此请求需要38秒。

但如果我从此请求中移除JOIN,收入和media_cost,则需要11秒。

如何更快地完成此请求?请帮帮我!

0 个答案:

没有答案