Skip to content
Snippets Groups Projects
Commit 90f783b5 authored by Eivind Ravndal's avatar Eivind Ravndal Committed by Jared Hancock
Browse files

Faster SQL-query, huge performance improvement on installations with thousands of tickets.

parent d6970162
No related branches found
No related tags found
No related merge requests found
......@@ -1797,47 +1797,54 @@ class Ticket {
if(!$staff || (!is_object($staff) && !($staff=Staff::lookup($staff))) || !$staff->isStaff())
return null;
$sql='SELECT count(open.ticket_id) as open, count(answered.ticket_id) as answered '
.' ,count(overdue.ticket_id) as overdue, count(assigned.ticket_id) as assigned, count(closed.ticket_id) as closed '
.' FROM '.TICKET_TABLE.' ticket '
.' LEFT JOIN '.TICKET_TABLE.' open
ON (open.ticket_id=ticket.ticket_id
AND open.status=\'open\'
AND open.isanswered=0
'.((!($cfg->showAssignedTickets() || $staff->showAssignedTickets()))?
' AND open.staff_id=0 ':'').') '
.' LEFT JOIN '.TICKET_TABLE.' answered
ON (answered.ticket_id=ticket.ticket_id
AND answered.status=\'open\'
AND answered.isanswered=1) '
.' LEFT JOIN '.TICKET_TABLE.' overdue
ON (overdue.ticket_id=ticket.ticket_id
AND overdue.status=\'open\'
AND overdue.isoverdue=1) '
.' LEFT JOIN '.TICKET_TABLE.' assigned
ON (assigned.ticket_id=ticket.ticket_id
AND assigned.status=\'open\'
AND assigned.staff_id='.db_input($staff->getId()).')'
.' LEFT JOIN '.TICKET_TABLE.' closed
ON (closed.ticket_id=ticket.ticket_id
AND closed.status=\'closed\' )'
.' WHERE (ticket.staff_id='.db_input($staff->getId());
$where = '1 = 1';
if(($teams=$staff->getTeams()))
$sql.=' OR ticket.team_id IN('.implode(',', db_input(array_filter($teams))).')';
$where .=' OR ticket.team_id IN('.implode(',', db_input(array_filter($teams))).')';
if(!$staff->showAssignedOnly() && ($depts=$staff->getDepts())) //Staff with limited access just see Assigned tickets.
$sql.=' OR ticket.dept_id IN('.implode(',', db_input($depts)).') ';
$sql.=')';
$where .=' OR ticket.dept_id IN('.implode(',', db_input($depts)).') ';
if(!$cfg || !($cfg->showAssignedTickets() || $staff->showAssignedTickets()))
$sql.=' AND (ticket.staff_id=0 OR ticket.staff_id='.db_input($staff->getId()).') ';
return db_fetch_array(db_query($sql));
$where2 =' AND (ticket.staff_id=0 OR ticket.staff_id='.db_input($staff->getId()).') ';
$sql = 'SELECT \'open\', count( ticket.ticket_id ) AS tickets '
.'FROM ' . TICKET_TABLE . ' ticket '
.'WHERE ticket.status = \'open\' '
.'AND ticket.isanswered =0 '
.'AND ( ' . $where . ' ) ' . $where2 . ' '
.'UNION SELECT \'answered\', count( ticket.ticket_id ) AS tickets '
.'FROM ' . TICKET_TABLE . ' ticket '
.'WHERE ticket.status = \'open\' '
.'AND ticket.isanswered =1 '
.'AND ( ' . $where . ' ) '
.'UNION SELECT \'overdue\', count( ticket.ticket_id ) AS tickets '
.'FROM ' . TICKET_TABLE . ' ticket '
.'WHERE ticket.status = \'open\' '
.'AND ticket.isoverdue =1 '
.'AND ( ' . $where . ' ) '
.'UNION SELECT \'assigned\', count( ticket.ticket_id ) AS tickets '
.'FROM ' . TICKET_TABLE . ' ticket '
.'WHERE ticket.status = \'open\' '
.'AND ticket.staff_id = ' . db_input($staff->getId()) . ' '
.'AND ( ' . $where . ' ) '
.'UNION SELECT \'closed\', count( ticket.ticket_id ) AS tickets '
.'FROM ' . TICKET_TABLE . ' ticket '
.'WHERE ticket.status = \'closed\' '
.'AND ( ' . $where . ' ) ';
$res = db_query($sql);
$returnArray = array();
while($row = db_fetch_row($res)) {
$returnArray[$row[0]] = $row[1];
}
return $returnArray;
}
/* Quick client's tickets stats
@email - valid email.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment