MySQL GROUP BY致查询慢千倍?Django百万行新闻表索引优化咨询
Django+MySQL百万级新闻表索引优化问题记录
基础环境与表结构
- 应用基于Django框架开发,数据库为MySQL
- 核心业务表
newsarticle存储新闻数据,单表数据量超100万行 - 表核心字段定义如下:
Field Type id int date_published datetime(6) date_retrieved datetime(6) title varchar(500) author varchar(200) content longtext source_id int online tinyint(1) main_article_of_duplicate_group tinyint(1)
已验证的基础索引方案
经多轮测试,当前性能表现最优的自建索引为:CREATE INDEX search_index ON newsarticle(date_published DESC, main_article_of_duplicate_group, source_id, online);
待优化查询与性能观测
实际执行的查询语句如下:
SELECT `newsarticle`.`id`, `newsarticle`.`url`, `newsarticle`.`date_published`, `newsarticle`.`date_retrieved`, `newsarticle`.`title`, `newsarticle`.`summary_provided`, `newsarticle`.`summary_generated`, `newsarticle`.`source_id`, COUNT(CASE WHEN `newsarticlefeedback`.`is_relevant` THEN `newsarticlefeedback`.`id` ELSE NULL END) AS `count_relevent`, COUNT(`newsarticlefeedback`.`id`) AS `count_nonrelevent`, ( SELECT U0.`is_relevant` FROM `newsarticlefeedback` U0 WHERE (U0.`news_id_id` = `newsarticle`.`id` AND U0.`user_id_id` = 27) ORDER BY U0.`created_date` DESC LIMIT 1 ) AS `is_relevant`, CASE WHEN `newsarticle`.`content` = '' THEN 0 ELSE 1 END AS `is_content`, `newsproviders_newsprovider`.`id`, `newsproviders_newsprovider`.`name_long` FROM `newsarticle` USE INDEX (SEARCH_INDEX) INNER JOIN `newsarticle_topics` ON (`newsarticle`.`id` = `newsarticle_topics`.`newsarticle_id`) LEFT OUTER JOIN `newsarticlefeedback` ON (`newsarticle`.`id` = `newsarticlefeedback`.`news_id_id`) LEFT OUTER JOIN `newsproviders_newsprovider` ON (`newsarticle`.`source_id` = `newsproviders_newsprovider`.`id`) WHERE ((1) AND `newsarticle`.`main_article_of_duplicate_group` AND `newsarticle`.`online` AND `newsarticle_topics`.`newstopic_id` = 42 AND `newsarticle`.`date_published` >= '2020-08-08 08:39:03.199488') GROUP BY `newsarticle`.`id` ORDER BY `newsarticle`.`date_published` DESC LIMIT 30
性能观测结论:
- 必须显式指定使用
search_index索引,否则查询性能会大幅下降 - 带
GROUP BY子句的当前查询耗时约1.4s - 同一条查询移除
GROUP BY子句后,耗时仅1-10ms,处于可接受范围 - 尝试在上述最优索引的不同位置追加
id字段,未获得任何性能提升
不同工具采集的执行计划差异
Django debug工具栏与MySQL Workbench对同一条查询生成的EXPLAIN执行计划不一致,但二者对应的实际查询性能基本相当。
Django debug工具栏采集的执行计划
ID SELECT_TYPE TABLE PARTITIONS TYPE POSSIBLE_KEYS KEY KEY_LEN REF ROWS FILTERED EXTRA 1 PRIMARY newsarticle_topics None ref newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,newsartic_newstopic_id_ddd996b6_fk_summarize newsartic_newstopic_id_ddd996b6_fk_summarize 4 const 312628 100.0 Using temporary; Using filesort 1 PRIMARY newsarticle None eq_ref PRIMARY,newsartic_source_id_6ea2b978_fk_summarize,newsartic_topic_id_b67ae2c9_fk_summarize,kek,last_updated,last_update,search_index,fulltext_idx_content PRIMARY 4 newstech.newsarticle_topics.newsarticle_id 1 22.69 Using where 1 PRIMARY newsarticlefeedback None ref newsartic_news_id_id_5af7594b_fk_summarize newsartic_news_id_id_5af7594b_fk_summarize 5 newstech.newsarticle_topics.newsarticle_id 1 100.0 None 1 PRIMARY newsproviders_newsprovider None eq_ref PRIMARY, PRIMARY 4 newstech.newsarticle.source_id 1 100.0 None 2 DEPENDENT SUBQUERY U0 None ref newsartic_news_id_id_5af7594b_fk_summarize,newsartic_user_id_id_fc217cfe_fk_auth_user newsartic_user_id_id_fc217cfe_fk_auth_user 5 const 1 10.0 Using where; Using filesort
MySQL Workbench采集的执行计划
id select_type table partitions type possible_keys key key_len ref rows filtered Extra 1 PRIMARY newsarticle NULL range PRIMARY,newsartic_source_id_6ea2b978_fk_,newsartic_topic_id_b67ae2c9_fk,kek,last_updated,last_update,search_index,fulltext_idx_content search_index 8 NULL 227426 81.00 Using index condition; Using MRR; Using temporary; Using filesort 1 PRIMARY newsarticle_topics NULL eq_ref newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,newsartic_newstopic_id_ddd996b6_fk newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq 8 newstech.newsarticle.id,const 1 100.00 Using index 1 PRIMARY newsarticlefeedback NULL ref newsartic_news_id_5af7594b_fk newsartic_news_id_id_5af7594b_fk 5 newstech.newsarticle.id 1 100.00 NULL 1 PRIMARY newsproviders_newsprovider NULL eq_ref PRIMARY PRIMARY 4 newstech.newsarticle.source_id 1 100.00 NULL 2 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,newsartic_user_id_id_fc217cfe_fk_auth_user newsartic_user_id_id_fc217cfe_fk_auth_user 5 const 1 10.00 Using where; Using filesort
补充测试记录
移除GROUP BY子句后的执行计划
id,select_type,table,partitions,type,possible_keys,key,key_len,ref,rows,filtered,Extra 1,SIMPLE,newsarticle,NULL,range,search_index,search_index,8,NULL,227426,81.00,"Using index condition" 1,SIMPLE,newsarticle_topics,NULL,eq_ref,"newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,newsartic_newstopic_id_ddd996b6_fk",newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,8,"newstech.newsarticle.id,const",1,100.00,"Using index" 1,SIMPLE,newsarticlefeedback,NULL,ref,newsartic_news_id_id_5af7594b_fk,newsartic_news_id_id_5af7594b_fk,5,newstech.newsarticle.id,1,100.00,"Using index" 1,SIMPLE,newsproviders_newsprovider,NULL,eq_ref,"PRIMARY,",PRIMARY,4,newstech.newsarticle.source_id,1,100.00,NULL
按调整建议优化索引后的测试结果
已完成的调整操作:
- 为
newsarticle表更换索引为(id, online, main_article_of_duplicate_group, date_published) - 为关联表
newsarticle_topics新增两个联合索引:(newstopic_id, newsarticle_id)、(newsarticle_id, newstopic_id)
测试结果:
- 显式指定使用调整后索引的查询耗时1.2s,对应执行计划:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra 1 PRIMARY newsarticle_topics NULL ref newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,opposite opposite 4 const 346286 100.00 Using index; Using temporary; Using filesort 1 PRIMARY newsarticle NULL ref search_index search_index 4 newstech.newsarticle_topics.newsarticle_id 1 27.00 Using index condition 1 PRIMARY newsproviders_newsprovider NULL eq_ref PRIMARY,filter_index PRIMARY 4 newstech.newsarticle.source_id 1 100.00 NULL 4 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index feedback_index 5 newstech.newsarticle.id 1 100.00 Using filesort 3 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index newsartic_news_id_id_5af7594b_fk 5 newstech.newsarticle.id 1 10.00 Using where 2 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index newsartic_news_id_id_5af7594b_fk 5 newstech.newsarticle.id 1 90.00 Using where
- 不指定强制索引的查询耗时2.6s,对应执行计划:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra 1 PRIMARY newsarticle_topics NULL ref newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,opposite opposite 4 const 346286 100.00 Using index; Using temporary; Using filesort 1 PRIMARY newsarticle NULL eq_ref PRIMARY,search_index PRIMARY 4 newstech.newsarticle_topics.newsarticle_id 1 27.00 Using where 1 PRIMARY newsproviders_newsprovider NULL eq_ref PRIMARY,filter_index PRIMARY 4 newstech.newsarticle.source_id 1 100.00 NULL 4 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index feedback_index 5 newstech.newsarticle.id 1 100.00 Using filesort 3 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index newsartic_news_id_id_5af7594b_fk 5 newstech.newsarticle.id 1 10.00 Using where 2 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index newsartic_news_id_id_5af7594b_fk 5 newstech.newsarticle.id 1 90.00 Using where
- 对比基准:使用原有索引
newsarticle(date_published DESC, main_article_of_duplicate_group, source_id, online)并显式指定时,查询仅耗时1-3ms,对应执行计划:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra 1 PRIMARY newsarticle NULL range search_index search_index 8 NULL 238876 81.00 Using index condition 1 PRIMARY newsproviders_newsprovider NULL eq_ref PRIMARY,filter_index PRIMARY 4 newstech.newsarticle.source_id 1 100.00 NULL 1 PRIMARY newsarticle_topics NULL eq_ref newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq,opposite newsarticle_t_newsarticle_id_newstopic_6b1123b3_uniq 8 newstech.newsarticle.id,const 1 100.00 Using index 4 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index feedback_index 5 newstech.newsarticle.id 1 100.00 Using filesort 3 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index feedback_index 6 newstech.newsarticle.id,const 1 100.00 Using index 2 DEPENDENT SUBQUERY U0 NULL ref newsartic_news_id_id_5af7594b_fk,feedback_index newsartic_news_id_id_5af7594b_fk 5 newstech.newsarticle.id 1 90.00 Using where; Using index
内容的提问来源于stack exchange,提问作者Bob
相关产品推荐
相关产品推荐

