简体   繁体   English

PHP从MySQL表中选择最大的`id`值

[英]PHP select the biggest `id` value from MySQL table

I have a table with more than 300 000 rows and I need to select the highest value for the column 'id'. 我有一个超过300 000行的表,我需要为列'id'选择最高值。 Usually, I will do like this: 通常,我会这样做:

SELECT id FROM my_table ORDER BY id DESC

... but this will cause slow queries and I don't want to use it. ...但这会导致查询速度慢,我不想使用它。 Is there a different way to solve this situation? 有没有不同的方法来解决这种情况? id is auto increment and primary key. id是自动增量和主键。

Later Edit: It seems my full code is quite bad written, as I deduct from your comments. 稍后编辑:我的完整代码似乎写得很糟糕,因为我从你的评论中扣除了。 Below I posted a sample of the code I'm working and the tables. 下面我发布了我正在使用的代码示例和表格。 Can you suggest a proper way to insert the last ID+1 of table_x in two tables (including table_x itself). 你能否建议一种正确的方法将table_x的最后一个ID + 1插入两个表中(包括table_x本身)。 I have to mention that the script will be running more than once. 我必须提到脚本将不止一次运行。

TABLE_X          TABLE_Y
------------     ----------
id_x | value     id_y | id_x
------------     ----------
   1 | A            1 | 3
   2 | B            
   3 | C

<?php
for($i=0; $i<10; $i++){
    $result_x = mysql_query('SELECT id_x FROM table_x ORDER BY id_x DESC');
    $row_x = mysql_fetch_array($result_x);

    $next = $row_x['id_x'] + 1;
    mysql_query('INSERT INTO table_x(id_x) VALUES("'.$next.'")');
    mysql_query('INSERT INTO table_y(id_x) VALUES("'.$next.'")');
}
?>

Slightly better: 稍微好一些:

SELECT id FROM my_table ORDER BY id DESC LIMIT 1

Significantly better: 显着更好:

SELECT MAX(id) FROM my_table

Here is the right code you have to use. 这是您必须使用的正确代码。

mysql_query('INSERT INTO table_x(id_x) VALUES(NULL)');
$id = mysql_insert_id();
mysql_query("INSERT INTO table_y(id_x) VALUES($id)");

Depending on the context either 视情况而定

SELECT id FROM my_table ORDER BY id DESC LIMIT 1

or mysql_insert_id() in PHP or ( SELECT LAST_INSERT_ID() ) in MySQL 或PHP中的mysql_insert_id()或MySQL中的( SELECT LAST_INSERT_ID()

As other said, you should use the MAX operator: 正如其他人所说,你应该使用MAX运算符:

SELECT MAX( id ) FROM my_table ORDER BY id DESC

As a general rule of thumb, always reduce the amount of records returned from the database . 作为一般经验法则,始终减少从数据库返回的记录数量 The database always is faster than your application program when operating on result sets. 在结果集上运行时, 数据库总是比应用程序

In case of slow queries, please give EXPLAIN a try: 如果查询速度很慢,请尝试使用EXPLAIN

EXPLAIN  SELECT id FROM my_table ORDER BY id DESC

vs.

EXPLAIN  SELECT MAX( id ) FROM my_table ORDER BY id DESC

EXPLAIN ask MySQL's query optimizer how it sees the query. EXPLAIN询问MySQL的查询优化器如何查看查询。 Look in the documentation, to learn how to read its output. 查看文档,了解如何阅读其输出。

PS: I really wonder, why you need MAX(id) . PS:我真的很想知道,为什么你需要MAX(id) Even if your application gets the value back from the database, it is useless: Another process might just during the next CPU cycle have inserted a new record - and MAX(id) isn't valid any more. 即使你的应用程序从数据库中获取了值,它也是无用的:另一个进程可能只是在下一个CPU周期中插入了一条新记录 - 而且MAX(id)不再有效。

I guess it is slow because you retrieve all 300 000 rows. 我猜它很慢,因为你检索了所有30万行。 Add LIMIT 1 to the query. LIMIT 1添加到查询中。

SELECT id FROM my_table ORDER BY id DESC LIMIT 1

Or use the MAX() operator. 或者使用MAX()运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM