SELECT *, SUM(real_premier_num) AS real_premier_num, SUM(premier_num) AS premier_num,
GROUP_CONCAT(tg_time SEPARATOR '-') AS post_time, GROUP_CONCAT(low_price SEPARATOR '-')
AS low_price FROM `port_wbinfo` WHERE `city_id` = 12 AND `is_tg` = 0 GROUP BY
`hz_post_id` ORDER BY `off_time` DESC, `shelf_time` DESC, `id` DESC LIMIT 10
我创建了一个如下所示的索引,但它不起作用,我必须分组。
alter table port_wbinfo add index `city_tg` (`city_id`,`is_tg`);
答案 0 :(得分:2)
您应该将查询编写为:
SELECT hz_post_id, SUM(real_premier_num) AS real_premier_num,
SUM(premier_num) AS premier_num,
GROUP_CONCAT(tg_time SEPARATOR '-') AS post_time,
GROUP_CONCAT(low_price SEPARATOR '-') AS low_price
FROM port_info pi
WHERE city_id = 12
GROUP BY hz_post_id
ORDER BY MAX(id) DESC;
带有SELECT *
的 GROUP BY
没有意义。最新版本的MySQL - 与几乎所有其他数据库一样 - 会发出错误(使用默认设置)。
对于此查询,您可以在port_info(city_id, hz_post_id)
上使用索引。如果您愿意,可以将real_premier_num, premier_num, tg_time, low_price, id
添加到索引中,以便涵盖查询。
这可能不会产生重大改进,但覆盖指数可能有所帮助。