在这里,我正在使用mysql查询来获取数据,但是即使所有列都已建立索引,也要花10秒20秒左右的时间。所以我是优化的新手,请帮助我
SELECT a.cname,
(SELECT status_name
FROM persontype_status
WHERE status = a.p3_status
LIMIT 1) AS
company_status_psp3,
a.curl,
a.cid,
a.pid,
a.addedbyuser,
a.no_of_emp,
( CASE
WHEN a.lead_status > 0
AND ( a.status = 0
OR a.status > 0 ) THEN (SELECT ( Max(Date(dateadded)) )
FROM d3_info
WHERE cid = a.cid
GROUP BY cid)
WHEN ( a.status > 0
AND a.lead_status = 0 ) THEN (SELECT ( Min(Date(dateadded)) )
FROM sperson_info
WHERE cid = a.cid
GROUP BY cid)
ELSE 0
end ) AS date_new,
a.rank,
a.lead_status,
a.status,
a.magid,
(SELECT pname
FROM personinfo
WHERE pid = a.pid) AS pname,
p1_status,
Count(c.email_id) AS total_mail,
Count(IF(s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P1'
AND d.send_status = 1, c.email_id, NULL)) AS t_p1,
Count(IF(s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P2'
AND d.send_status = 1, c.email_id, NULL)) AS t_p2,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P3'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p3,
Count(DISTINCT IF(c.person_type = 'P3'
AND c.email_id != '', c.email_id, NULL)) AS t_p3_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P4'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p4,
Count(DISTINCT IF(c.person_type = 'P4'
AND c.email_id != '', c.email_id, NULL)) AS t_p4_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P5'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p5,
Count(DISTINCT IF(c.person_type = 'P5'
AND c.email_id != '', c.email_id, NULL)) AS t_p5_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P6'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p6,
Count(DISTINCT IF(c.person_type = 'P6'
AND c.email_id != '', c.email_id, NULL)) AS t_p6_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P7'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p7,
Count(DISTINCT IF(c.person_type = 'P7'
AND c.email_id != '', c.email_id, NULL)) AS t_p7_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P8'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p8,
Count(DISTINCT IF(c.person_type = 'P8'
AND c.email_id != '', c.email_id, NULL)) AS t_p8_e FROM mperson_companies a
INNER JOIN aperson_detail c
ON a.cid = c.cid
AND a.pid = c.pid
LEFT JOIN compose d
ON c.email_id = d.receiver_email
AND c.pid = d.pid
LEFT JOIN session_person s
ON d.session_id = s.session_id WHERE a.lead_status = 0
AND a.flag != 'q'
AND a.approval != 5
AND a.pid = 2832
AND a.rank > 1
AND c.email_id != '' GROUP BY a.cid HAVING t_p1 = 2
AND t_p2 = 2
AND IF(t_p3_e = 1, t_p3 = 2, 1 = 1)
AND IF(t_p4_e = 1, t_p4 = 2, 1 = 1)
AND IF(t_p5_e = 1, t_p5 = 2, 1 = 1)
AND IF(t_p6_e = 1, t_p6 = 2, 1 = 1)
AND IF(t_p7_e = 1, t_p7 = 2, 1 = 1)
AND IF(t_p8_e = 1, t_p8 = 2, 1 = 1) ORDER BY date_new DESC;
答案 0 :(得分:0)
需要查看SHOW CREATE TABLE
和EXPLAIN SELECT
。
与此同时
WHERE a.lead_status = 0
AND a.flag != 'q'
AND a.approval != 5
AND a.pid = 2832
AND a.rank > 1
AND c.email_id != ''
乞求
INDEX(lead_status, pid, -- in either order
flag, approval, -- in either order
rank) -- last
和这个 SELECT(最小(日期(添加日期))) 来自sperson_info cid = a.cid GROUP BY CID)
不需要包含GROUP BY cid
,因为它只会返回一个项目。
和
( a.status = 0 OR a.status > 0 )
可以很简单
a.status >= 0
但是,status
有哪些值?不平等有时效率不及平等。
还有这个
Count(IF(s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P2'
AND d.send_status = 1, c.email_id, NULL)
)
可以缩写为
SUM( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P2'
AND d.send_status = 1 )
可能还有更多建议;这可能需要几次迭代。