代码之家  ›  专栏  ›  技术社区  ›  Larry K

mysql:order by and limit给出错误的结果

  •  8
  • Larry K  · 技术社区  · 14 年前

    MySQL 5.1.26版

    我选了一个包含where、order by和limit子句的选项,结果是错误的。 只有当order by使用id列时才有问题。

    我看过mysql手册 LIMIT Optimization

    我从阅读手册中推测,主键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 |
    +----+-------------+------------------+-------+--------------------------------------+--------------------------------------+---------+------+------+-------------+
    

    添加显示创建表开票\发票结果:

    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
    

    添加更多:

    我现在看到在我的开发机器上,一切都正常工作。那台机器的版本是5.1.26-rc-log

    在我身上 生产 machine,在问题所在,我看到version()返回5.1.26-rc-percona-log

    所以现在,我想问题出在Percona软件上?

    添加更多:

    在这一点上,我认为它是Percona InnoDB驱动程序中的一个bug。我放了一个 question to their forum . 作为一项即时的工作,我将由Created_at订购。我还将研究升级我的系统上的数据库,看看是否有帮助。

    感谢Rabbott和MDMA的帮助。我也感谢你的帮助,我没有做傻事,这真的是个问题。

    3 回复  |  直到 13 年前
        1
  •  3
  •   Rabbott    14 年前

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

    我在本地运行5.1.42。我从上面复制并粘贴您的查询,并获得所有正确的结果。不管是不是上面提到的bug,听起来都像是bug,而且似乎是在比您的更新版本中修复的。

        2
  •  3
  •   mdma    14 年前

    看起来很奇怪,也许是虫子?作为一种解决方法,您可以使选择显式化——使用子查询来选择max(id),并在WHERE子句中对其进行筛选。例如。

    SELECT id, created_at FROM billing_invoices 
       WHERE id IN (SELECT MAX(id) FROM billing_invoices WHERE account_id=5)
    
        3
  •  1
  •   Chandra Sekhar    13 年前

    从这里,

    Bug Details

    它似乎固定在5.1.28中:

    [2008年7月22日20:34]Bug系统

    推入 5.1.28

    但是,我注意到我的版本中有同样的问题: 5.1.41-3ubuntu12.8