简体   繁体   中英

Does update change the order of records in a table in PostgreSQL?

My code depends on the order of records in the table. My assumption was that a table can be considered a list so that the records maintain order. I have a small update code as shown below that will update a record at a particular index in the table.

p = pieces[index]

p.position = 0

p.save

I check the order of records before this update and after this update then i see that after the update the record that is updated is moved to the last of the list. I print Piece.all to print the list. The order is maintained in mysql but when i deploy it to heroku which uses postgre the order was not maintained so this was a surprising find for me.

Is there no guarantee of order in tables and one should not depend on the order? Please correct my misunderstanding and thanks for the clarification.

You should NEVER depend on the order in my honest opinion.

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.

MySQL tends to return rows in the order they're inserted, and this is why you see the different in behavior.

If you want them to always be returned in the order they were created, you can use Item.order("created_at")

This is too long for a comment.

You state:

My assumption was that a table can be considered a list so that the records maintain order.

This is incorrect. A table represents an unordered set. There is no inherent ordering in the table. A result set similarly lacks ordering. The only way to guarantee the ordering of a result set is to use ORDER BY in the query.

So, an update changes values in one or more columns in one or more rows. It does not change the "ordering" of rows, because they are not ordered.

Note: Under some circumstances, a query may appear to return results in a particular order. You really should not depend on this behavior, unless the query has an explicit ORDER BY .

Tables normally are unordered, and should be presumed to be unordered unless they have a CLUSTER (ed) index . That's an important piece of information because understanding clustered indexes is somewhat useful. That said, what you receive back from a query, the resultset , should be presumed to be unordered because the join-order is always undefined.

So if order matters always be explicit and use ORDER BY . Now for illustration let's have some fun.

CREATE TABLE bar ( qux serial PRIMARY KEY, asdf text );
INSERT INTO bar (asdf) ( VALUES ('z'),('x'),('g'),('a') );

Now we've got this,

SELECT * FROM BAR;
 qux | asdf 
-----+------
   1 | z
   2 | x
   3 | g
   4 | a

Now we create a CLUSTER ed index,

CREATE INDEX asdfidx ON bar (asdf);
CLUSTER bar USING asdfidx;

Now the order is guaranteed,

SELECT * FROM bar;
 qux | asdf 
-----+------
   4 | a
   3 | g
   2 | x
   1 | z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM