diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375112208.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375112208.gif new file mode 100644 index 0000000000000000000000000000000000000000..3439e2c9b07ab0bbfc46474486ae1ac78e9d9e12 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375112208.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375411998.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375411998.gif new file mode 100644 index 0000000000000000000000000000000000000000..6a48b3e6cff59b8faeda3e30a9ce9eecc61f49b5 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375411998.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375412010.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375412010.gif new file mode 100644 index 0000000000000000000000000000000000000000..60a7f482c65028c0f2916c64821785c24bc4a14b Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375412010.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375412014.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375412014.gif new file mode 100644 index 0000000000000000000000000000000000000000..b59973e7e9f6e98cf294a68a81e22672935c1953 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375412014.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375431400.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375431400.gif new file mode 100644 index 0000000000000000000000000000000000000000..0e03a36271a3c6138610121cb1272d6d9aac50e1 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375431400.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375591336.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375591336.gif new file mode 100644 index 0000000000000000000000000000000000000000..c651c07a002f3a8310e08fe436b7afc07d97b3bd Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375591336.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001375591340.gif b/content/zh/post/2022/figures/zh-cn_image_0000001375591340.gif new file mode 100644 index 0000000000000000000000000000000000000000..7a1152b8945642da72cb9b1b413d31e1738214c8 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001375591340.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425412261.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425412261.gif new file mode 100644 index 0000000000000000000000000000000000000000..78c5f0556d67167331e0a75a6e6d8e9d68630f0f Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425412261.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425531861.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425531861.gif new file mode 100644 index 0000000000000000000000000000000000000000..32eeb32e63162ef45772f00e2ecc6b1625ecf169 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425531861.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425531865.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425531865.gif new file mode 100644 index 0000000000000000000000000000000000000000..003c89a780978f570c1821f7392e16bd2904c9b0 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425531865.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425531873.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425531873.gif new file mode 100644 index 0000000000000000000000000000000000000000..78b1c3bdbe387f7f4e934775a4b7d2b4a5c34fb6 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425531873.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425732073.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425732073.gif new file mode 100644 index 0000000000000000000000000000000000000000..f5e0012c4ae6af8f63010aed9af2953913074948 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425732073.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425732077.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425732077.gif new file mode 100644 index 0000000000000000000000000000000000000000..b95967b109eccee8b46130e5ce65007200eb534f Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425732077.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001425911265.gif b/content/zh/post/2022/figures/zh-cn_image_0000001425911265.gif new file mode 100644 index 0000000000000000000000000000000000000000..bad3817c7f20f6725b0f14d176c4c6156041d89f Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001425911265.gif differ diff --git a/content/zh/post/2022/figures/zh-cn_image_0000001426032181.png b/content/zh/post/2022/figures/zh-cn_image_0000001426032181.png new file mode 100644 index 0000000000000000000000000000000000000000..09c9c725513cfb29124b0ea69a6a71dbcaf81eb2 Binary files /dev/null and b/content/zh/post/2022/figures/zh-cn_image_0000001426032181.png differ diff --git "a/content/zh/post/2022/openGauss\347\232\204SQL\345\274\225\346\223\216\345\234\250\347\211\210\346\234\254\344\270\255\345\201\232\344\272\206\345\223\252\344\272\233\344\274\230\345\214\226.md" "b/content/zh/post/2022/openGauss\347\232\204SQL\345\274\225\346\223\216\345\234\250\347\211\210\346\234\254\344\270\255\345\201\232\344\272\206\345\223\252\344\272\233\344\274\230\345\214\226.md" new file mode 100644 index 0000000000000000000000000000000000000000..fffc34352b1eadd4910e41be4aab90180ca23c83 --- /dev/null +++ "b/content/zh/post/2022/openGauss\347\232\204SQL\345\274\225\346\223\216\345\234\250\347\211\210\346\234\254\344\270\255\345\201\232\344\272\206\345\223\252\344\272\233\344\274\230\345\214\226.md" @@ -0,0 +1,42 @@ ++++ + +title = "openGauss的SQL引擎在版本中做了哪些优化系" + +date = "2022-10-10" + +tags = [ "SQL引擎优化"] + +archives = "2022-010" + +author = "王天庆" + +summary = "性能专项" + +img = "/zh/post/2022/title/img7.png" + +times = "12:30" + ++++ + +# openGauss的SQL引擎在版本中做了哪些优化? + +查询执行能力对数据库来说至关重要,这直接决定了查询语句生成的执行计划以何种方式进行执行,如果哪个执行算子的执行表现不好,将会对数据库的整体性能产生极大的影响。同时,执行算子的实现也极大考验一款数据库的工程实现能力。为此,openGauss数据库在3.1.0版本中,进一步优化了当前的部分执行算子,并希望通过优化执行算子的实现,来提高数据库的整体表现。下面介绍一下在该版本中,openGauss数据库都做了哪些SQL引擎的优化。 + +## **分区表页面估算优化** + +目前分区表的统计信息,代价模型使用的是普通表的机制,由于分区表和普通表结构上的不同,所以不能完全依赖普通表的机制去处理分区表的代价问题。由于分区表存在天然的分区优势,所以在分区剪枝的情况下,SQL语句在查询过程中不会访问所有分区。对于分区存在剪枝的场景,分区表页面统计时就不能单纯的统计所有分区的页面。在剪枝场景下只需要统计剪枝后的分区页面数即可。由于目前分区表没有统计每个分区的页面数,因此基于均匀性假设(分区页面不倾斜),用以下公式估算分区页面数: + +剪枝后分区页面数 = 分区表总页面数 \* (剪枝后分区数 / 总分区数) + +通过改进分区表页面的估算方法,可以优化数据库的表扫描性能,从而提升数据库在分区表场景中的执行表现。该特性可通过GUC参数partition\_page\_estimation控制。 + +## **Partition Iterator算子优化** + +在openGauss数据库中,分区表是依靠分区迭代算子进行分区扫描的,迭代算子控制每一个分区依次扫描数据。当分区剪枝结果只有一个分区时,迭代算子已经失去了迭代器的作用,在此情况下消除迭代算子,可以避免执行时迭代算子计算的开销。由于执行器的pipeline架构,迭代算子会被重复执行,在数据量比较大的场景下消除迭代算子的收益将十分可观。故在该版本中,我们通过对迭代算子进行改进,消除分区扫描时的overhead,进而提升数据库在该场景下的执行性能。该特性可通过GUC参数partition\_iterator\_elimination控制。 + +## **SeqScan算子优化** + +开启该优化,当用户不采集cpu信息时\(例如explain cpu off\),关闭cpu信息采集功能(例如执行CPUUsageGetCurrent函数),从而减少cpu执行周期。该特性不影响正常的cpu信息采集,相关监控视图和系统函数不受影响。同时,从磁盘读取数据时,优化mdread机制,减少冗余的checksum检查,进而通过消除无用的操作来提升数据库性能。该特性可通过GUC参数enable\_seqscan\_fusion控制。 + +上面的三个方面是该版本对openGauss数据库的SQL引擎的优化,后续我们还会对更多的openGauss数据库内部执行逻辑进行优化,敬请关注。 + diff --git "a/content/zh/post/2022/\350\247\243\345\257\206openGauss\346\225\260\346\215\256\345\272\223\344\270\255\347\232\204\345\207\275\346\225\260\344\276\235\350\265\226\345\205\263\347\263\273.md" "b/content/zh/post/2022/\350\247\243\345\257\206openGauss\346\225\260\346\215\256\345\272\223\344\270\255\347\232\204\345\207\275\346\225\260\344\276\235\350\265\226\345\205\263\347\263\273.md" new file mode 100644 index 0000000000000000000000000000000000000000..aaa45d8e28437c748ae92d60cd2a373a7ded3a2f --- /dev/null +++ "b/content/zh/post/2022/\350\247\243\345\257\206openGauss\346\225\260\346\215\256\345\272\223\344\270\255\347\232\204\345\207\275\346\225\260\344\276\235\350\265\226\345\205\263\347\263\273.md" @@ -0,0 +1,113 @@ +# + ++++ + +title = "解密openGauss数据库中的函数依赖关系" + +date = "2022-10-10" + +tags = [ "函数依赖关系"] + +archives = "2022-010" + +author = "王天庆" + +summary = "函数依赖关系" + +img = "/zh/post/2022/title/img7.png" + +times = "12:30" + ++++ + +# 解密openGauss数据库中的函数依赖关系 + +生活中总是存在着错综复杂的联系,例如喜欢打篮球的人,身高普遍比较高;喜欢穿艳丽色衣服的人,性格会普遍比较开朗;在超市买炸鸡的人,会大概率买啤酒。而反过来,这种联系并不一定成立。 + +在数据库领域,表中的不同属性就像生活中不同的事物,也会存在着各种类似的关联关系。如何利用这种关联关系来提升数据库查询性能?这篇文章可以解密openGauss的一种应用方案,该特性可通过GUC参数enable\_functional\_dependency进行控制。 + +## 1 函数依赖的用途 + +函数依赖特性就是用一个介于0\~1的值来量化这种关联关系的强弱程度,这个数值又被称为函数依赖度,其中0表示没有关联,1表示完全关联。函数依赖,是多列统计信息的一种,可以描述两个属性之间的关联关系的强弱,也可以用于描述多个属性与另一个属性的关联关系的强弱,其主要用途是提高选择率计算的准确性。其中选择率的定义为 + +![](figures/zh-cn_image_0000001425412261.gif) + +选择率是对SQL查询结果规模的预期,取值0\~1。如果选择率比较大,即接近于1,那么查询优化器会倾向于使用顺序扫描,如果选择率比较小,即接近于0,查询优化器会倾向于使用索引扫描,如果介于两者之间,查询优化器倾向于使用位图扫描。所以选择率能否估算准确,会直接影响到执行计划的选择,进而影响到数据库的查询效率。函数依赖特性的加入,能一定程度上提高选择率估算的准确性。 + +在引用函数依赖特性之前,数据库会使用条件独立假设的方法来估算选择率。举一个生活中的例子,在一个班级中,男女生的比例是1:1,喜欢篮球的人数占比为20%,身高175cm以上的人数占比为50%。我们可以说喜欢篮球且身高在175cm以上的男生的占比为50% \* 20% \* 50% = 5%吗?显然这个计算结果会比实际结果低,因为这几个属性之间有关联关系,喜欢篮球的人普遍身高偏高,喜欢篮球的人中男生占多数,男生的身高较女生普遍偏高。 + +## 2 理论推导 + +下面我们从理论上来对比,条件独立假设和函数依赖在计算选择率时的差异。假设一个表 t\(a int, b int, c int\),A是只与属性\{a\}相关的等式约束条件,B是只与属性\{b\}相关的等式约束条件。在条件独立假设下,同时满足约束条件A和约束条件B的选择率的计算公式为 + +![](figures/zh-cn_image_0000001425732073.gif) + +这会导致选择率一定程度的低估,因为约束条件A和约束条件B会存在或强或弱关联,而这里并没有考虑在内。引入函数依赖特性,并应用函数依赖关系![](figures/zh-cn_image_0000001375411998.gif)来计算选择率s\(A, B\)的理论公式为 + +![](figures/zh-cn_image_0000001425531861.gif) + +其中,![](figures/zh-cn_image_0000001425732077.gif)为一个实数,取值0\~1。类似地,对于含有三个属性的函数依赖关系"a , b=\> c: ![](figures/zh-cn_image_0000001425531865.gif)",计算选择率s\(A, B, C\)的理论公式为 + +![](figures/zh-cn_image_0000001425531873.gif) + +但是在实际应用中,考虑到最终的选择率不能大于任何一个属性列的选择率,需要对上述的理论公式做一个轻微变形,用min\(s\(A\), s\(B\)\)来代替第一项的s\(A\),min\(s\(A, B\), s\(C\)\)来代替第一项的s\(A, B\)。这种变形会对计算不满足函数依赖的数值的选择率,产生一定的修正作用。 + +![](figures/zh-cn_image_0000001375412010.gif) + +![](figures/zh-cn_image_0000001375412014.gif) + +## 3 应用示例 + +上面我们已经对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。 + +![](figures/zh-cn_image_0000001425911265.gif) + +--执行explain analyze select \* from t5 where b=2 and c=2;并查看计划。真实行数为100000,估算行数为83396,行数估计准确率略低,为0.834。 + +![](figures/zh-cn_image_0000001375112208.gif) + +--执行explain analyze select \* from t5 where b=3 and c=3; 并查看计划。真实行数为10000,估算行数为832,行数估计准确率较低,为0.083。 + +![](figures/zh-cn_image_0000001375591336.gif) + +--设置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倍。 + +![](figures/zh-cn_image_0000001375431400.gif) + +--执行explain analyze select \* from t5 where b=2 and c=2; 并查看计划。真实行数为100000,估算行数为99708,所以行数估计准确率为0.997。应用函数依赖统计信息,准确率从0.834提升为0.997,提升1.19倍。 + +![](figures/zh-cn_image_0000001375591340.gif) + +--执行explain analyze select \* from t5 where b=3 and c=3; 并查看计划。真实行数为10000,估算行数为10066,所以行数估计准确率为0.993。应用函数依赖统计信息,准确率从0.083提升为0.993,提升11.96倍。 + +![](figures/zh-cn_image_0000001426032181.png) + +通过在openGauss中使用函数依赖关系的数据库优化技术,我们可以看到数据库的查询优化性能得到了显著的提升。如果大家对该特性感兴趣,欢迎下载最新的openGauss数据库进行体验! + diff --git "a/content/zh/post/wzr/\346\225\260\346\215\256\345\272\223\347\232\204\345\210\233\345\273\272.md" "b/content/zh/post/wzr/\346\225\260\346\215\256\345\272\223\347\232\204\345\210\233\345\273\272.md" index 16304e57eb7704d4fa5531e820334fab57afedfc..254009cee78c0bb6a2a2cd064aae31948704dd3f 100644 --- "a/content/zh/post/wzr/\346\225\260\346\215\256\345\272\223\347\232\204\345\210\233\345\273\272.md" +++ "b/content/zh/post/wzr/\346\225\260\346\215\256\345\272\223\347\232\204\345\210\233\345\273\272.md" @@ -1,13 +1,13 @@ -+++
-title="openGauss社区入门(opengauss-数据库的创建)"
-date="2022-09-30"
-tags=["openGauss社区开发入门"]
-archives="2022-09"
-author="wangrururu"
-summary="openGauss社区开发入门"
-img="/zh/post/wzr/title/title.jpg"
-times="21:15"
-+++
++++ +title="openGauss社区入门(opengauss-函数的创建)" +date="2022-09-26" +tags=["openGauss社区开发入门"] +archives="2022-09" +author="wangrururu" +summary="openGauss社区开发入门" +img="/zh/post/wzr/title/title.jpg" +times="21:15" ++++ # 1.数据库的创建语法