A
A
Alexander Gamov2019-08-06 17:27:14
Laravel
Alexander Gamov, 2019-08-06 17:27:14

Why does the application take a second longer when changing the sort?

There is a service in which sorting is added.

switch ($request->get('sort')) {
    case 'rating':
        $orderedQuery = $rawQuery->orderBy('rating', 'DESC');
        break;
    case 'price':
        $orderedQuery = $rawQuery->orderBy('min_price', 'ASC');
        break;
    default:
        $orderedQuery = $rawQuery->orderBy('internal_rating', 'DESC');
}

All these fields have an index and the same type (double precision);
When choosing the first two, the entire query takes 0.3 - 0.5s, with the last one - from 1.7s.
Moreover, the query execution time does not differ for them - on average 0.1 - 0.2s (according to the debug bar), only in the case of `internal_rating` the application works longer.
UPD: and now the most interesting. internal_rating contains
numbers on average from 50 - 2000 (~500)
rating, in turn, from 1 to 5 (on a five-point scale) . I draw a preliminary conclusion that sorting by large numbers in this case takes longer (I remind you that both fields
UPDATE doctors SET internal_rating=random();
double precision), and also that the debugbar is blatantly lying about the runtime. It's hard to believe in the second, I'll try to dig more.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Gamov, 2019-08-07
@slowdream

The problem turned out to be serialization. Models with a higher rating had much more additional data (which is quite logical).

V
Vyacheslav Plisko, 2019-08-07
@AmdY

Copy the query and do EXPLAIN, compare the number of scanned rows, the use of indexes and their selectivity. Look at the number of requests and how much data they return.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question