栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

检索每个组中的最后一条记录

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

检索每个组中的最后一条记录

MySQL 8.0现在支持窗口功能,就像几乎所有流行的SQL实现一样。使用这种标准语法,我们可以编写每组最多n个查询:

WITH ranked_messages AS (  SELECt m.*, ROW_NUMBER() OVER (PARTITION BY name ORDER BY id DESC) AS rn  FROM messages AS m)SELECt * FROM ranked_messages WHERe rn = 1;

以下是我在2009年为此问题写的原始答案:


我这样写解决方案:

SELECt m1.*FROM messages m1 LEFT JOIN messages m2 ON (m1.name = m2.name AND m1.id < m2.id)WHERe m2.id IS NULL;

关于性能,一种解决方案可能会更好,这取决于数据的性质。因此,您应该测试两个查询,并使用给定数据库性能最好的查询。

例如,我有一个StackOverflow
August数据转储
的副本。我将其用于基准测试。该

Posts
表中有1,114,357行。它在Macbook
Pro 2.40GHz的MySQL 5.0.75上运行。

我将编写查询以查找给定用户ID(我的用户)的最新帖子。

首先 在子查询中使用@Eric 所示的技术

GROUP BY

SELECt p1.postidFROM Posts p1INNER JOIN (SELECt pi.owneruserid, MAX(pi.postid) AS maxpostid FROM Posts pi GROUP BY pi.owneruserid) p2  ON (p1.postid = p2.maxpostid)WHERe p1.owneruserid = 20860;1 row in set (1 min 17.89 sec)

甚至

EXPLAIN
分析也要花费超过16秒的时间:

+----+-------------+------------+--------+----------------------------+-------------+---------+--------------+---------+-------------+| id | select_type | table      | type   | possible_keys   | key         | key_len | ref          | rows    | Extra       |+----+-------------+------------+--------+----------------------------+-------------+---------+--------------+---------+-------------+|  1 | PRIMARY     | <derived2> | ALL    | NULL | NULL        | NULL    | NULL         |   76756 |  | |  1 | PRIMARY     | p1         | eq_ref | PRIMARY,PostId,OwnerUserId | PRIMARY     | 8       | p2.maxpostid |       1 | Using where | |  2 | DERIVED     | pi         | index  | NULL | OwnerUserId | 8       | NULL         | 1151268 | Using index | +----+-------------+------------+--------+----------------------------+-------------+---------+--------------+---------+-------------+3 rows in set (16.09 sec)

现在用产生同样的查询结果 我的技术有

LEFT JOIN

SELECT p1.postidFROM Posts p1 LEFT JOIN posts p2  ON (p1.owneruserid = p2.owneruserid AND p1.postid < p2.postid)WHERe p2.postid IS NULL AND p1.owneruserid = 20860;1 row in set (0.28 sec)

EXPLAIN
分析表明,这两个表都能够使用他们的指标:

+----+-------------+-------+------+----------------------------+-------------+---------+-------+------+--------------------------------------+| id | select_type | table | type | possible_keys   | key         | key_len | ref   | rows | Extra          |+----+-------------+-------+------+----------------------------+-------------+---------+-------+------+--------------------------------------+|  1 | SIMPLE      | p1    | ref  | OwnerUserId     | OwnerUserId | 8       | const | 1384 | Using index    | |  1 | SIMPLE      | p2    | ref  | PRIMARY,PostId,OwnerUserId | OwnerUserId | 8       | const | 1384 | Using where; Using index; Not exists | +----+-------------+-------+------+----------------------------+-------------+---------+-------+------+--------------------------------------+2 rows in set (0.00 sec)

这是我的

Posts
桌子的DDL :

CREATE TABLE `posts` (  `PostId` bigint(20) unsigned NOT NULL auto_increment,  `PostTypeId` bigint(20) unsigned NOT NULL,  `AcceptedAnswerId` bigint(20) unsigned default NULL,  `ParentId` bigint(20) unsigned default NULL,  `CreationDate` datetime NOT NULL,  `Score` int(11) NOT NULL default '0',  `ViewCount` int(11) NOT NULL default '0',  `Body` text NOT NULL,  `OwnerUserId` bigint(20) unsigned NOT NULL,  `OwnerDisplayName` varchar(40) default NULL,  `LastEditorUserId` bigint(20) unsigned default NULL,  `LastEditDate` datetime default NULL,  `LastActivityDate` datetime default NULL,  `Title` varchar(250) NOT NULL default '',  `Tags` varchar(150) NOT NULL default '',  `AnswerCount` int(11) NOT NULL default '0',  `CommentCount` int(11) NOT NULL default '0',  `FavoriteCount` int(11) NOT NULL default '0',  `ClosedDate` datetime default NULL,  PRIMARY KEY  (`PostId`),  UNIQUE KEY `PostId` (`PostId`),  KEY `PostTypeId` (`PostTypeId`),  KEY `AcceptedAnswerId` (`AcceptedAnswerId`),  KEY `OwnerUserId` (`OwnerUserId`),  KEY `LastEditorUserId` (`LastEditorUserId`),  KEY `ParentId` (`ParentId`),  ConSTRAINT `posts_ibfk_1` FOREIGN KEY (`PostTypeId`) REFERENCES `posttypes` (`PostTypeId`)) ENGINE=InnoDB;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/367094.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号