Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does postgres order the results when ORDER BY is not provided

Let's say I have two tables:

user (user_name varchar(50), project_name varchar(50))
project (project_name varchar(50), project_cost(integer))

I have a query that's returns me results which are "de-facto desired" :

select u.user_name, p.project_name 
from user u, project p 
where u.project_name = p.project_name

Postgres says that order of rows is not predictable when ORDER BY is not given. But yet in my local test, postgres returns rows in a same order for repeated tests.

Could you please help me understand what Postgres really does when order by is not provided in the query?

I don't have access to all the possible places where the real table and schema a live, so I really need to know what really happens in order to keep existing ordering intact.

like image 575
Mecon Avatar asked May 04 '15 19:05

Mecon


People also ask

How does Postgres ORDER BY default?

The ORDER BY clause in PostgreSQL is used together with the SELECT statement to sort table data. The table data can either be sorted in ascending or descending order. By default, the data is sorted in ascending order.

Does Postgres guarantee order?

The only order that is guaranteed is the order that your "ORDER BY" imposes. If there are permutations possible within that order these could all be a valid output.

Can we use group by without ORDER BY?

The ORDER BY clause is not strictly necessary. It's up to you to decide whether you want the result set ordered. However, given the whole point of GROUP BY , the following is recommended: You probably want the data displayed in the same order as per the GROUP BY clause, to make the grouping more apparent.

Does column order matter in PostgreSQL?

The order of columns doesn't matter in creating tables in PostgreSQL, but it does matter sometimes in creating indexes in PostgreSQL. PostgreSQL implements primary keys with an underlying unique index.


2 Answers

If no order by clause is given, postgres (and any other reasonable database, for that sake), should return the rows in the order it was able to produce them (be it from an internal cache, an index, or directly from the table).

Since the same algorithm is used on the same data, it isn't surprising you're getting the same rows in the same order. However, this does not mean you should rely on this ordering. If you do something to change the data's layout on the disk (e.g., back it up and restore it, or even rebuild the tables' indexes), you're likely to get a different ordering.

like image 99
Mureinik Avatar answered Nov 14 '22 23:11

Mureinik


To know what really DBMS does one should look at the PLAN. The output order will depend upon it too. However there are two things to remember: first, if the plan includes 'full (heap) table scan' then the order is undefined (as DBMS may freely reorder heap data); second, the plan may change significantly if you change your SQL statement or update DB stats. This is why you should not rely on output order's stability in a long run.

like image 40
Matt Avatar answered Nov 14 '22 21:11

Matt