gpt4 book ai didi

php - 我的 sql 查询有改进吗?

转载 作者:行者123 更新时间:2023-11-30 23:38:10 25 4
gpt4 key购买 nike

是否有任何方法可以用其他东西删除 MAX,也许是 ASC LIMIT 1 以减少数据库限制?我的查询获取最大 ID 并将其加 1。

这是我的查询:

$query = 'SELECT MAX(ID) +1 as maxidpost  
FROM wp_posts';
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo 'p='. $row['maxidpost'];
}
mysql_close();

最佳答案

关于您的查询,数据库告诉您什么?如果 id 被索引,那么

mysql> explain select max(id) + 1 from times;
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.18 sec)

mysql> explain select id from times order by id ASC limit 1;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| 1 | SIMPLE | times | index | NULL | PRIMARY | 4 | NULL | 1 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

关于php - 我的 sql 查询有改进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com