Postgres default sort by id - worldship

后端 未结 4 1826
粉色の甜心
粉色の甜心 2020-12-10 01:52

I need to setup worldship to pull from one of our postgres databases. I need to have it so that the packages are sorted by id. I have no way (that i am aware of) of having w

相关标签:
4条回答
  • 2020-12-10 02:06

    For what it's worth, which probably isn't much, PostgreSQL's "default" ordering is based on the time the records were last updated. The most recently updated records will appear last.

    0 讨论(0)
  • 2020-12-10 02:12

    Rows are returned in an unspecified order, per sql specs, unless you add an order by clause. In Postgres, that means you'll get rows in, basically, the order that live rows read on the disk.

    If you want a consistent order without needing to add an order by clause, create a view as suggested in Jack's comment.

    0 讨论(0)
  • 2020-12-10 02:16

    You could eventually use a sorted index, which should guarantee you order of retrieved rows in case the query plan hits the index, or if you force it, but this approach will be more than circuitous :). ORDER BY clause is the way to go as mentioned already.

    0 讨论(0)
  • 2020-12-10 02:30

    There is no such thing as a "default sort". Rows in a table are not sorted.

    You could fake this with a view (as suggested by Jack Maney) there is no way you can influence the order of the rows that are returned.

    But if you do that, be aware that adding an additional ORDER BY to a SELECT based on that view will sort the data twice.

    Another option might be to run the CLUSTER command on that table to physically order the rows on the disk according to the column you want. But this sill does not guarantee that the rows are returned in that order. Not even with a plain SELECT * FROM your_table (but chances are reasonably high for that). You will need to re-run this statement on a regular basis because the order created by the CLUSTER command is not automatically maintained.

    0 讨论(0)
提交回复
热议问题