MYSQL中1000万条数据你是怎么查询的,查询非常慢怎么优化
•
数据库
select count(*) from user_login_log; select * from user_login_log limit 10000,100; select * from user_login_log limit 10000,1000; select * from user_login_log limit 10000,10000; select * from user_login_log limit 10000,100000; select * from user_login_log limit 10000,1000000;

数据量越大,花费时间越长
select * from user_login_log limit 100,100; select * from user_login_log limit 1000,100; select * from user_login_log limit 10000,100; select * from user_login_log limit 100000,100; select * from user_login_log limit 1000000,100;

偏移量越大,花费时间越长(浅层分页到深层分页)
SQL优化:
1.MySQL自身
2.网络IO
3.SQL自身
–原SQL
select * from user_login_log limit 10000,100000;
–避免使用select *
select user_id,ip,sttr1,attr2,attr3,attr4,attr5,attr6,attr7,attr8,attr9,attr10 from user_login_log limit 10000,100000;

全表扫描,查询效率慢
–按需查找字段
select id from user_login_log limit 10000,100000;

explain select id from user_login_log limit 10000,100000;

使用索引扫描,主键索引,进行提升
select user_id from user_login_log limit 10000,100000;


–覆盖索引
alter table user_login_log add index idx_user_id(user_id); select user_id from user_login_log limit 10000,100000;


查询字段索引覆盖,通过辅助索引提升查询效率
针对数据量大的情况,我们可以做如下优化:
按需查询字段,减少网络IO消耗
避免使用select *,减少MySQL优化器负担
查询的字段尽量保证索引覆盖
借助nosql缓存数据缓解MySQL数据库的压力
select * from user_login_log limit 1000000,100;

–增加索引where条件,缩减数据范围
select * from user_login_log where id> 1000000 limit 100;

偏移量大的场景,我们可以使用数据量大的优化方案,除此之外还可以将偏移量改为使用id限定的方式提升查询效率
针对偏移量大的情况,我们可以做如下优化:
添加where条件缩减条数,然后limit关键再进行数据筛选
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/5ab6d0ba88.html
