我的查询有很大的问题,执行时间超过 10 秒
许多子查询和左连接
这是我的查询
SELECT tickets.tic_id,
tickets.tic_title,
tic_statue,
tic_priority,
tickets.tic_msg,
customers.cus_name,
employee.emp_name,
count(comments.com_id),
customers.cus_id ,
(
SELECT comments.com_msg
from comments
WHERE tickets.tic_id = comments.tic_id
order by com_id DESC
limit 1
) AS last_comment,
(
SELECT customers.cus_name
from customers INNER JOIN comments on (customers.cus_id = comments.tic_uid)
where comments.com_msg = last_comment
LIMIT 1
)AS by_customer,
(
SELECT employee.emp_name
from employee INNER JOIN comments on (employee.emp_id = comments.tic_emp)
where comments.com_msg = last_comment
LIMIT 1)AS by_employee,
tic_date,
(SELECT comments.com_statue
from comments
WHERE tickets.tic_id = comments.tic_id
order by com_id DESC
limit 1)
AS last_comment_color
FROM tickets
LEFT JOIN employee
on (tickets.tic_emp = employee.emp_id)
LEFT JOIN customers
on (tickets.tic_cus = customers.cus_id)
LEFT join cats
on (tickets.tic_cat = cats.cat_id)
LEFT JOIN comments
on (tickets.tic_id = comments.tic_id)
GROUP by tickets.tic_id
ORDER BY tickets.tic_statue ASC, comments.com_time DESC
limit 50
结果工作正常,但时间长
索引:tickets.tic_id comments.com_id comments.tic_id customers.cus_id
谢谢 :)