现在有两个大表进行left join
select count(*) from `test_a` left join `test_b` on `test_a`.`task_id` = `test_b`.`id` where `test_a`.`status` in (1, 11) and `test_b`.`city_id` = '10';
第一步:分析一下哪个表是小表,由于left join 设计循环,默认应该是小表在外,然后 将大表尽可能先执行条件 把执行的结果与小表进行join.
select count(*) from `test_a` join (select id from test_b where city_id = 10) `test_c` on `test_a`.`task_id` = `test_c`.`id` where `test_a`.`status` in (1, 11) ;
这样第一可以用到大表的索引,然后再与小表join查询,达到优化的效果
本文介绍了一种通过调整SQL查询方式来优化数据库性能的方法。通过分析两个大表进行left join操作时,如何选择合适的表作为小表,以及如何利用索引和子查询来减少查询时间,提高查询效率。
1364

被折叠的 条评论
为什么被折叠?



