gpt4 book ai didi

php - MySQL 没有正确选择行(有时)

转载 作者:可可西里 更新时间:2023-11-01 06:33:43 25 4
gpt4 key购买 nike

这是对这个问题的更新,我在其中四处寻找,试图弄清楚到底发生了什么:

MySQL sometimes erroneously returns 0 for count(*)

我最终接受了那里的答案,因为它确实回答了我提出的问题(“为什么会发生这种情况”),即使它没有回答我真正想知道的问题(“为什么这会发生在我身上” ). 但是我已经设法将后一个问题的范围缩小了一点,并且我认为我可以明确地说我不理解也从未见过的地方出了问题。

这个问题真的很难调试,因为出于我无法理解的原因,登录数据库会自动修复它。但是,今天我在终端中打开 MySQL session 时设法触发了有问题的状态。以下是该 session 中的一些查询和后续响应:

首先,这是我的表格布局:

mysql> describe forum_posts;                                                    
+-----------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+----------------+
| post_id | int(11) | NO | PRI | NULL | auto_increment |
| thread_id | int(11) | YES | MUL | NULL | |
| forum_id | int(11) | YES | MUL | NULL | |
| user_id | int(11) | YES | MUL | NULL | |
| moderator | tinyint(1) | NO | | 0 | |
| message | mediumtext | YES | MUL | NULL | |
| date | int(11) | NO | MUL | NULL | |
| edited | int(11) | YES | | NULL | |
| deleted | tinyint(1) | YES | MUL | 0 | |
| bbcode | tinyint(1) | NO | | 1 | |
+-----------+------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

现在,让我们看看给定的论坛帖子中有多少帖子:

mysql> SELECT count(post_id) as num FROM `forum_posts` where thread_id=5243;
+-----+
| num |
+-----+
| 195 |
+-----+
1 row in set (0.00 sec)

好的,但我只想要没有设置 deleted 标志的论坛帖子:

mysql> SELECT count(post_id) as num FROM `forum_posts` where thread_id=5243 and deleted=0;
+-----+
| num |
+-----+
| 0 |
+-----+
1 row in set (0.06 sec)

mysql> select post_id,deleted from forum_posts where thread_id=5243 and deleted=0;
Empty set (0.06 sec)

好的,让我们再次确保它们实际上并没有全部删除:

mysql> select post_id,deleted from forum_posts where thread_id=5243;
+---------+---------+
| post_id | deleted |
+---------+---------+
| 104081 | 0 |
| 104082 | 0 |

[snip]

| 121162 | 0 |
| 121594 | 0 |
+---------+---------+
195 rows in set (0.00 sec)

该表中的每一行都将“已删除”设置为 0,但向查询添加 and deleted=0 不会产生任何结果。直到我通过从终端窗口再次登录到 MySQL 打开一个新 session ,之后我可以再次正确选择“已删除”为 0 的行。

到底是什么?


更新:

@miken32 在下面的评论中建议我尝试 EXPLAIN SELECT ...,所以:

mysql> explain select post_id,deleted from forum_posts where thread_id='5243' and deleted=0;
+----+-------------+-------------+-------------+-------------------+-------------------+---------+------+------+--------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+-------------+-------------------+-------------------+---------+------+------+--------------------------------------------------------------+
| 1 | SIMPLE | forum_posts | index_merge | thread_id,deleted | thread_id,deleted | 5,2 | NULL | 97 | Using intersect(thread_id,deleted); Using where; Using index |
+----+-------------+-------------+-------------+-------------------+-------------------+---------+------+------+--------------------------------------------------------------+
1 row in set (0.00 sec)

最佳答案

根据使用 FORCE KEY 改变查询结果的评论,我们很可能正在处理合并优化器错误。原始查询的 EXPLAIN 显示优化是通过从已删除的键中选择,然后从 post_id 键中选择,然后合并结果来完成的。当我们强制绕过该代码时,问题就消失了。

起点的步骤:

  • 使用最新的 5.6 版 MySQL 在相同数据上尝试
  • 如果问题重现,请尝试将其隔离到最小的测试用例,访问 http://bugs.mysql.com/并报告错误

关于php - MySQL 没有正确选择行(有时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588696/

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