mysql 从最后 n 行中选择

发布于 2024-07-13 19:39:21 字数 78 浏览 3 评论 0原文

我有一个带有索引(自动增量)和整数值的表。 该表有数百万行长。

如何最有效地搜索某个数字是否出现在表的最后 n 行中?

I have a table with index (autoincrement) and integer value. The table is millions of rows long.

How can I search if a certain number appear in the last n rows of the table most efficiently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

穿透光 2024-07-20 19:39:21

从@chaos给出的答案开始,但有一个一些修改:

  • 如果您使用LIMIT,则应始终使用ORDER BY。 RDBMS 表不保证隐式顺序。 您通常可以按主键的顺序获取行,但您不能依赖于此,也不能移植。

  • 如果按降序排序,则无需事先知道表中的行数。

  • 您必须为派生表提供一个关联名称(也称为表别名)。

这是我的查询版本:

SELECT `id`
FROM (
    SELECT `id`, `val`
    FROM `big_table`
    ORDER BY `id` DESC
    LIMIT $n
) AS t
WHERE t.`val` = $certain_number;

Starting from the answer given by @chaos, but with a few modifications:

  • You should always use ORDER BY if you use LIMIT. There is no implicit order guaranteed for an RDBMS table. You may usually get rows in the order of the primary key, but you can't rely on this, nor is it portable.

  • If you order by in the descending order, you don't need to know the number of rows in the table beforehand.

  • You must give a correlation name (aka table alias) to a derived table.

Here's my version of the query:

SELECT `id`
FROM (
    SELECT `id`, `val`
    FROM `big_table`
    ORDER BY `id` DESC
    LIMIT $n
) AS t
WHERE t.`val` = $certain_number;
甜妞爱困 2024-07-20 19:39:21

可能是一个很晚的答案,但这很好而且简单。

select * from table_name order by id desc limit 5

此查询将返回您在表中插入的一组最后 5 个值(最后 5 行)

Might be a very late answer, but this is good and simple.

select * from table_name order by id desc limit 5

This query will return a set of last 5 values(last 5 rows) you 've inserted in your table

壹場煙雨 2024-07-20 19:39:21

最后 5 行在 mysql 中检索

此查询工作正常

SELECT * FROM (SELECT * FROM recharge ORDER BY sno DESC LIMIT 5)sub ORDER BY sno ASC

select sno from(select sno from recharge order by sno desc limit 5) as t where t.sno order by t.sno asc

Last 5 rows retrieve in mysql

This query working perfectly

SELECT * FROM (SELECT * FROM recharge ORDER BY sno DESC LIMIT 5)sub ORDER BY sno ASC

or

select sno from(select sno from recharge order by sno desc limit 5) as t where t.sno order by t.sno asc
沧笙踏歌 2024-07-20 19:39:21

像使用分页一样利用 SORT 和 LIMIT。 如果您想要第 i 个行块,请使用 OFFSET。

SELECT val FROM big_table
where val = someval
ORDER BY id DESC
LIMIT n;

回复尼尔:
排序操作不一定会受到惩罚,这取决于查询规划器的操作。 由于此用例对于分页性能至关重要,因此需要进行一些优化(请参阅上面的链接)。 这在 postgres 中也是如此“ORDER BY ... LIMIT 可以在不排序的情况下完成” E.7.1。 最后一个项目符号

explain extended select id from items where val = 48 order by id desc limit 10;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | items | const | PRIMARY       | PRIMARY | 4       | const |    1 | Using index | 
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+

Take advantage of SORT and LIMIT as you would with pagination. If you want the ith block of rows, use OFFSET.

SELECT val FROM big_table
where val = someval
ORDER BY id DESC
LIMIT n;

In response to Nir:
The sort operation is not necessarily penalized, this depends on what the query planner does. Since this use case is crucial for pagination performance, there are some optimizations (see link above). This is true in postgres as well "ORDER BY ... LIMIT can be done without sorting " E.7.1. Last bullet

explain extended select id from items where val = 48 order by id desc limit 10;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | items | const | PRIMARY       | PRIMARY | 4       | const |    1 | Using index | 
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
兮颜 2024-07-20 19:39:21

因为它是自动增量,所以这是我的看法:

Select * from tbl 
where certainconditionshere 
and autoincfield >= (select max(autoincfield) from tbl) - $n

because it is autoincrement, here's my take:

Select * from tbl 
where certainconditionshere 
and autoincfield >= (select max(autoincfield) from tbl) - $n
注定孤独终老 2024-07-20 19:39:21

我知道这可能有点旧,但请尝试使用 PDO::lastInsertId。 我认为它可以实现您想要的功能,但是您必须重写应用程序才能使用 PDO(这对于攻击来说更安全)

I know this may be a bit old, but try using PDO::lastInsertId. I think it does what you want it to, but you would have to rewrite your application to use PDO (Which is a lot safer against attacks)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文