您好,欢迎访问一九零五行业门户网

面试官:千万级数据,怎么快速查询?

先来看一个面试场景:
面试官:来说说,一千万的数据,你是怎么查询的?小哥哥:直接分页查询,使用limit分页。面试官:有实操过吗?小哥哥:肯定有呀也许有些朋友根本就没遇过上千万数据量的表,也不清楚查询上千万数据量的时候会发生什么。
今天就来带大家实操一下,这次是基于mysql 5.7.26版本做测试
准备数据 没有一千万的数据怎么办?
创建呗
代码创建一千万?那是不可能的,太慢了,可能真的要跑一天。可以采用数据库脚本执行速度快很多。
创建表create table `user_operation_log` ( `id` int(11) not null auto_increment, `user_id` varchar(64) character set utf8mb4 collate utf8mb4_general_ci null default null, `ip` varchar(20) character set utf8mb4 collate utf8mb4_general_ci null default null, `op_data` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr1` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr2` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr3` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr4` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr5` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr6` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr7` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr8` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr9` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr10` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr11` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, `attr12` varchar(255) character set utf8mb4 collate utf8mb4_general_ci null default null, primary key (`id`) using btree) engine = innodb auto_increment = 1 character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic;
创建数据脚本采用批量插入,效率会快很多,而且每1000条数就commit,数据量太大,也会导致批量插入效率慢
delimiter ;;create procedure batch_insert_log()begin declare i int default 1; declare userid int default 10000000; set @execsql = 'insert into `test`.`user_operation_log`(`user_id`, `ip`, `op_data`, `attr1`, `attr2`, `attr3`, `attr4`, `attr5`, `attr6`, `attr7`, `attr8`, `attr9`, `attr10`, `attr11`, `attr12`) values'; set @execdata = ''; while i<=10000000 do set @attr = "'测试很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的属性'"; set @execdata = concat(@execdata, "(", userid + i, ", '10.0.69.175', '用户登录操作'", ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ",", @attr, ")"); if i % 1000 = 0 then set @stmtsql = concat(@execsql, @execdata,";"); prepare stmt from @stmtsql; execute stmt; deallocate prepare stmt; commit; set @execdata = ""; else set @execdata = concat(@execdata, ","); end if; set i=i+1; end while;end;;delimiter ;
开始测试 田哥的电脑配置比较低:win10 标压渣渣i5 读写约500mb的ssd
由于配置低,本次测试只准备了3148000条数据,占用了磁盘5g(还没建索引的情况下),跑了38min,电脑配置好的同学,可以插入多点数据测试
select count(1) from `user_operation_log`
返回结果:3148000
三次查询时间分别为:
14060 ms13755 ms13447 ms普通分页查询mysql 支持 limit 语句来选取指定的条数数据, oracle 可以使用 rownum 来选取。
mysql分页查询语法如下:
select * from table limit [offset,] rows | rows offset offset
第一个参数指定第一个返回记录行的偏移量第二个参数指定返回记录行的最大数目下面我们开始测试查询结果:
select * from `user_operation_log` limit 10000, 10
查询3次时间分别为:
59 ms49 ms50 ms这样看起来速度还行,不过是本地数据库,速度自然快点。
换个角度来测试
相同偏移量,不同数据量select * from `user_operation_log` limit 10000, 10select * from `user_operation_log` limit 10000, 100select * from `user_operation_log` limit 10000, 1000select * from `user_operation_log` limit 10000, 10000select * from `user_operation_log` limit 10000, 100000select * from `user_operation_log` limit 10000, 1000000
查询时间如下:
数量第一次第二次第三次
10条 53ms 52ms 47ms
100条 50ms 60ms 55ms
1000条 61ms 74ms 60ms
10000条 164ms 180ms 217ms
100000条 1609ms 1741ms 1764ms
1000000条 16219ms 16889ms 17081ms
从上面结果可以得出结束:数据量越大,花费时间越长
相同数据量,不同偏移量select * from `user_operation_log` limit 100, 100select * from `user_operation_log` limit 1000, 100select * from `user_operation_log` limit 10000, 100select * from `user_operation_log` limit 100000, 100select * from `user_operation_log` limit 1000000, 100
偏移量第一次第二次第三次
100 36ms 40ms 36ms
1000 31ms 38ms 32ms
10000 53ms 48ms 51ms
100000 622ms 576ms 627ms
1000000 4891ms 5076ms 4856ms
从上面结果可以得出结束:偏移量越大,花费时间越长
select * from `user_operation_log` limit 100, 100select id, attr from `user_operation_log` limit 100, 100
如何优化 既然我们经过上面一番的折腾,也得出了结论,针对上面两个问题:偏移大、数据量大,我们分别着手优化
优化偏移量大问题采用子查询方式我们可以先定位偏移位置的 id,然后再查询数据
select * from `user_operation_log` limit 1000000, 10select id from `user_operation_log` limit 1000000, 1select * from `user_operation_log` where id >= (select id from `user_operation_log` limit 1000000, 1) limit 10
查询结果如下:
sql花费时间
第一条 4818ms
第二条(无索引情况下) 4329ms
第二条(有索引情况下) 199ms
第三条(无索引情况下) 4319ms
第三条(有索引情况下) 201ms
从上面结果得出结论:
第一条花费的时间最大,第三条比第一条稍微好点子查询使用索引速度更快缺点:只适用于id递增的情况
id非递增的情况可以使用以下写法,但这种缺点是分页查询只能放在子查询里面
注意:某些 mysql 版本不支持在 in 子句中使用 limit,所以采用了多个嵌套select
select * from `user_operation_log` where id in (select t.id from (select id from `user_operation_log` limit 1000000, 10) as t)
采用 id 限定方式这种方法要求更高些,id必须是连续递增,而且还得计算id的范围,然后使用 between,sql如下
select * from `user_operation_log` where id between 1000000 and 1000100 limit 100select * from `user_operation_log` where id >= 1000000 limit 100
查询结果如下:
sql花费时间
第一条 22ms
第二条 21ms
从结果可以看出这种方式非常快
注意:这里的 limit 是限制了条数,没有采用偏移量
优化数据量大问题返回结果的数据量也会直接影响速度
select * from `user_operation_log` limit 1, 1000000select id from `user_operation_log` limit 1, 1000000select id, user_id, ip, op_data, attr1, attr2, attr3, attr4, attr5, attr6, attr7, attr8, attr9, attr10, attr11, attr12 from `user_operation_log` limit 1, 1000000
查询结果如下:
sql花费时间
第一条 15676ms
第二条 7298ms
第三条 15960ms
从结果可以看出减少不需要的列,查询效率也可以得到明显提升
第一条和第三条查询速度差不多,这时候你肯定会吐槽,那我还写那么多字段干啥呢,直接 * 不就完事了
注意本人的 mysql 服务器和客户端是在_同一台机器_上,所以查询数据相差不多,有条件的同学可以测测客户端与mysql分开
select * 它不香吗?在这里顺便补充一下为什么要禁止 select *。难道简单无脑,它不香吗?
主要两点:
用 select * 数据库需要解析更多的对象、字段、权限、属性等相关内容,在 sql 语句复杂,硬解析较多的情况下,会对数据库造成沉重的负担。增大网络开销,* 有时会误带上如log、iconmd5之类的无用且大文本字段,数据传输size会几何增长。特别是mysql和应用程序不在同一台机器,这种开销非常明显。结束 最后还是希望大家自己去实操一下,肯定还可以收获更多!
以上就是面试官:千万级数据,怎么快速查询?的详细内容。
其它类似信息

推荐信息