解密 openGauss 数据库中的函数依赖关系

我们已经对 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: 一款高性能、高安全、高可靠的企业级开源关系型数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值