SQL query help

Status
Not open for further replies.

dlp

Ars Tribunus Militum
2,220
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:
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
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:
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
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.
 

wallinbl

Ars Legatus Legionis
14,319
Subscriptor
This:

Code:
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

Is not functionally different from this:
Code:
LEFT JOIN
    (SELECT ticket_id, MAX(visit_time) AS last_time
     FROM visits
     GROUP BY ticket_id
    ) AS last_visit_time ON tickets.id = last_visit_time.ticket_id
LEFT JOIN visits on tickets.id = visits.ticket_id and visits.visit_time = last_visit_time.last_time

For performance reasons, I might take the subquery one step further and filter it explicitly:
Code:
LEFT JOIN
    (SELECT ticket_id, MAX(visit_time) AS last_time
     FROM visits
     INNER JOIN tickets on visits.ticket_id = tickets.id
     WHERE tickets.status_id != 4
     GROUP BY ticket_id
    ) AS last_visit_time ON tickets.id = last_visit_time.ticket_id
LEFT JOIN visits on tickets.id = visits.ticket_id and visits.visit_time = last_visit_time.last_time
 

GohanIYIan

Ars Legatus Legionis
10,195
[url=http:/civis/viewtopic.php?p=25182039#p25182039:2xvxhaal said:
wallinbl[/url]":2xvxhaal]
For performance reasons, I might take the subquery one step further and filter it explicitly:

This is good advice. MySQL generally isn't going to apply the outer WHERE clause to your subqueries for you.

In my experience, when joining against a subquery MySQL almost always creates a temporary table. Temporary tables are not indexed, so index(es) on the source table are irrelevant. If the temporary tables don't fit in memory, the query is just going to take a long time.

It may actually be faster to do this:
Code:
SELECT tickets.id,
 visits.id,
 ticket_people.name,
 visits.visit_time,
 statuses.status_text,
 visit_people.name
FROM tickets
 LEFT JOIN visits
  ON tickets.id = visits.ticket_id
 LEFT JOIN statuses
  ON 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 visits.person_id = visit_people.id
WHERE tickets.status_id != 4
ORDER BY tickets.id, visits.visit_time DESC
And then filter the results yourself. It depends on the dataset, but if avoiding temporary tables means no paging to disk that can be a big boost.
 
Status
Not open for further replies.