11

When selecting columns from a MySQL table, is performance affected by the order that you select the columns as compared to their order in the table (not considering indexes that may cover the columns)?

For example, you have a table with rows uid, name, bday, and you have the following query.

SELECT uid, name, bday FROM table

Does MySQL see the following query any differently and thus cause any sort of performance hit?

SELECT uid, bday, name FROM table
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
James Simpson
  • 13,488
  • 26
  • 83
  • 108

3 Answers3

7

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.

futureal
  • 3,025
  • 1
  • 22
  • 33
2

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

Wazy
  • 8,822
  • 10
  • 53
  • 98
0

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.

Jé Queue
  • 10,359
  • 13
  • 53
  • 61