sql - MySQL:排序依据和限制给出错误的结果

标签 sql mysql

MySQL 版本 5.1.26

我在使用包含 where、order by 和 limit 子句的选择时得到了错误的结果。 只有当 order by 使用 id 列时才会出现问题。

我看到 LIMIT Optimization 的 MySQL 手册

我阅读手册后的猜测是主键 id 上的索引存在一些问题。但是我不知道我应该从这里去哪里......

问题:我应该怎么做才能最好地解决这个问题?

Works correctly:
mysql> SELECT id, created_at FROM billing_invoices 
       WHERE (billing_invoices.account_id = 5) ORDER BY id DESC ;
+------+---------------------+
| id   | created_at          |
+------+---------------------+
| 1336 | 2010-05-14 08:05:25 |
| 1334 | 2010-05-06 08:05:25 |
| 1331 | 2010-05-05 23:18:11 |
+------+---------------------+
3 rows in set (0.00 sec)

WRONG result when limit added! Should be the first row, id - 1336
mysql> SELECT id, created_at FROM billing_invoices 
       WHERE (billing_invoices.account_id = 5) ORDER BY id DESC limit 1;
+------+---------------------+
| id   | created_at          |
+------+---------------------+
| 1331 | 2010-05-05 23:18:11 |
+------+---------------------+
1 row in set (0.00 sec)

Works correctly:
mysql> SELECT id, created_at FROM billing_invoices 
       WHERE (billing_invoices.account_id = 5) ORDER BY created_at DESC ; 
+------+---------------------+
| id   | created_at          |
+------+---------------------+
| 1336 | 2010-05-14 08:05:25 |
| 1334 | 2010-05-06 08:05:25 |
| 1331 | 2010-05-05 23:18:11 |
+------+---------------------+
3 rows in set (0.01 sec)

Works correctly with limit:
mysql> SELECT id, created_at FROM billing_invoices 
       WHERE (billing_invoices.account_id = 5) ORDER BY created_at DESC limit 1;
+------+---------------------+
| id   | created_at          |
+------+---------------------+
| 1336 | 2010-05-14 08:05:25 |
+------+---------------------+
1 row in set (0.01 sec)

Additional info:
explain SELECT id, created_at FROM billing_invoices WHERE (billing_invoices.account_id = 5) ORDER BY id DESC limit 1;
+----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+
| id | select_type | table            | type  | possible_keys                        | key                                  | key_len | ref  | rows | Extra       |
+----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+
|  1 | SIMPLE      | billing_invoices | range | index_billing_invoices_on_account_id | index_billing_invoices_on_account_id | 4       | NULL |    3 | Using where |
+----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+

添加了 SHOW CREATE TABLE billing_invoices 结果:

Table -- billing_invoices
Create Table --
CREATE TABLE `billing_invoices` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) NOT NULL,
  `invoice_date` date NOT NULL,
  `prior_invoice_id` int(11) DEFAULT NULL,
  `closing_balance` decimal(8,2) NOT NULL,
  `note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `monthly_invoice` tinyint(1) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_billing_invoices_on_account_id` (`account_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1337 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

添加了更多:

我现在看到在我的开发机器上一切正常。那台机器的版本 VERSION() 为 5.1.26-rc-log

在我的 production 机器上,问题出在哪里,我看到 VERSION() 返回 5.1.26-rc-percona-log

所以在这一点上,我认为问题出在 percona 软件上?

添加了更多:

在这一点上,我打算将其视为 Percona InnoDB 驱动程序中的错误。我放了一个 question to their forum .作为一种直接的解决方法,我将按 created_at 进行订购。我还将调查升级我系统上的数据库,看看是否有帮助。

感谢 Rabbott 和 mdma 的帮助。我也很感谢您的帮助,我没有做傻事,这确实是个问题。

最佳答案

这个错误可能是您的更新版本从未解决过的吗? http://bugs.mysql.com/bug.php?id=31001

我在本地运行 5.1.42。我从上面复制并粘贴了您的查询,并得到了所有正确的结果。无论是否是上面提到的错误,这听起来像是一个错误,而且它似乎已在比您的版本更新的版本中得到修复..

关于sql - MySQL:排序依据和限制给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2844699/

相关文章:

mysql - 从 3 个左连接选择查询非常慢

php - 如何使用数组的多个键/值进行 SQL 查询?

php - 单击并在 sql 查询中使用时获取值

即使在上一步中删除了外键,MySQL 脚本也无法删除表

mysql - 修改 SQL 查询以获取实际返回产品的数量

sql - 在go中参数化sql查询

mysql - 在查找另一个表后从表中删除 - SQL

php - Web 开发,PHP 将 SQL 中的变量显示为其他内容

php - 试图找到两个日期之间的记录

mysql - SQL Server 转储到 MySQL