Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?( 二 )

注意只有主键 , 没有索引的情况下 。
然后用程序插入数据 , 先测试看插入 400多万数据的时候 , 数据库的操作情况

Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
查询
因为只有主键 , 并没有做索引 , 根据主键count耗时 3.53秒
然后 取出10条数据
select * from user_test limit 10;速度非常快 , 0.00秒 (400万+的数据量)
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
400万数据查询
select * from user_test where char_email='zyzonovuvaadcd@126.com' limit 10;char 格式的数据查询 , 耗时 7.39秒 (400万+的数据量)
select * from user_test where varchar_email='zyzonovuvaadcd@126.com' limit 10;varchar 格式的数据查询 , 耗时 5.52 秒 (400万+的数据量)
select * from user_test where user_id > 78921 limit 10;这个查询可以用于分页操作 , 数据量大的时候可以用
耗时 0.07 秒 , 速度也很快 。使用主键进行判断查询很快 。(400万+的数据量)
select * from user_test where user_id > 79021 limit 2; 查询速度非常快
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
查询
select * from user_test where varchar_name='行文可抒怀畅志 , 扬己志于天下 , 左登峰也喜此道 , 但他心性阴暗 , 不喜成群为伍 , 喧哗宣讲 。' limit 12;varchar 查询 耗时24.34 秒(400万+的数据量)
 
select * from user_test where char_name='行文可抒怀畅志 , 扬己志于天下 , 左登峰也喜此道 , 但他心性阴暗 , 不喜成群为伍 , 喧哗宣讲 。' limit 12;char 查询 耗时 24.09 秒 (400万+的数据量)
将表的数据量升级到 1513万 , 继续测试 。
select count(user_id) from user_test;耗时 50.32 秒 , 虽然是主键但是因为没加索引 , 速度很慢 。(1500万+的数据量)
select count(char_type) from user_test where char_type='del';char type 查询 耗时 57.27 秒  , 并非主键 , 并且没有索引 。(1500万+的数据量)
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
没有索引
增加索引之后 , 耗时:1秒 , 和tinyint差别不大 。
select count(tinyint_type) from user_test where tinyint_type='7';tinyint type 查询 耗时 56.68秒 并非主键 , 并且没有索引 。(1500万+的数据量)
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
增加索引之后
增加索引之后 , 耗时:0.69秒 。(1500万+的数据量)
 
select * from user_test where tinyint_type=7 limit 3;耗时:0.07秒 , 带索引 , (1500万+的数据量)
select * from user_test where char_type='del' limit 3;耗时:0.1秒 , 带索引 , (1500万+的数据量)
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
查询
这个测试证明 , 使用tinyint 或者 char 用来表示 属性 , 在没有索引的情况下 , 差别非常小 。
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
查询
增加索引前查询count
select count(user_id) from user_test;耗时 56.99秒
增加索引之后查询 count
Mysql数据库tinyint,int,bigint,char,varchar究竟用哪个?

文章插图
查询
select count(user_id) from user_test;耗时 3.24秒  , 速度提升非常多啊 。
但是依然有3.24秒 , 速度依然不理想啊 , 查询了下解决方案 , 不用主键做索引 , 
使用 count(*) 居然比使用 count(主键快) 这个你敢信???经过反复测试 , 发现都差不多的 。
那最终的count的效率的解决方案是什么呢?答案是如果数据量比较大 , 可以使用一个计数列 , 通过逻辑处理 , 不过如果增删改查比较多就不要用这样的方式 , 可以通过逻辑处理的方式进行处理 , 比如入库队列 , redis 缓存处理等等 。


推荐阅读