Does the order of columns in a query matter? Does the order of columns in a query matter? sql sql

Does the order of columns in a query matter?


The order doesn't matter, actually, so you are free to order them however you'd like.

edit: I guess a bit more background is helpful: As far as I know, the process of optimizing any query happens prior to determining exactly what subset of the row data is being pulled. So the query optimizer breaks it down into first what table to look at, joins to perform, indexes to use, aggregates to apply, etc., and then retrieves that dataset. The column ordering happens between the data pull and the formation of the result set, so the data actually "arrives" as ordered by the database, and is then reordered as it is returned to your application.


In practice, I suspect it might.

With a decent query optimiser: it shouldn't.

You can only tell for your cases by measuring. And the measurements will likely change as the distribution of data changes in the database.

with regards

Wazzy


The order of the attributes selected is negligible. The underlying storage engines surely order their attribute locations, but you would not necessarily have a way to know the specific ordering (renames, alter tables, row vs. column stores) in most cases may be independent from the table description which is just meta data anyway. The order of presentation into the result set would be insignificant in terms of any measurable overhead.