查询花了很多时间如何解决性能问题

时间:2014-12-08 13:12:18

标签: mysql sql mysqli

查询至少需要20秒 我计划创造意见 但如果有子查询,我们就无法创建视图

请提供一些有关创建视图的建议

欢迎任何其他建议

SELECT 
    CAST(t.timecreated AS DATE) date,
    date_format(timecreated, '%d-%b-%Y'),
    SUM(t.enquiry_cnt) enquiry_cnt,
    (SELECT 
            COUNT(DISTINCT lead_id)
        FROM
            mg_lead_suggested_listing
        WHERE
            CAST(timecreated AS DATE) = CAST(t.timecreated AS DATE)
                AND source = 1) as lead_enquiry_cnt,
    SUM(t.suggested_cnt) suggested_cnt,
    (SELECT 
            COUNT(DISTINCT lead_id)
        FROM
            mg_lead_suggested_listing
        WHERE
            CAST(timecreated AS DATE) = CAST(t.timecreated AS DATE)
                AND source = 6) as lead_suggested_cnt,
    SUM(t.tot_cnt) tot_cnt,
    (SELECT 
            COUNT(DISTINCT lead_id)
        FROM
            mg_lead_suggested_listing
        WHERE
            CAST(timecreated AS DATE) = CAST(t.timecreated AS DATE)
                AND (source = 1 or source = 6)) AS lead_cnt,
    SUM(t.dropped_cnt) droped_cnt,
    (SELECT 
            COUNT(DISTINCT lead_id)
        FROM
            mg_lead_suggested_listing
        WHERE
            CAST(timecreated AS DATE) = CAST(t.timecreated AS DATE)
                AND (source = 1 || source = 6)
                && directory_status = 4) as lead_droped_cnt,
    sum(t.visit_pending_cnt) visit_pending_cnt,
    (SELECT 
            COUNT(DISTINCT lead_id)
        FROM
            mg_lead_suggested_listing
        WHERE
            CAST(timecreated AS DATE) = CAST(t.timecreated AS DATE)
                AND (directory_status = 3 && finl_status = 6)) as lead_visit_pending_cnt
FROM
    (SELECT 
        CASE
                WHEN source = 1 THEN 1
                ELSE 0
            END enquiry_cnt,
            CASE
                WHEN source = 6 THEN 1
                ELSE 0
            END suggested_cnt,
            CASE
                WHEN (source = 1 OR source = 6) THEN 1
                ELSE 0
            END tot_cnt,
            case
                when
                    ((source = 1 || source = 6)
                        && directory_status = 4)
                then
                    1
                else 0
            end dropped_cnt,
            case
                when (directory_status = 3 && finl_status = 6) then 1
                else 0
            end visit_pending_cnt,
            lead_id,
            timecreated
    FROM
        mg_lead_suggested_listing) AS t
group by CAST(t.timecreated AS DATE)
order by date desc
limit 10

仅供参考:此查询中没有联接       我想只计算一个有一些条件的表 提前致谢

1 个答案:

答案 0 :(得分:0)

您应该能够使用条件聚合执行您想要的操作。查询看起来像这样:

SELECT CAST(t.timecreated AS DATE) as date, date_format(timecreated, '%d-%b-%Y'),
       SUM(t.enquiry_cnt) as enquiry_cnt,
       COUNT(DISTINCT CASE WHEN source = 1 THEN lead_id END) as lead_enquiry_cnt,
       SUM(t.suggested_cnt) suggested_cnt,
       COUNT(DSTINCT CASE WHEN source = 6 THEN lead_id END) as lead_suggested_cnt,
       SUM(t.tot_cnt) as tot_cnt,
       COUNT(DISTINCT CASE WHEN source IN (1, 6) THEN lead_id END) as lead_suggested_cnt,
       SUM(t.dropped_cnt) as droped_cnt,
       COUNT(DISTINCT CASE WHEN source IN (1, 6) and Directory_status = 4 THEN lead_id END) as lead_droped_cnt,
       sum(t.visit_pending_cnt) visit_pending_cnt,
       COUNT(DISTINCT CASE WHEN Directory_status = 3 AND finl_status = 6 THEN lead_id END) as lead_visit_pending_cnt
FROM (SELECT (source = 1) as enquiry_cnt,
             (source = 6) as suggested_cnt,
             (source = 1 OR source = 6) as tot_cnt,
             (source IN (1, 6) AND directory_status = 4) as dropped_cnt,
             (directory_status = 3 AND finl_status = 6) as visit_pending_cnt,
            lead_id,
            timecreated
     FROM mg_lead_suggested_listing
    ) AS t
group by CAST(t.timecreated AS DATE)
order by date desc
limit 10;

删除子查询并在日期逻辑上使用where子句可能也会有所帮助。