S
S
SEOD2019-11-20 17:41:59
MySQL
SEOD, 2019-11-20 17:41:59

How will a SQL query with an ORDER BY expression work?

There is a SQL query that is supposed to work under SQLite and MySQL:

SELECT id, name, col1, col2 FROM table ORDER BY col2/col1 ASC

It expression col2/col1 will recalculate at each movement of elements at sorting of an array or one-time for each line of the table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman MySQL, 2019-11-20
@SEOVirus

one-time for each row of the table
firstly, these are not arrays, and secondly, sorting does not work that way.

I
Ilya, 2019-11-20
@sarapinit

The order in which data is read from disk does not guarantee order.
I think that:
first, rows will be read and col2 / col1 will be calculated,
then sorting
and the result of the query will go to the database cache in the RAM.
Therefore, if you request in a row, the cached result will be returned.
But this is not accurate, because you need to know how the query optimizer works inside, statistics affect query execution, i.e. non-deterministic operation.
Therefore, you need to measure performance locally if this is a bottleneck. And if not, then don’t worry too much about such trifles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question