Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 迁移mysql库到tidb,使用 group by 语句如果不加 order by ,查出来的数据是乱序的
【TiDB Usage Environment】Production Environment
【TiDB Version】v4.0.8
【Encountered Problem】
After migrating the MySQL database to TiDB, since the business previously used MySQL and all query statements used GROUP BY
without adding ORDER BY
, the results in MySQL would always remain the same. However, in TiDB, the order of the query results changes every time. This causes the business statistics displayed on the web page to be disordered. (Because our old project SQLs do not have ORDER BY
, modifying the business SQLs would be too costly, requiring changes to hundreds of statements.)
【Reproduction Path】
【Help Needed】
Is there a way to use GROUP BY
in TiDB without adding ORDER BY
(since modifying hundreds of SQL statements in the business is too costly) and still have the results sorted in the same order as in MySQL, instead of having the results in a different order each time?
【Problem Phenomenon and Impact】
Comparison of two query results using GROUP BY
in TiDB, showing the order has changed:
First query using TiDB:
Second query using TiDB:
Query results using MySQL, showing automatic sorting by the timestamp
field: