M
M
mihalich2013-05-13 09:54:35
MySQL
mihalich, 2013-05-13 09:54:35

Long queries with sorting (2-5 sec instead of 0.1-03)

Queries like this take a long time.

SELECT SQL_NO_CACHE news.*, news_category.name AS category FROM news, news_category WHERE news.news_category_id = news_category.id AND news_category.id != 114 AND create_date < CURRENT_DATE AND FIND_IN_SET(50, news.region_ids) AND news.active_status = 1 AND news_category.active_status = 1 ORDER BY id DESC LIMIT 3

And even so, adding sorting to any query leads to a disproportionate increase in its execution time.

Tables - MyISAM

What could be wrong?
Just some kind of stupor.

Settings here - www.cottage.ru/mysql-params.txt (utf-8)

Server php-fpm+nginx

Answer the question

In order to leave comments, you need to log in

5 answer(s)
H
himik, 2013-05-13
@himik

what does EXPLAIN say?

G
Grigory Peretyaka, 2013-05-13
@Peretyaka

Sorting is a heavy operation, what surprises you? Use inner join, try to join already filtered data like this:
SELECT SQL_NO_CACHE news.*, news_category.name AS category FROM news
INNER JOIN news_category ON (news.news_category_id = news_category.id AND news_category.id != 114 AND news_category. active_status = 1)
WHERE AND create_date < CURRENT_DATE AND FIND_IN_SET(50, news.region_ids) AND news.active_status = 1 ORDER
BY id DESC LIMIT 3

G
gaelpa, 2013-05-13
@gaelpa

So that sorting does not slow down queries, it is necessary that an index be available, starting with the fields by which sorting is performed and optionally continuing with filtering fields. Do you have one?

N
NeX, 2013-05-13
@NeX

Try increasing sort_buffer_size and show dev.mysql.com/doc/refman/5.6/en/show-profile.html for query

M
mihalich, 2013-05-15
@mihalich

As a result, they solved the problem like this - they moved tmdir (temporary directory) to the RAM, and everything worked out.
Query profiling showed that most of the time is taken by Copying to tmp table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question