mysql - 不确定的排序顺序 - MySQL 与 Postgres

标签 mysql sql postgresql

我有以下查询

SELECT * 
FROM table_1 
INNER JOIN table_2 ON table_1.orders = table_2.orders 
ORDER BY table_2.purchasetime;

上面的查询结果是不确定的,即当购买时间与 MySQL 手册本身的值相同时,它可能会随着不同的查询而改变。为了克服这个问题,我们在唯一列上进行排序,并将其与常规排序相结合.

客户不希望看到不同页面刷新的不同结果,因此我们专门针对 MySQL 进行了上述修复,这是不必要的,并且需要为 asc 和 desc 提供额外的复合索引。

我不确定这是否适用于 postgres。到目前为止,我无法重现该场景。如果有人可以为 postgres 回答这个问题或为我指明正确的方向,我将不胜感激。

编辑 1:排序列已编入索引。因此假设磁盘数据没有排序,但在索引(btree 数据结构)的情况下,使用 postgres 可能会出现恒定排序?

最佳答案

不,它在 PostgreSQL 中不会有什么不同(或者,事实上,在我所知道的任何其他关系数据库中)。

参见 http://www.postgresql.org/docs/9.4/static/queries-order.html :

After a query has produced an output table (after the select list has been processed) it can optionally be sorted. If sorting is not chosen, the rows will be returned in an unspecified order. The actual order in that case will depend on the scan and join plan types and the order on disk, but it must not be relied on. A particular output ordering can only be guaranteed if the sort step is explicitly chosen.

即使您偶然找到了一个 PostgreSQL 版本和索引来保证您运行的所有测试的顺序,也请不要依赖它。任何数据库升级、数据更改或 Maya 日历或月相的更改都可能突然打乱您的排序顺序。然后调试它是一个真正和可怕的颈部疼痛。

关于mysql - 不确定的排序顺序 - MySQL 与 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267809/

相关文章:

mysql - 检查最近 n 天的行是否存在

php - 只获取最新的帖子

java - postgresql "time without zone"对应哪个Java数据类型

php - 更改输入格式和数据库问题

sql - 从另一个表创建临时表,包括配置单元中的分区列

javascript - 仅当唯一时使用 knex 插入

PostgreSQL 10 使用枚举按列表分区

php - 请求大量 Twitter API 数据、服务或操作方法?

php - 将数据从 mssql 转换为 mysql 并在加载文件时出现问题

MySQL REGEXP 匹配逗号分隔列表中的零