检索每组中的最后一条记录 - MySQL
有一个表messages
,其中包含如下所示的数据:
Id Name Other_Columns
-------------------------
1 A A_data_1
2 A A_data_2
3 A A_data_3
4 B B_data_1
5 B B_data_2
6 C C_data_1
如果我运行查询select * from messages group by name
,我将得到的结果为:
1 A A_data_1
4 B B_data_1
6 C C_data_1
查询将返回什么下面的结果?
3 A A_data_3
5 B B_data_2
6 C C_data_1
也就是说,应该返回每组中的最后一条记录。
目前,这是我使用的查询:
SELECT
*
FROM (SELECT
*
FROM messages
ORDER BY id DESC) AS x
GROUP BY name
但这看起来效率很低。还有其他方法可以达到相同的结果吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
MySQL 8.0 现在支持窗口函数,就像几乎所有流行的函数一样SQL 实现。使用这种标准语法,我们可以编写每组最大n个查询:
这种方法和其他方法来查找 分组最大行数在 MySQL 手册中进行了说明。
以下是我在 2009 年为这个问题写的原始答案:
我是这样写解决方案的:
关于性能,一种解决方案或另一种可能会更好,具体取决于数据的性质。因此,您应该测试这两个查询,并根据您的数据库使用性能更好的查询。
例如,我有一份 StackOverflow 8 月数据转储 的副本。我将用它来进行基准测试。
Posts
表中有 1,114,357 行。它在我的 Macbook Pro 2.40GHz 上的 MySQL 5.0.75 上运行。我将编写一个查询来查找给定用户 ID(我的)的最新帖子。
首先使用如图所示的技术 @Eric 在子查询中使用
GROUP BY
:甚至
EXPLAIN
分析 需要超过 16 秒:现在使用 我的技术与
LEFT JOIN
:EXPLAIN
分析显示两个表都能够使用它们的索引:这是我的
Posts
表的 DDL:评论者注意:如果您想要另一个基准使用不同版本的 MySQL、不同的数据集或不同的表设计,您可以自行操作。我已经展示了上面的技术。 Stack Overflow 在这里向您展示如何进行软件开发工作,而不是为您完成所有工作。
MySQL 8.0 now supports windowing functions, like almost all popular SQL implementations. With this standard syntax, we can write greatest-n-per-group queries:
This and other approaches to finding groupwise maximal rows are illustrated in the MySQL manual.
Below is the original answer I wrote for this question in 2009:
I write the solution this way:
Regarding performance, one solution or the other can be better, depending on the nature of your data. So you should test both queries and use the one that is better at performance given your database.
For example, I have a copy of the StackOverflow August data dump. I'll use that for benchmarking. There are 1,114,357 rows in the
Posts
table. This is running on MySQL 5.0.75 on my Macbook Pro 2.40GHz.I'll write a query to find the most recent post for a given user ID (mine).
First using the technique shown by @Eric with the
GROUP BY
in a subquery:Even the
EXPLAIN
analysis takes over 16 seconds:Now produce the same query result using my technique with
LEFT JOIN
:The
EXPLAIN
analysis shows that both tables are able to use their indexes:Here's the DDL for my
Posts
table:Note to commenters: If you want another benchmark with a different version of MySQL, a different dataset, or different table design, feel free to do it yourself. I have shown the technique above. Stack Overflow is here to show you how to do software development work, not to do all the work for you.
UPD:2017-03-31,版本5.7.5 默认启用 ONLY_FULL_GROUP_BY 开关(因此,非确定性 GROUP BY 查询被禁用)。此外,他们更新了 GROUP BY 实现,即使禁用了开关,该解决方案也可能无法按预期工作。需要检查一下。
当组内的项目数相当小时,Bill Karwin 的上述解决方案工作正常,但当组相当大时,查询的性能会变得很差,因为该解决方案需要大约 n*n /2 + n/2 仅
IS NULL
比较。我在包含
18684446
行和1182
组的 InnoDB 表上进行了测试。该表包含功能测试的测试结果,并以(test_id, request_id)
作为主键。因此,test_id
是一个组,我正在为每个test_id
搜索最后一个request_id
。Bill 的解决方案已经在我的 Dell e4310 上运行了几个小时,尽管它在覆盖索引上运行(因此在 EXPLAIN 中使用索引),但我不知道它什么时候会完成。
我有几个基于相同想法的其他解决方案:
(group_id, item_value)
对是其中的最后一个值每个group_id
,如果我们按降序遍历索引,则这是每个group_id
的第一个;MySQL 使用索引的 3 种方式是一篇很棒的文章,可以帮助您了解一些细节。
解决方案 1
这个速度快得令人难以置信,在我的 18M+ 行上大约需要 0.8 秒:
如果您想将顺序更改为 ASC,请将其放入子查询中,仅返回 ids 并使用它作为连接到其余列的子查询:
这对我的数据大约需要 1.2 秒。
解决方案 2
这是另一个解决方案,我的表大约需要 19 秒:
它也按降序返回测试。它要慢得多,因为它执行完整索引扫描,但它可以让您了解如何为每个组输出 N 个最大行。
查询的缺点是查询缓存无法缓存其结果。
UPD: 2017-03-31, the version 5.7.5 of MySQL made the ONLY_FULL_GROUP_BY switch enabled by default (hence, non-deterministic GROUP BY queries became disabled). Moreover, they updated the GROUP BY implementation and the solution might not work as expected anymore even with the disabled switch. One needs to check.
Bill Karwin's solution above works fine when item count within groups is rather small, but the performance of the query becomes bad when the groups are rather large, since the solution requires about
n*n/2 + n/2
of onlyIS NULL
comparisons.I made my tests on a InnoDB table of
18684446
rows with1182
groups. The table contains testresults for functional tests and has the(test_id, request_id)
as the primary key. Thus,test_id
is a group and I was searching for the lastrequest_id
for eachtest_id
.Bill's solution has already been running for several hours on my dell e4310 and I do not know when it is going to finish even though it operates on a coverage index (hence
using index
in EXPLAIN).I have a couple of other solutions that are based on the same ideas:
(group_id, item_value)
pair is the last value within eachgroup_id
, that is the first for eachgroup_id
if we walk through the index in descending order;3 ways MySQL uses indexes is a great article to understand some details.
Solution 1
This one is incredibly fast, it takes about 0,8 secs on my 18M+ rows:
If you want to change the order to ASC, put it in a subquery, return the ids only and use that as the subquery to join to the rest of the columns:
This one takes about 1,2 secs on my data.
Solution 2
Here is another solution that takes about 19 seconds for my table:
It returns tests in descending order as well. It is much slower since it does a full index scan but it is here to give you an idea how to output N max rows for each group.
The disadvantage of the query is that its result cannot be cached by the query cache.
使用您的 子查询 返回正确的分组,因为您一半的地方。
试试这个:
如果它不是
id
,你想要的最大值:这样,你可以避免相关子查询和/或子查询中的排序,这往往非常慢/低效。
Use your subquery to return the correct grouping, because you're halfway there.
Try this:
If it's not
id
you want the max of:This way, you avoid correlated subqueries and/or ordering in your subqueries, which tend to be very slow/inefficient.
我找到了一个不同的解决方案,即获取每个组中最后一篇文章的 ID,然后使用第一个查询的结果作为
WHERE x IN
构造的参数从消息表中进行选择:我不知道与其他一些解决方案相比,它的性能如何,但它对于我的包含 3 多百万行的表来说效果非常好。 (4 秒执行,1200 多个结果)
这应该适用于 MySQL 和 SQL Server。
I arrived at a different solution, which is to get the IDs for the last post within each group, then select from the messages table using the result from the first query as the argument for a
WHERE x IN
construct:I don't know how this performs compared to some of the other solutions, but it worked spectacularly for my table with 3+ million rows. (4 second execution with 1200+ results)
This should work both on MySQL and SQL Server.
通过子查询解决fiddle Link
通过连接条件解决小提琴链接
这篇文章的原因是仅提供小提琴链接。
其他答案中已经提供了相同的 SQL。
Solution by sub query fiddle Link
Solution By join condition fiddle link
Reason for this post is to give fiddle link only.
Same SQL is already provided in other answers.
我们将了解如何使用 MySQL 获取记录 Group By 中的最后一条记录。例如,如果您有此帖子结果集。
我希望能够获取每个类别中的最后一篇文章,即标题 3、标题 5 和标题 6要按类别获取帖子,您将使用 MySQL Group By 键盘。
但我们从这个查询中得到的结果是。
group by 将始终返回结果集上组中的第一条记录。
这将返回每个组中 ID 最高的帖子。
参考点击这里
We will look at how you can use MySQL at getting the last record in a Group By of records. For example if you have this result set of posts.
I want to be able to get the last post in each category which are Title 3, Title 5 and Title 6. To get the posts by the category you will use the MySQL Group By keyboard.
But the results we get back from this query is.
The group by will always return the first record in the group on the result set.
This will return the posts with the highest IDs in each group.
Reference Click Here
一种相当快的方法如下。
结果
An approach with considerable speed is as follows.
Result
这里有两个建议。首先,如果 mysql 支持 ROW_NUMBER(),则非常简单:
我假设“最后”是指 Id 顺序中的最后一个。如果不是,请相应地更改 ROW_NUMBER() 窗口的 ORDER BY 子句。如果 ROW_NUMBER() 不可用,这是另一种解决方案:
其次,如果不可用,这通常是继续的好方法:
换句话说,选择不存在具有相同名称的 Later-Id 消息的消息。
Here are two suggestions. First, if mysql supports ROW_NUMBER(), it's very simple:
I'm assuming by "last" you mean last in Id order. If not, change the ORDER BY clause of the ROW_NUMBER() window accordingly. If ROW_NUMBER() isn't available, this is another solution:
Second, if it doesn't, this is often a good way to proceed:
In other words, select messages where there is no later-Id message with the same Name.
显然,有很多不同的方法可以获得相同的结果,您的问题似乎是在 MySQL 中获得每组最后结果的有效方法是什么。如果您正在处理大量数据,并且假设您将 InnoDB 与最新版本的 MySQL(例如 5.7.21 和 8.0.4-rc)一起使用,那么可能没有一种有效的方法来做到这一点。
有时我们需要对行数超过 6000 万的表执行此操作。
对于这些示例,我将使用仅包含约 150 万行的数据,其中查询需要查找数据中所有组的结果。在我们的实际案例中,我们经常需要返回大约 2,000 个组的数据(假设不需要检查太多数据)。
我将使用以下表格:
温度表填充了大约 150 万条随机记录,以及 100 个不同的组。
selected_group 填充了这 100 个组(在我们的例子中,所有组的比例通常小于 20%)。
由于此数据是随机的,这意味着多行可以具有相同的记录时间戳。我们想要的是按照 groupID 的顺序获取所有选定组的列表,其中包含每个组的最后记录时间戳,如果同一组有多个这样的匹配行,则获取这些行的最后一个匹配 id。
如果假设 MySQL 有一个 last() 函数,它从特殊 ORDER BY 子句中的最后一行返回值,那么我们可以简单地执行以下操作:
在这种情况下,只需要检查几百行,因为它不使用任何正常的 GROUP BY 功能。这将在 0 秒内执行,因此效率很高。
请注意,通常在 MySQL 中,我们会在 GROUP BY 子句之后看到 ORDER BY 子句,但是此 ORDER BY 子句用于确定 Last() 函数的 ORDER,如果它位于 GROUP BY 之后,那么它将对 GROUPS 进行排序。如果不存在 GROUP BY 子句,则所有返回行中的最后一个值将相同。
然而 MySQL 没有这个,所以让我们看看它所拥有的不同想法,并证明这些都不是有效的。
示例 1
这检查了 3,009,254 行,在 5.7.21 上花费了约 0.859 秒,在 8.0.4-rc 上花费了约
1.25 秒示例 2
这检查了 1,505,331 行,在 8.0.4-rc 上花费了约 1.25 秒5.7.21 和 8.0.4-rc 上稍长
示例 3
这检查了 3,009,685 行,在 5.7.21 上花费了约 1.95 秒,在 8.0.4-rc 上稍长
示例 4
在 5.7.21 上检查了 6,137,810 行,花费了约 2.2 秒,在 8.0.4-rc 上花费了约 2.2 秒
示例 5
在 8.0.4-rc 上检查了 6,017,808 行,花费了约 4.2 秒
>示例 6
这检查了 6,017,908 行,在 8.0.4-rc 上花费了大约 17.5 秒
示例 7
这个花费了很长时间,所以我不得不杀死它。
Clearly there are lots of different ways of getting the same results, your question seems to be what is an efficient way of getting the last results in each group in MySQL. If you are working with huge amounts of data and assuming you are using InnoDB with even the latest versions of MySQL (such as 5.7.21 and 8.0.4-rc) then there might not be an efficient way of doing this.
We sometimes need to do this with tables with even more than 60 million rows.
For these examples I will use data with only about 1.5 million rows where the queries would need to find results for all groups in the data. In our actual cases we would often need to return back data from about 2,000 groups (which hypothetically would not require examining very much of the data).
I will use the following tables:
The temperature table is populated with about 1.5 million random records, and with 100 different groups.
The selected_group is populated with those 100 groups (in our cases this would normally be less than 20% for all of the groups).
As this data is random it means that multiple rows can have the same recordedTimestamps. What we want is to get a list of all of the selected groups in order of groupID with the last recordedTimestamp for each group, and if the same group has more than one matching row like that then the last matching id of those rows.
If hypothetically MySQL had a last() function which returned values from the last row in a special ORDER BY clause then we could simply do:
which would only need to examine a few 100 rows in this case as it doesn't use any of the normal GROUP BY functions. This would execute in 0 seconds and hence be highly efficient.
Note that normally in MySQL we would see an ORDER BY clause following the GROUP BY clause however this ORDER BY clause is used to determine the ORDER for the last() function, if it was after the GROUP BY then it would be ordering the GROUPS. If no GROUP BY clause is present then the last values will be the same in all of the returned rows.
However MySQL does not have this so let's look at different ideas of what it does have and prove that none of these are efficient.
Example 1
This examined 3,009,254 rows and took ~0.859 seconds on 5.7.21 and slightly longer on 8.0.4-rc
Example 2
This examined 1,505,331 rows and took ~1.25 seconds on 5.7.21 and slightly longer on 8.0.4-rc
Example 3
This examined 3,009,685 rows and took ~1.95 seconds on 5.7.21 and slightly longer on 8.0.4-rc
Example 4
This examined 6,137,810 rows and took ~2.2 seconds on 5.7.21 and slightly longer on 8.0.4-rc
Example 5
This examined 6,017,808 rows and took ~4.2 seconds on 8.0.4-rc
Example 6
This examined 6,017,908 rows and took ~17.5 seconds on 8.0.4-rc
Example 7
This one was taking forever so I had to kill it.
这是使用带有 order by 的
GROUP_CONCAT
和SUBSTRING_INDEX
来获取最后一条相关记录的另一种方法,以从列表中选择一条记录以上查询将对所有
进行分组位于同一
将以降序排列特定组中的所有Name
组中并使用ORDER BY id DESC
的 Other_ColumnsOther_Columns
在我的例子中,使用提供的分隔符,我使用了||
,在此列表上使用SUBSTRING_INDEX
将选择第一个小提琴演示
Here is another way to get the last related record using
GROUP_CONCAT
with order by andSUBSTRING_INDEX
to pick one of the record from the listAbove query will group the all the
Other_Columns
that are in sameName
group and usingORDER BY id DESC
will join all theOther_Columns
in a specific group in descending order with the provided separator in my case i have used||
,usingSUBSTRING_INDEX
over this list will pick the first oneFiddle Demo
@Vijay Dev,您好,如果您的表 messages 包含 Id (自动增量主键),那么要根据主键获取最新记录,您的查询应如下所示:
Hi @Vijay Dev if your table messages contains Id which is auto increment primary key then to fetch the latest record basis on the primary key your query should read as below:
如果您需要分组查询中文本列的最新或最旧记录,并且您不想使用子查询,则可以执行此操作...
例如。您有一个电影列表,需要获取该系列的计数和最新电影
这返回...
MAX 将返回具有最高值的行,因此通过将 id 与名称连接,您现在将获得最新的记录,然后只需去掉 id 即可获得最终结果。
比使用子查询更有效。
因此,对于给定的示例:
快乐编码,以及“愿原力与你同在”:)
If you need the most recent or oldest record of a text column in a grouped query, and you would rather not use a subquery, you can do this...
Ex. You have a list of movies and need to get the count in the series and the latest movie
This returns...
MAX will return the row with the highest value, so by concatenating the id to the name, you now will get the newest record, then just strip off the id for your final result.
More efficient than using a subquery.
So for the given example:
Happy coding, and "May The Force Be With You" :)
您也可以从这里观看风景。
http://sqlfiddle.com/#!9/ef42b/9
第一解决方案
第二种解决方案
You can take view from here as well.
http://sqlfiddle.com/#!9/ef42b/9
FIRST SOLUTION
SECOND SOLUTION
**
嗨,此查询可能有帮助:
**
**
Hi, this query might help :
**
试试这个:
Try this:
我还没有使用大型数据库进行测试,但我认为这可能比连接表更快:
I've not yet tested with large DB but I think this could be faster than joining tables:
我在 https://dzone.com/articles 中找到了最佳解决方案/获取每个mysql组中的最后一条记录
i find best solution in https://dzone.com/articles/get-last-record-in-each-mysql-group
在我寻求通用分组最大值的过程中,我看到了许多关于该主题的答案和博客文章。甚至 我最喜欢的(实际上是有关该主题的精彩系列的一部分)未能找到可移植的解决方案,而是深入研究每个 RDMBS 的细节。
幸运的是,确实存在便携式解决方案!
为此所需的二级索引是
name
。 (name, id
将是相同的,因为主键始终隐式包含在内。)创建
message
组,并使用依赖子查询来获取最新行每组。这是可移植的,因为它只需要以下构建块的组合:
GROUP BY
。ORDER BY [ASC/DESC]
和LIMIT/TOP
索引SELECT
。只需确保具有正确的索引:
GroupKeyColumn(s)、GroupWinnerColumn(s)、PrimaryKeyColumn(s)
。在OP的例子中,组键是
name
,组的获胜者由id
确定,并且主键已经被它覆盖,所以:name, id
。许多人提出了涉及子查询的解决方案,但最容易被忽视的方面是高度特定的排序子句集,它导致使用正确的索引 - 在正确的遍历方向上,同样如此。
其他优势
ASC
) 与最大值 (DESC
)。时间戳、id
。 (这也使我们能够消除非唯一获胜者的歧义,例如“最新时间戳”。)company_id,department_name
。WHERE
轻松扩展要选择的组。WHERE
轻松扩展要忽略的项目,包括索引 (id >= 1000
) 和非索引 (is_deleted = 0
) 。为什么这个工作[最佳]?
想象一下翻阅物理电话簿,找到每个城镇的最后一个条目,即具有该城镇字母顺序最大名称的条目。你会怎么做?
你将从最后开始。书中的最后一个条目是最后一个城镇的组最大值。这是您遇到的第一个结果行。
对于每个后续所需的结果行,您将向后进行二分搜索,直到下一个最大的城镇。在当前城镇过渡到其前任城镇的位置,存在前任城镇的最后一行(按字母顺序排列的最大名称),即下一个结果行。重复直到不再有城镇。
粗略地说,电话簿就像
{ Town, Name, PhoneNumber }
上的二级索引,以PhoneNumber
作为主键。 (为了论证,我简化了事情,假装电话号码分配给一个人,名字形成一列。)您实际上是通过索引进行反向搜索。通过高效地重复跳转到下一个城镇(得益于二分搜索或 B 树结构),工作受到结果行数而不是总行数的限制。这是渐近最优的。由于反向遍历方向,您遇到的每个城镇都从其最大的行(您的目标)“开始”。这很重要:想象一下,如果您必须扫描一个城镇的所有行,将会有多少不必要的工作量。
将解决方案更改为 groupwise-min 与更改遍历方向(即从
DESC
到ASC
)一样简单。RDBMS 注释
Using index
,但 MySQL 5.7 显示了令人担忧的Using where;使用index
,但实际上执行正确。 (在涉及非常大的组的庞大数据集上进行测试。在约 3 秒内获得分布在数亿条记录中的数万个结果。)SELECT TOP 1
SELECT ... LIMIT 1
。In my quest for a universal groupwise-max, I've seen many answers and blog posts on the subject. Even my favorite (actually part of a fantastic series on the subject) failed to identify a portable solution, instead diving deep into specifics per RDMBS.
Luckily, a portable solution does exist!
The secondary index you need for this is
name
. (name, id
would be identical, as the primary key is always included implicitly.)Create groups of the
message
s, and use a dependent subquery to get the latest row for each group.This is portable because it requires only the following combination of building blocks:
GROUP BY
.SELECT
withORDER BY [ASC/DESC]
andLIMIT/TOP
.Just be sure to have the correct index:
GroupKeyColumn(s), GroupWinnerColumn(s), PrimaryKeyColumn(s)
.In OP's case, the group key is
name
, the group's winner is determined byid
, and the primary key is already covered by that, so:name, id
.Many have suggested solutions involving subqueries, but the most overlooked aspect is the highly specific set of ordering clauses that causes the correct index to be used - in the right traversal direction, no less.
Additional Advantages
ASC
) vs. max (DESC
).timestamp, id
. (This also allows us to disambiguate non-unique winners, such as "latest timestamp".)company_id, department_name
.WHERE
on which groups to select.WHERE
on what items to ignore, both indexed (id >= 1000
) and non-indexed (is_deleted = 0
).Why does this work [optimally]?
Imagine leafing through the physical phone book, finding the last entry for each town, i.e. the entry with that town's alphabetically greatest name. How would you do it?
You would start at the very end. The very last entry in the book is the group maximum of the last town. It is the first result row that you encounter.
For each subsequent desired result row, you would binary search backwards, to the next-greatest town. At the point where the current town transitions into its predecessor, there is the predecessor's last row (alphabetically greatest name), i.e. your next result row. Repeat until no more towns.
Loosely speaking, the phone book is like a secondary index on
{ Town, Name, PhoneNumber }
, withPhoneNumber
serving as the primary key. (I'm simplifying things for agument's sake, pretending phone numbers are assigned to one person and names form a single column.)You are effectively doing a reverse seek through the index. By repeatedly jumping to the next town efficiently (thanks to binary search or a B-tree structure), the work is constrained by the number of result rows rather than the total number of rows. This is asymptotically optimal. And thanks to the reverse traversal direction, each town you encounter "starts" with its greatest row, your target. That is important: imagine the absurd amount of needless work if you'd have to scan all rows for a town.
Changing the solution to a groupwise-min is as trivial as changing the traversal direction, i.e. from
DESC
toASC
.RDBMS Notes
Using index
for this, MySQL 5.7 shows a worrisomeUsing where; Using index
, but it actually performs correctly. (Tested on a huge data set involving very large groups. Tens of thousands of results spread through hundreds of millions of records were obtained in ~3 seconds.)SELECT TOP 1
instead ofSELECT ... LIMIT 1
.下面的查询将根据您的问题正常工作。
The below query will work fine as per your question.
如果您想要每个
Name
的最后一行,那么您可以通过Name
为每个行组指定行号,并按Id
排序降序排列。查询
SQL Fiddle
If you want the last row for each
Name
, then you can give a row number to each row group by theName
and order byId
in descending order.QUERY
SQL Fiddle
这是我的解决方案:
Here is my solution:
如果性能确实是您关心的问题,您可以在表中引入一个名为
IsLastInGroup
的 BIT 类型新列。在最后的列上将其设置为 true 并在每行插入/更新/删除时维护它。写入会变慢,但读取会受益。这取决于您的用例,我仅在您专注于阅读时才推荐它。
所以你的查询将如下所示:
If performance is really your concern you can introduce a new column on the table called
IsLastInGroup
of type BIT.Set it to true on the columns which are last and maintain it with every row insert/update/delete. Writes will be slower, but you'll benefit on reads. It depends on your use case and I recommend it only if you're read-focused.
So your query will look like:
MariaDB 10.3 及更高版本使用 GROUP_CONCAT。
这个想法是使用
ORDER BY
+LIMIT
:db<>fiddle 演示
MariaDB 10.3 and newer using GROUP_CONCAT.
The idea is to use
ORDER BY
+LIMIT
:db<>fiddle demo
怎么样:
我有类似的问题(在 postgresql 上很难)和 1M 记录表。该解决方案需要 1.7 秒,而使用 LEFT JOIN 的解决方案需要 44 秒。
就我而言,我必须根据 NULL 值过滤 name 字段的对应项,从而获得更好的性能 0.2 秒
How about this:
I had similar issue (on postgresql tough) and on a 1M records table. This solution takes 1.7s vs 44s produced by the one with LEFT JOIN.
In my case I had to filter the corrispondant of your name field against NULL values, resulting in even better performances by 0.2 secs
怎么样:
我已经在 sqlite 上测试了它,它返回所有列和所有名称的最大 id 值。
What about:
I have tested it on sqlite and it returns all columns and max id value for all names.
从 MySQL 8.0.14 开始,这也可以使用 横向派生表:
db<>fiddle
As of MySQL 8.0.14, this can also be achieved using Lateral Derived Tables:
db<>fiddle
没有子查询的另一种选择。
此解决方案使用 MySQL
LAST_VALUE
窗口函数,利用Window Function Frame
可从 .请在此处尝试一下。
Yet another option without subqueries.
This solution uses MySQL
LAST_VALUE
window function, exploitingWindow Function Frame
available MySQL tool from .Try it here.
希望以下Oracle查询可以帮助您:
Hope below Oracle query can help: