From 387fe1ae3c5afb1e005a6b45a36cd8051ff1e07a Mon Sep 17 00:00:00 2001
From: Jared Hancock <gravydish@gmail.com>
Date: Tue, 14 Aug 2018 03:23:12 +0000
Subject: [PATCH] cron: specify index for ticket aging query

Somehow on large datasets (like >1M tickets), MySQL can get confused on which
index will provide the best performance. Generally, as systems age, they will
have significantly more closed tickets than open ones. Therefore, it should be
safe to assume that scanning the `status_id` index on the ticket table for
`open` tickets would be the fastest way to arrive at the sort-of short list of
tickets which should need to possibly be aged.
---
 include/class.ticket.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/class.ticket.php b/include/class.ticket.php
index 0347004de..9db2bff77 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -4210,7 +4210,8 @@ implements RestrictedAccess, Threadable, Searchable {
          Punt for now
          */
 
-        $sql='SELECT ticket_id FROM '.TICKET_TABLE.' T1 '
+        $sql='SELECT ticket_id FROM '.TICKET_TABLE.' T1'
+            .' USE INDEX (status_id)'
             .' INNER JOIN '.TICKET_STATUS_TABLE.' status
                 ON (status.id=T1.status_id AND status.state="open") '
             .' LEFT JOIN '.SLA_TABLE.' T2 ON (T1.sla_id=T2.id AND T2.flags & 1 = 1) '
-- 
GitLab