MySQL:我可以进行左连接并从连接表中只拉一行吗?

Nic*_*ick 4 mysql query-optimization

我为工作写了一个自定义帮助台,它一直运行良好......直到最近.一个查询确实放慢了速度.现在大约需要14秒!以下是相关表格:

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;
Run Code Online (Sandbox Code Playgroud)

当用户提交票证时,它将进入"票证"表.然后,当代理人解决问题时,他们会记录他们采取的行动.每个条目都进入"解决方案"表.换句话说,门票有很多解决方案.

减慢查询的目标是从"票证"表中提取所有字段,也从"解决方案"表中提取最新条目.这是我一直在使用的查询:

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
Run Code Online (Sandbox Code Playgroud)

以下是"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.
Run Code Online (Sandbox Code Playgroud)

在PHP中,我拆分了"latest_solution_entry"字段并正确格式化.

当我注意到,在运行查询的页面已经放慢办法了,我不跑了子查询的查询,这是超级快.然后我EXPLAIN在原始查询上运行并得到了这个:

+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+
| 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 |
+----+--------------------+-----------+-------+---------------+-----------+---------+---------------------+-------+-----------------------------+
Run Code Online (Sandbox Code Playgroud)

所以我正在寻找一种方法来提高我的查询效率,但仍然达到了同样的目标.有任何想法吗?

Oli*_*and 18

让我总结一下我的理解:你想选择每张票和它的最后解决方案.

我喜欢使用以下模式来解决这类问题,因为它避免了子查询模式,因此在需要性能的地方非常好.缺点是理解起来有点棘手:

SELECT
  t.*,
  s1.*
FROM tickets t
INNER JOIN solutions s1 ON t.id = s1.ticket_id
LEFT JOIN solutions s2 ON s1.ticket_id = s2.ticket_id AND s2.id > s1.id
WHERE s2.id IS NULL;
Run Code Online (Sandbox Code Playgroud)

为了更好地理解,我只写了模式的核心.

关键是:

  • solutions表格的LEFT JOIN 与s1.ticket_id = s2.ticket_id条件:它模仿GROUP BY ticket_id.

  • 条件s2.id > s1.id:它是"我只想要最后一个解决方案"的SQL,它模拟了MAX().我认为在你的模型中,the last意味着with the greatest id你可以在这里使用日期条件.请注意,这s2.id < s1.id将为您提供第一个解决方案.

  • WHERE子句s2.id IS NULL:最奇怪的但绝对必要的...只保留你想要的记录.

试试看,让我知道:)

编辑1:我刚刚意识到第二点假设是过度简化了问题.这使它更有趣:p我正试图看看这种模式如何适用于你的date, id订购.

编辑2:好的,它有点扭曲,效果很好.LEFT JOIN的条件变为:

LEFT JOIN solutions s2 ON s1.ticket_id = s2.ticket_id
  AND (s2.date > s1.date OR (s2.date = s1.date AND s2.id > s1.id))
Run Code Online (Sandbox Code Playgroud)