MySQL: Posso fazer uma junção esquerda e puxar apenas uma linha da tabela de junção?

Eu escrevi um help desk personalizado para o trabalho e está funcionando muito bem ... até recentemente. Uma consulta foirealmente abrandou. Demora cerca de 14 segundos agora! Aqui estão as tabelas 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;

Quando um usuário envia um ticket, ele entra na tabela "tickets". Então, conforme os agentes resolvem o problema, eles registram as ações realizadas. Cada entrada vai para a tabela "soluções". Em outras palavras, os tickets têm muitas soluções.

O objetivo da consulta que diminuiu é extrair todos os campos da tabela "tickets" e também a última entrada da tabela "solutions". Esta é a consulta que tenho usado:

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

Aqui está um exemplo de como é o 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.

No PHP, divido o campo "latest_solution_entry" e o formato corretamente.

Quando percebi que a página que executa a consulta havia diminuídocaminho Para baixo, eu corri a consulta sem a subconsulta e foi super rápido. Eu então corri umEXPLAIN na consulta original e recebi isso:

+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+
| 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 |
+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+

Então, estou procurando uma maneira de tornar minha consulta mais eficiente, mas ainda assim atingir o mesmo objetivo. Alguma ideia?

questionAnswers(4)

yourAnswerToTheQuestion