看到数据库报警,查看慢 sql 日志看到
select id, content from table_name where status = 1 order by id asc limit 1328000, 1000
执行了 90 秒,扫描了 1329172 行。
改写成
select a2.id, a2.content from (select id from table_name where status = 1 order by id asc limit 1328000, 1000) a1, table_name a2 where a1.id=a2.id;
总结
这种数据量很大的表,应该是先做一个子查询查出 id(只会在索引里面扫描),然后关联查询,这样扫描的行数是限定的。而不会扫描表前面所有的行。
