多次告诉我,相同的查询MariaDB可以像在MySQL上一样工作…直到遇到此问题.
最近,我正在尝试将应用程序从MysqL(InnoDB)克隆到MariaDB(XTradB).
尽管MariaDB无需更改即可运行MySQL查询,但令我惊讶的是,发现相同的查询实际上在两种平台上的行为都大不相同,尤其是在ORDER BY和GROUP BY中.
例如:
MyTable
=======
+----+----------+---------------------+-----------+
| id | parentId | creationDate | name |
+----+----------+---------------------+-----------+
| 1 | 2357 | 2017-01-01 06:03:40 | Anna |
+----+----------+---------------------+-----------+
| 2 | 5480 | 2017-01-02 07:13:20 | Becky |
+----+----------+---------------------+-----------+
| 3 | 2357 | 2017-01-03 08:20:12 | Christina |
+----+----------+---------------------+-----------+
| 4 | 2357 | 2017-01-03 08:20:15 | Dorothy |
+----+----------+---------------------+-----------+
| 5 | 5480 | 2017-01-04 09:25:45 | emma |
+----+----------+---------------------+-----------+
| 6 | 1168 | 2017-01-05 10:30:10 | Fiona |
+----+----------+---------------------+-----------+
| 7 | 5480 | 2017-01-05 10:33:23 | Gigi |
+----+----------+---------------------+-----------+
| 8 | 1168 | 2017-01-06 12:46:34 | Heidi |
+----+----------+---------------------+-----------+
| 9 | 1168 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
| 10 | 2357 | 2017-01-07 14:58:37 | Jane |
+----+----------+---------------------+-----------+
| 11 | 2357 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
基本上,我想从查询中获得的是每个GROUPING(即parentId)的最新记录.最近,我的意思是MAX(creationDate)和MAX(id)
因此,对于上面的示例,由于只有三个不同的parentId值,我希望得到:
+----+----------+---------------------+-----------+
| id | parentId | creationDate | name |
+----+----------+---------------------+-----------+
| 11 | 2357 | 2017-01-07 14:58:37 | Katy |
+----+----------+---------------------+-----------+
| 9 | 1168 | 2017-01-06 12:46:34 | Irene |
+----+----------+---------------------+-----------+
| 7 | 5480 | 2017-01-05 10:33:23 | Gigi |
+----+----------+---------------------+-----------+
最初,应用程序具有与以下方式类似的查询:
SELECT * FROM
( SELECT * FROM `MyTable` WHERE `parentId` IN (...)
ORDER BY `creationDate` DESC, `id` DESC ) AS `t`
GROUP BY `parentId`;
在MysqL上,这是可行的,因为内部查询将排序,然后外部查询从内部查询的结果中获取每个GROUP的第一个.外部查询基本上服从内部查询的顺序.
但是在MariaDB上,外部查询将忽略内部查询结果的顺序.我在MariaDB上得到了这个:
+----+----------+---------------------+-----------+
| id | parentId | creationDate | name |
+----+----------+---------------------+-----------+
| 1 | 2357 | 2017-01-01 06:03:40 | Anna |
+----+----------+---------------------+-----------+
| 2 | 5480 | 2017-01-02 07:13:20 | Becky |
+----+----------+---------------------+-----------+
| 6 | 1168 | 2017-01-05 10:30:10 | Fiona |
+----+----------+---------------------+-----------+
为了在MariaDB上实现相同的行为,我想出了类似的方法. (虽然不确定这是否正确.)
SELECT `t1`.* FROM `MyTable` `t1` LEFT JOIN `MyTable` `t2` ON (
`t1`.`parentId` = `t2`.`parentId`
AND `t2`.`parentId` IN (...)
AND `t1`.`creationDate` <= `t2`.`creationDate`
AND `t1`.`id` < `t2`.`id`)
) WHERE `t2`.`id` IS NULL;
现在的问题是…如果我要重写查询,则必须重写数百个查询…它们之间有些许差异.
我想知道这里是否有人有什么想法可以让我做出尽可能少的改变.
谢谢大家.
解决方法:
是的,这是仅链接的答案.但是这些链接都指向MariaDB网站.
这是“不兼容性”的另一个讨论:https://mariadb.com/kb/en/mariadb/group-by-trick-has-been-optimized-away/
从技术上讲,MysqL实现了对Ansi标准的扩展.很久以后,它决定删除它,所以我认为您会发现MysqL已经向MariaDB迁移.
这是进行逐组最大的“快速”方法的列表,这可能是您要尝试执行的操作:https://mariadb.com/kb/en/mariadb/groupwise-max-in-mariadb/