我有我的查詢花了超過10秒到執行MySQL的:優化SQL查詢+慢查詢
很多子查詢和大的問題左連接
這裏是我的查詢
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
謝謝:)
包括在你的問題的數據庫架構將使這可能更容易... – CodeZombie