SELECT SQL_NO_CACHE id, title, description, category_id, city_id, address, phone, email, url, messenger, img, your_name, delete_pass, unix_time, ip, info_new, ref, marked, premium, fixed FROM `boplusru_ads` AS A RIGHT JOIN ( SELECT aid FROM ( SELECT * FROM ( SELECT SQL_SMALL_RESULT adv_id as aid, unix_time, info_new, CASE WHEN premium >= 1501051075 THEN premium ELSE 0 END as pr, sum(weight) as wi FROM `boplusru_adswordindex` LEFT JOIN `boplusru_ads` ON `boplusru_ads`.id = adv_id WHERE ((dict_id = 0 AND word_id = 17706) OR (dict_id = 0 AND word_id = 651) OR (dict_id = 0 AND word_id = 306)) AND (city_id = 17 OR city_id = -1) AND info_new = 0 GROUP BY adv_id ORDER BY wi DESC, unix_time DESC LIMIT 200 ) AS BASETBL1 ORDER BY pr DESC, wi DESC, unix_time DESC LIMIT 100 ) AS BASETBL2 LIMIT 0, 10 ) AS LST ON A.id = LST.aid Lost connection to MySQL server during query