Здесь я выбираю данные с помощью запросов 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;