MySQL ver 5.1.26
我得到了错误的结果,其中包含where,order by和limit子句。 当订单使用id列时,这只是一个问题。
的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
在问题所在的生产机器上,我看到VERSION()返回5.1.26-rc-percona-log
所以在这一点上,我认为问题在于percona软件?
增加了更多:
此时,我将把它视为Percona InnoDB驱动程序中的一个错误。我放了question to their forum。作为一个直接的解决方案,我将按create_at订购。我还将研究在我的系统上升级db,看看是否有帮助。
感谢Rabbott和mdma的帮助。我也很感激帮助,我没有做些傻事,这确实是一个问题。
答案 0 :(得分:3)
可能是这个从未为您的更新版本解决的错误? http://bugs.mysql.com/bug.php?id=31001
我在本地运行5.1.42。我从上面复制并粘贴你的查询,并得到了所有正确的结果..无论是否是上面提到的错误,它听起来像一个错误,它似乎已经在一个比你更新的版本中得到修复.. < / p>
答案 1 :(得分:3)
似乎很奇怪,也许是一个错误?作为工作量,您可以明确选择 - 使用子查询选择MAX(id)并在WHERE子句中对其进行过滤。 E.g。
SELECT id, created_at FROM billing_invoices
WHERE id IN (SELECT MAX(id) FROM billing_invoices WHERE account_id=5)
答案 2 :(得分:1)
从这里开始,
似乎已在5.1.28中修复:
[2008年7月22日20:34]错误系统
推入5.1.28
但是,我在我的版本中注意到同样的问题:5.1.41-3ubuntu12.8