我们已经对 openGauss 数据库中的函数依赖关系进行了理论上的介绍,下面我们看看在具体实践中,应该如何操作。下面的几个例子,可以很好地展现 openGauss 数据库的该特性。
-- 优化 SQL 查询的行数估计模型前
create table t5 (a int, b int, c int, d int, e int);
insert into t5 select 1, 1, 1, 1, 1 from generate_series(1,100) i;
insert into t5 select 2, 2, 2, 2, 2 from generate_series(1,100000) i;
insert into t5 select 3, 3, 3, 3, 3 from generate_series(1,10000) i;
insert into t5 select 4, 4, 4, 4, 4 from generate_series(1,10000) i;
analyze t5;
执行 explain analyze select * from t5 where b=1 and c=1; 并查看计划。真实行数为 100,估算行数为 1,行数估计准确率较低,为 0.01。
执行 explain analyze select * from t5 where b=2 and c=2; 并查看计划。真实行数为 100000,估算行数为 83396,行数估计准确率略低,为 0.834。
执行 explain analyze select * from t5 where b=3 and c=3; 并查看计划。真实行数为 10000,估算行数为 832,行数估计准确率较低,为 0.083。
设置 ANALYZE 的采样方式为百分比采样,采样率为 2%
set default_statistics_target=-2;
开启 GUC 参数,生成函数依赖统计信息
set enable_functional_dependency=on;
alter table t5 add statistics ((a, b, c));
analyze t5((a, b, c));
执行 explain analyze select * from t5 where b=1 and c=1; 并查看计划。真实行数为 100,估算行数为 72,所以行数估计准确率为 0.72。应用函数依赖统计信息,准确率从 0.01 提升为 0.72,提升 72 倍。
执行 explain analyze select * from t5 where b=2 and c=2; 并查看计划。真实行数为 100000,估算行数为 99708,所以行数估计准确率为 0.997。应用函数依赖统计信息,准确率从 0.834 提升为 0.997,提升 1.19 倍。
执行 explain analyze select * from t5 where b=3 and c=3; 并查看计划。真实行数为 10000,估算行数为 10066,所以行数估计准确率为 0.993。应用函数依赖统计信息,准确率从 0.083 提升为 0.993,提升 11.96 倍。
通过在 openGauss 中使用函数依赖关系的数据库优化技术,我们可以看到数据库的查询优化性能得到了显著的提升。如果大家对该特性感兴趣,欢迎下载最新的 openGauss 数据库进行体验!
openGauss: 一款高性能、高安全、高可靠的企业级开源关系型数据库。