Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is SQL SELECT faster if called in right order?

Tags:

sql

database

Assume we have a table:

|   col1   |   col2   |   ....  |  coln   |
-------------------------------------------
|    -     |    -     |    -    |    -    | 
|    -     |    -     |    -    |    -    | 
|    -     |    -     |    -    |    -    | 
-------------------------------------------

a query where the columns are ordered:

$query1 = "SELECT col1, col2, col3, col4, col5, ..., coln FROM table";

and a query where the columns are unordered:

$query2 = "SELECT col180, col1, col78, col13, col930, col2 FROM table";

Is one of the queries faster than the other? Why would it be faster? Or, why isn't one faster?

like image 537
qsi Avatar asked Apr 15 '14 12:04

qsi


People also ask

Does order matter in SQL SELECT?

Yes, order matters.

Does order by speed up SQL?

Index the SQL ORDER BY Column(s) Indexes are all about quick searches. And having one in the columns you use in the ORDER BY clause can speed up your query.

Is SELECT * faster than selecting columns?

For your question just use SELECT *. If you need all the columns there's no performance difference.


1 Answers

It should make a very small impact on performance. Here is a previous answer by spencer7593 that explains in detail:

The order of columns in a table will have a very small impact on performance, as compared to the performance impact of your database design (entities, attributes and relationships), your transaction design and your query design.

like image 85
AhDev Avatar answered Nov 08 '22 01:11

AhDev