Faster SQL-query

Faster SQL-query, huge performance improvement on installations with thousands of tickets. We use osTicket and have about 25 000 tickets and it was getting slow.