一、建表
CREATE TABLE t2 (
f1 VARCHAR(30),
f2 VARCHAR(30),
...
f12 VARCHAR(30)
);
CREATE TABLE t3 (
f1 VARCHAR(30),
f2 VARCHAR(30),
...
f12 VARCHAR(30)
) USING columnar;
说明:
T2为行存、T3为列存;
考虑到日常字符类型使用较多,且大部分表的列数约为10列,故建立12列字符类型的表。
二、插入性能
1、下面是从128万到256万的插入性能
insert into t2 select * from t2
> Affected rows: 2560000
> 查询时间: 18.882s
insert into t3 select * from t3
> Affected rows: 2560000
> 查询时间: 20.384s
说明:在这个数据量下,行存占优
2、下面是从256万到512万的插入性能
insert into t2 select * from t2
> Affected rows: 5120000
> 查询时间: 30.945s
insert into t3 select * from t3
> Affected rows: 5120000
> 查询时间: 38.031s
说明:在这个数据量下,行存占优
3、下面是从1024万到2048万的插入性能
insert into t2 select * from t2
> Affected rows: 20480000
> 查询时间: 243.78s
insert into t3 select * from t3
> Affected rows: 20480000
> 查询时间: 295.289s
说明:在这个数据量下,行存占优。但随着数据量的增加,列存会逐步占优。
三、查询性能
先看一组测试数据:
行存表查询 | 行存表耗时 | 列存表查询 | 列存表耗时 |
select count(*) from t2 where f2 like '3XJD%' limit 100 | 21.142s | select count(*) from t3 where f2 like '3XJD%' limit 100 | 2.607s |
select substr(f1, 1, 3), count(*) from t2 group by substr(f1, 1, 3) | 24.712s | select substr(f1, 1, 3), count(*) from t3 group by substr(f1, 1, 3) | 9.711s |
select f1, count(*) from t2 group by f1 | 6.855s | select f1, count(*) from t3 group by f1 | 5.959s |
select count(*) from t2 | 4.643s | select count(*) from t3 | 0.409s |
从上述测试来看,列存在上述的查询场景下表现较好。为了更好的测试列存的适用范围,我们向列存的软肋下手看看效果。
行存表查询 | 行存表耗时 | 列存表查询 | 列存表耗时 |
select concat_ws(',', f1, f2, f3, f4, f5) from t2 where f1 like 'jkG%' limit 100 | 0.076s | select concat_ws(',', f1, f2, f3, f4, f5) from t3 where f1 like 'W4SK%' limit 100 | 0.15s |
select concat_ws(',', f1, f2, f3, f4, f5) from t2 where f1 like 'jkG%' limit 10000 | 11.007s | select concat_ws(',', f1, f2, f3, f4, f5) from t3 where f1 like 'W4SK%' limit 10000 | 7.476s |
select sum(length(v1)) from (select concat_ws(',', f1, f2, f3, f4, f5) as v1 from t2 where f1 like 'jkG%') a | 11.749s | select sum(length(v1)) from (select concat_ws(',', f1, f2, f3, f4, f5) as v1 from t3 where f1 like 'W4SK%') a | 6.062s |
select sum(length(v1)) from (select concat_ws(',', f1, f2, f3, f4, f5, f6, f7, f8, f9) as v1 from t2 where f1 like 'jkG%') a; | 11.243s | select sum(length(v1)) from (select concat_ws(',', f1, f2, f3, f4, f5, f6, f7, f8, f9) as v1 from t3 where f1 like 'W4SK%') a; | 13.325s |
可以看出,随着选取列数的增加,列存的性能会有一定程度的下降。
四、总结
综合来看,行存适用于有数据录入需求并且数据量小于1000万的场景。列存适用于超过1000万的数据分析场景。
数合建模,会根据客户的需求,匹配响应的版本来满足客户需求。