I've got a query that takes a few minutes to run on my MySQL server, was wondering if there was a better way to write this query. I've sanitized the tables and model a bit, so this may not make complete sense. Here's the tables used:
The idea is a dispatch/ticketing system. Customers call in and a ticket gets created. Tickets may have a person assigned or may not, but if so the person's name is associated to people by people_id. Tickets have a status (reported, assigned, in process, closed, etc.) that is associated by ticket_id. If it has been worked on, there will be associated visits.
If there are visits, there will be a person on the visit that's in the same list as people on the ticket, a time of the visit, and a status (same list of statuses as tickets).
People and status tables are basically lookup tables.
What I want is:
All tickets not of a specific status. If there are visits on these tickets, need the visiting person's name, the time, and the visit status from the visit with the MOST RECENT visit time (doesn't always correspond to visit id). What I have:
Any thoughts on how I can speed it up? Assume that visits.ticket_id is indexed. status_id is not indexed on tickets or visits, it resulted in a slower query and was explained to me yesterday that since the number of possible values for status_id is low, an index won't do much good. I can deal with changing this query or adding indexes, but major changes to the tables isn't a great option right now.
EXPLAIN is telling me that tickets is a scan of 79k rows, then small numbers (3, 10, 1, 1, 1) for other tables, then 166k for the derived table dealing with visits.
Code:
Table: tickets - 79k rows
Columns:
id - autonumber primary key
status_id - foreign key to status
person_id - optional foreign key to people
Table: visits - 166k rows
Columns:
id - autonumber primary key
ticket_id - foreign key to tickets
person_id - foreign key to people
visit_time - datetime
status_id - foreign key to status
Table: people - 300 rows
Columns:
id - autonumber primary key
name - text
Table: statuses - 6 rows
Columns:
id - autonumber primary key
text - text
If there are visits, there will be a person on the visit that's in the same list as people on the ticket, a time of the visit, and a status (same list of statuses as tickets).
People and status tables are basically lookup tables.
What I want is:
All tickets not of a specific status. If there are visits on these tickets, need the visiting person's name, the time, and the visit status from the visit with the MOST RECENT visit time (doesn't always correspond to visit id). What I have:
Code:
SELECT tickets.id,
last_visits.id,
ticket_people.name,
last_visits.visit_time,
statuses.status_text,
visit_people.name
FROM tickets
LEFT JOIN (
SELECT visits.ticket_id, id, status_id, visit_time, person_id
FROM visits
INNER JOIN
(SELECT ticket_id, MAX(visit_time) AS last_time
FROM visits
GROUP BY ticket_id
) AS temp
ON visits.ticket_id = temp.ticket_id AND visits.visit_time = temp.last_time
) AS last_visits
ON tickets.id = last_visits.ticket_id
LEFT JOIN statuses
ON last_visits.status_id = statuses.id
LEFT JOIN people AS ticket_people
ON tickets.person_id = ticket_people.id
LEFT JOIN people AS visit_people
ON last_visits.person_id = visit_people.id
WHERE tickets.status_id != 4
ORDER BY tickets.id
EXPLAIN is telling me that tickets is a scan of 79k rows, then small numbers (3, 10, 1, 1, 1) for other tables, then 166k for the derived table dealing with visits.