2012-05-17 176 views
1

我有我的查詢花了超過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

謝謝:)

+0

包括在你的問題的數據庫架構將使這可能更容易... – CodeZombie

回答

1

我創建了一個大型票務系統類似表之前。我發現使用last_comment_id列更新主tickets表十倍容易,可以讓您在沒有子選擇的情況下獲取所有信息。目前,你必須掃描你的comments次,所以根據它的大小,將需要一段時間。

+0

是@Aaron W.其不錯的主意,我會嘗試:)謝謝 –

2

試試這個

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 , 
     @cm := coalesce(last_comment.COM_MSG, 000000) as COM_MSG, 
     @cn := coalesce(by_customer.CUS_NAME, 000000) as CUS_NAME, 
     @be := coalesce(by_employee.EMP, 000000) as EMP, 
     @cs := coalesce(last_comment_color.COM_STATUE, 000000) as COM_STATUE 

     FROM 
     (select 
      @cm := 0, 
      @cn := 0, 
      @be := 0, 
      @cs := 0 
     ) sqlvars, 

     tickets 

     LEFT JOIN (
      SELECT com_msg AS COM_MSG 
      from  comments 
      order by com_id DESC 
      limit 1 
     ) AS last_comment 
      ON tickets.tic_id = last_comment.tic_id 

     LEFT JOIN (
       SELECT com_msg,cus_name AS CUS_NAME 
       from customers INNER JOIN comments on (cus_id = comments.tic_uid) 
       LIMIT 1 
     )AS by_customer, 
      ON by_customer.com_msg = last_comment 

      LEFT JOIN (
      SELECT com_msg,emp_name AS EMP 
      from employee INNER JOIN comments on (emp_id = comments.tic_emp) 
      LIMIT 1)AS by_employee 
      ON by_employee.com_msg = last_comment 

     LEFT JOIN (SELECT tic_id ,com_statue AS COM_STATUE 
      from  comments 
      order by com_id DESC 
      limit 1) 
      AS last_comment_color 
     ON tickets.tic_id = last_comment_color.tic_id 


     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 
+0

謝謝查詢主但:請你解釋一下這段代碼 ** @ cm:= coalesce(last_comment.COM_MSG,000000)** –

+0

@MonaAbdelmajeed我只是用session變量將你的子查詢替換成左連接.... * @cm: = *這是會話變量,該變量包含臨時數據,我只是通過這個會話控制一些查詢數據varibale –

+0

你用解釋執行你的查詢,然後你realiz編輯你的子查詢依賴於你的查詢,你的查詢執行時間慢的原因@MonaAbdelmajeed –