1

クエリに大きな問題があり、実行に 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

結果は正常に動作しますが、長い時間

インデックス: ticket.tic_id comments.com_id comments.tic_id customers.cus_id

ありがとうございました :)

4

2 に答える 2

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
于 2012-05-17T19:15:03.260 に答える
1

以前、同様のテーブルを使用して大規模なチケット システムを作成しました。副選択なしですべての情報を取得できる列を使用してメインticketsテーブルを更新する方が 10 倍簡単であることがわかりました。現在、テーブルを5last_comment_id回スキャンする必要があるため、サイズによっては時間がかかります。comments

于 2012-05-17T18:52:26.110 に答える