如何改进长表的 SQL 间隙搜索代码?
如何改进(加速)长表(1M 行)的代码?
我有一个名为名称的表。 id列中的数据是1,2,5,7。
ID | NAME
1 | Homer
2 | Bart
5 | March
7 | Lisa
我需要从表中找到丢失的序列号。 我的 SQL 查询发现我的表中缺少序列号。 这与此处提出的问题类似。但我的解决方案不同。我期待这样的结果:
id
----
3
4
6
(3 rows)
所以,我的代码(对于 postgreSql):
SELECT series AS id
FROM generate_series(1, (SELECT ID FROM names ORDER BY ID DESC LIMIT 1), 1)
series LEFT JOIN names ON series = names.id
WHERE id IS NULL;
How I can improve (speed up) my code for long tables (1M rows)?
I have a table named names. The data in id's column is 1, 2, 5, 7.
ID | NAME
1 | Homer
2 | Bart
5 | March
7 | Lisa
I need to find the missing sequence numbers from the table.
My SQL query found the missing sequence numbers from my table.
It is similar with problem asked here. But my solution is different. I am expecting results like:
id
----
3
4
6
(3 rows)
so, my code (for postgreSql):
SELECT series AS id
FROM generate_series(1, (SELECT ID FROM names ORDER BY ID DESC LIMIT 1), 1)
series LEFT JOIN names ON series = names.id
WHERE id IS NULL;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用max(id)获得最大的
在这里
Use max(id) to get the biggest one
Result here