Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does column order matter in PostgreSQL tables? [duplicate]

Tags:

postgresql

For example, I have table users(id, username, email).

Is there any difference between following queries ? (maybe performance, or anything else)

SELECT id, username, email FROM users

and

SELECT id, email, username FROM users

Generally, does column order matter creating PostgreSQL tables ?

like image 364
Teimuraz Avatar asked Dec 24 '15 11:12

Teimuraz


People also ask

Does column order matter in table?

OK, for years I've been saying that SQL Server doesn't care about the order in which you define the columns of your table because internally SQL Server will re-arrange your columns to store all of the fixed width columns first and the variable columns last.

Does ordering of column or row in a table matters?

column order does not matter. This is purely a convenience feature. just to allow you to restructure your database table the way you like after it has been created.

Does the column order matter in the DB Multi column index?

Whereas if the telephone book were organized by first name then by last name, you'd find all the Johns together, then within the Johns, all the 'S' last names would be grouped together. So the order of columns in a multi-column index definitely matters.

Does the order of columns matter in a relation?

No, column order is not significant.


2 Answers

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. If the primary key contains more than one column, then PostgreSQL creates the underlying index with columns in the order you declare them. Some queries against part of that primary key will be able to use the index; some will not.

This quote is from the docs on multicolumn indexes.

A multicolumn B-tree index can be used with query conditions that involve any subset of the index's columns, but the index is most efficient when there are constraints on the leading (leftmost) columns. The exact rule is that equality constraints on leading columns, plus any inequality constraints on the first column that does not have an equality constraint, will be used to limit the portion of the index that is scanned. Constraints on columns to the right of these columns are checked in the index, so they save visits to the table proper, but they do not reduce the portion of the index that has to be scanned. For example, given an index on (a, b, c) and a query condition WHERE a = 5 AND b >= 42 AND c < 77, the index would have to be scanned from the first entry with a = 5 and b = 42 up through the last entry with a = 5. Index entries with c >= 77 would be skipped, but they'd still have to be scanned through. This index could in principle be used for queries that have constraints on b and/or c with no constraint on a — but the entire index would have to be scanned, so in most cases the planner would prefer a sequential table scan over using the index.

like image 67
Mike Sherrill 'Cat Recall' Avatar answered Oct 03 '22 06:10

Mike Sherrill 'Cat Recall'


It does matter... slightly sometimes. There is something like data structure alignment. If you start using a mixture of types that are less than 4 bytes (smallint, char(1) etc), your table might get bigger if you interleave them. If you do use types that are at least 4 bytes, the order does not matter.

There is a good read https://www.2ndquadrant.com/en/blog/on-rocks-and-sand/

like image 41
mlt Avatar answered Oct 03 '22 05:10

mlt