业务大概是文章表(tb_article),文章阅读表(tb_article_read),现在要统计某个人阅读的记录总数。
总共1912379条数据,优化起来都是4秒左右,其中第三个性能略佳,不知道是否还有优化手段达到毫秒级。
SELECT count(r.id) FROM tb_article a join tb_article_read r on a.id = r.tb_article_id WHERE 1=1 AND r.user_id = "8f36b62d77f7493fb198f0975e04cd4b" and title like "%试%";
SELECT count(r.exec_status) FROM tb_article a join tb_article_read r on a.id = r.tb_article_id WHERE 1=1 AND r.user_id = "8f36b62d77f7493fb198f0975e04cd4b" and title like "%试%";
SELECT count(*) FROM tb_article a join tb_article_read r on a.id = r.tb_article_id WHERE 1=1 AND r.user_id = "8f36b62d77f7493fb198f0975e04cd4b" and title like "%试%";
select count(r.id) from (select id,tb_article_id from tb_article_read where user_id="8f36b62d77f7493fb198f0975e04cd4b") r join tb_article a on a.id = r.tb_article_id where title like "%试%";
select count(r.id) from (select id from tb_article where title like "%试%") a join tb_article_read r on a.id = r.tb_article_id WHERE 1=1 AND r.user_id = "8f36b62d77f7493fb198f0975e04cd4b" ;
这是结果:
优化一般从索引角度考虑。你可以通过explain sql语句 查询你的索引使用情况。
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE a null ALL PRIMARY null null null 111460 11.11 Using where
1 SIMPLE r null ref tb_article_read_notify_id,tb_article_read_user_id tb_article_read_notify_id 259
mydb.a.id 19 50.00 Using where
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632