Personally, I tested that Postgres reorders columns and the optimizer doesn't care in which order you write the query — it will reorganize it as needed. What do you think?
sobes.tech AI
Answer from AI
PostgreSQL indeed has a powerful query optimizer that analyzes SQL and constructs an execution plan regardless of the order of columns in SELECT or conditions in WHERE. The optimizer can rearrange operations, choose indexes, and determine the order of joins for maximum efficiency.
This means that the order of columns in a query does not affect performance — the actual meaning of the query and the presence of indexes are what matter. For example, queries SELECT a, b FROM table and SELECT b, a FROM table will have the same execution plan.
However, it is important to remember that in some cases, such as when using ORDER BY or GROUP BY, the order of columns affects the result, but not the optimization. Overall, PostgreSQL's optimizer is smart enough to restructure queries for the optimal plan.