From 7b7e85540afb9bc07e601b7865059562ca6a107d Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Tue, 23 Jun 2015 11:50:55 -0500 Subject: [PATCH] Optimise thread load query Thanks, @torohill --- include/class.thread.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/include/class.thread.php b/include/class.thread.php index 46c018282..c13014848 100644 --- a/include/class.thread.php +++ b/include/class.thread.php @@ -39,18 +39,14 @@ class Thread { $sql='SELECT ticket.ticket_id as id ' .' ,count(DISTINCT attach.attach_id) as attachments ' - .' ,count(DISTINCT message.id) as messages ' - .' ,count(DISTINCT response.id) as responses ' - .' ,count(DISTINCT note.id) as notes ' + ." ,count(DISTINCT CASE WHEN thread.thread_type = 'M' THEN thread.id ELSE NULL END) as messages " + ." ,count(DISTINCT CASE WHEN thread.thread_type = 'R' THEN thread.id ELSE NULL END) as responses " + ." ,count(DISTINCT CASE WHEN thread.thread_type = 'N' THEN thread.id ELSE NULL END) as notes " .' FROM '.TICKET_TABLE.' ticket ' .' LEFT JOIN '.TICKET_ATTACHMENT_TABLE.' attach ON (' .'ticket.ticket_id=attach.ticket_id) ' - .' LEFT JOIN '.TICKET_THREAD_TABLE.' message ON (' - ."ticket.ticket_id=message.ticket_id AND message.thread_type = 'M') " - .' LEFT JOIN '.TICKET_THREAD_TABLE.' response ON (' - ."ticket.ticket_id=response.ticket_id AND response.thread_type = 'R') " - .' LEFT JOIN '.TICKET_THREAD_TABLE.' note ON ( ' - ."ticket.ticket_id=note.ticket_id AND note.thread_type = 'N') " + .' LEFT JOIN '.TICKET_THREAD_TABLE.' thread ON (' + .'ticket.ticket_id=thread.ticket_id) ' .' WHERE ticket.ticket_id='.db_input($this->getTicketId()) .' GROUP BY ticket.ticket_id'; -- GitLab