在postgresql9.5的時候做過一個測試就是sum()的效率最終的測試結果是sum(int)>sum(numeric)>sum(bigint)當時比較詫異為啥sum(bigint)效率比sum(numeric)還低。sum(numeric)的效率比sum(bigint)快了10%。
在pg10版本的時候對sum()的性能做了優化,pg10.4
最終的測試結果為pg10的效率大幅提升,sum(int)>sum(bigint)>sum(numeric),當一個表中有bigint,int時,誰放在第一列效率要高點。但是差別不是很大,效率都比numeric高。
bigint for smallint or int arguments, numeric for bigint arguments, otherwise the same as the argument data type
這次主要做abase5.0測試,以及pg11 jit測試。
插入1kw數據測試。
這里只是輸出類型的轉換,並不會太影響效率。
numeric的算術運算比整數類型要慢很多。
通過求助,最終了解到可能和pg的元組變形(tuple deform)有關,
這次創建三張表分別對應三種數據類型。
create table t_int(n_int int);
create table t_bigint(n_bigint bigint);
create table t_numeric(n_numeric numeric);
insert into t_int select generate_series(1,10000000);
insert into t_bigint select generate_series(1,10000000);
insert into t_numeric select generate_series(1,10000000);
numeric :
https://explain.depesz.com/s/Lk6P
pg:bigint,numeric,int效率測試:
drop table t_int;
drop table t_bigint;
drop table t_numeric;
show shared_buffers;
drop table t_bigint
create table t_int(n_int int);
create table t_bigint(n_bigint bigint);
create table t_numeric(n_numeric numeric);
insert into t_int select generate_series(1,10000000);
insert into t_bigint select generate_series(1,10000000);
insert into t_numeric select generate_series(1,10000000);
numeric
select version();
explain analyze
select count(*) from t_num_type
SET max_parallel_workers_per_gather = 2;
show max_parallel_workers_per_gather ;
select version();
1.單表測試
explain (analyze,buffers,format text) select sum(n_int) from t_int;--560
explain (analyze,buffers,format text) select sum(n_bigint) from t_bigint;--575
explain (analyze,buffers,format text) select sum(n_numeric) from t_numeric;--868
sum(int)>sum(bigint)>sum(numeric)
2.一個表測試
drop table t_num_type
create table t_num_type(n_bigint bigint,n_numeric numeric,n_int int);
insert into t_num_type select n,n,n from generate_series(1,10000000) as t(n)
explain (analyze,buffers,format text) select sum(n_int) from t_num_type ;--661
explain (analyze,buffers,format text) select sum(n_bigint) from t_num_type;--625
explain (analyze,buffers,format text) select sum(n_numeric) from t_num_type;--946
sum(bigint)>sum(int)>sum(numeric)
但是整體比單表慢。
select * from t_num_type_3 limit 10
drop table t_num_type_2
create table t_num_type_2(n_int int,n_numeric numeric,n_bigint bigint);
insert into t_num_type_2 select n,n,n from generate_series(1,10000000) as t(n)
explain (analyze,buffers,format text) select sum(n_int) from t_num_type_2;--603
explain (analyze,buffers,format text) select sum(n_bigint) from t_num_type_2;--668
explain (analyze,buffers,format text) select sum(n_numeric) from t_num_type_2;--947
sum(int)>sum(bigint)>sum(numeric)
--show jit_above_cost
int放前面int快,bigint又慢了。
3.
create table t_num_type_3(n_bigint bigint,n_int int,n_numeric numeric);
insert into t_num_type_3 select n,n,n from generate_series(1,10000000) as t(n)
explain (analyze,buffers,format text) select sum(n_int) from t_num_type_3;--623
explain (analyze,buffers,format text) select sum(n_bigint) from t_num_type_3;--616
explain (analyze,buffers,format text) select sum(n_numeric) from t_num_type_3;--973
目前來bigint放到第一列總是快的。當int放到第一列的時候又比bigint快。
create table t_num_type_4(n_int int,n_bigint bigint,n_numeric numeric);
insert into t_num_type_4 select n,n,n from generate_series(1,10000000) as t(n)
explain (analyze,buffers,format text) select sum(n_int) from t_num_type_4;--617
explain (analyze,buffers,format text) select sum(n_bigint) from t_num_type_4;--643
explain (analyze,buffers,format text) select sum(n_numeric) from t_num_type_4;--973