MySQL: ¿Puedo hacer una combinación a la izquierda y extraer solo una fila de la tabla de combinación?

Escribí una mesa de ayuda personalizada para el trabajo y ha estado funcionando muy bien ... hasta hace poco. Una consulta tieneDe Verdad ralentizado. ¡Tarda unos 14 segundos ahora! Aquí están las tablas relevantes:

CREATE TABLE `tickets` (
  `id` int(11) unsigned NOT NULL DEFAULT '0',
  `date_submitted` datetime DEFAULT NULL,
  `date_closed` datetime DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `description` text,
  `agent_id` smallint(5) unsigned NOT NULL DEFAULT '1',
  `status` smallint(5) unsigned NOT NULL DEFAULT '1',
  `priority` tinyint(4) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `date_closed` (`date_closed`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `solutions` (
  `id` int(10) unsigned NOT NULL,
  `ticket_id` mediumint(8) unsigned DEFAULT NULL,
  `date` datetime DEFAULT NULL,
  `hours_spent` float DEFAULT NULL,
  `agent_id` smallint(5) unsigned DEFAULT NULL,
  `body` text,
  PRIMARY KEY (`id`),
  KEY `ticket_id` (`ticket_id`),
  KEY `date` (`date`),
  KEY `hours_spent` (`hours_spent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Cuando un usuario envía un ticket, entra en la tabla de "tickets". Luego, a medida que los agentes resuelven el problema, registran las acciones que tomaron. Cada entrada entra en la tabla de "soluciones". En otras palabras, los boletos tienen muchas soluciones.

El objetivo de la consulta que se ha ralentizado es extraer todos los campos de la tabla "tickets" y también la última entrada de la tabla "solutions". Esta es la consulta que he estado usando:

SELECT tickets.*,
    (SELECT CONCAT_WS(" * ", DATE_FORMAT(solutions.date, "%c/%e/%y"), solutions.hours_spent, CONCAT_WS(": ", solutions.agent_id, solutions.body))
    FROM solutions
    WHERE solutions.ticket_id = tickets.id
    ORDER BY solutions.date DESC, solutions.id DESC
    LIMIT 1
) AS latest_solution_entry
FROM tickets
WHERE tickets.date_closed IS NULL
OR tickets.date_closed >= '2012-06-20 00:00:00'
ORDER BY tickets.id DESC

Aquí hay un ejemplo de cómo se ve el campo "latest_solution_entry":

6/20/12 * 1337 * 1: I restarted the computer and that fixed the problem. Yes, I took an hour to do this.

En PHP, divido el campo "latest_solution_entry" y lo formateo correctamente.

Cuando me di cuenta de que la página que ejecuta la consulta se había ralentizadocamino Abajo, ejecuté la consulta sin la subconsulta y fue súper rápido. Entonces corrí unEXPLAIN en la consulta original y consiguió esto:

+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+
| id | select_type        | table     | type  | possible_keys | key       | key_len | ref                 | rows  | Extra                       |
+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+
|  1 | PRIMARY            | tickets   | index | date_closed   | PRIMARY   | 4       | NULL                | 35804 | Using where                 |
|  2 | DEPENDENT SUBQUERY | solutions | ref   | ticket_id     | ticket_id | 4       | helpdesk.tickets.id |     1 | Using where; Using filesort |
+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+

Así que estoy buscando una manera de hacer que mi consulta sea más eficiente, y aún así lograr el mismo objetivo. ¿Algunas ideas?

Respuestas a la pregunta(4)

Su respuesta a la pregunta