weizhengte commented on code in PR #19323: URL: https://github.com/apache/doris/pull/19323#discussion_r1186655979
########## docs/zh-CN/docs/query-acceleration/statistics.md: ########## @@ -0,0 +1,888 @@ +--- +{ +"title": "统计信息", +"language": "zh-CN" +} +--- + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# 统计信息 + +## 统计信息简介 + +在 SQL 数据库中,查询优化器的质量对系统性能有重要影响。优化器需要依据统计信息来估算查询代价,尤其在等值查询场景中,精确估算基数是非常重要的,基数估算可以帮助优化器选择最优的查询计划,从而提高查询性能。 + +在执行一个查询时,未经过充分优化的执行计划和经过优化的执行计划可能会导致执行时间上的巨大差异,这个差距可以高达数倍。因此,对于 SQL 查询优化器来说,收集和分析统计信息是非常重要的,只有通过这些统计信息,优化器才能够准确地评估不同执行计划的成本,并选择最佳的执行计划。 + +Doris 查询优化器使用统计信息来确定查询最有效的执行计划。Doris 维护的统计信息包括表级别的统计信息和列级别的统计信息。 + +表统计信息: + +| 信息 | 描述 | +| :------------------ | :------------------------- | +| `row_count` | 表的行数 | +| `data_size` | 表的⼤⼩(单位 byte) | +| `update_rows` | 收集统计信息后所更新的行数 | +| `healthy` | 表的健康度 | +| `update_time` | 最近更新的时间 | +| `last_analyze_time` | 上次收集统计信息的时间 | + +> 表的健康度:表示表统计信息的健康程度。当 `update_rows` 大于等于 `row_count` 时,健康度为 0;当 `update_rows` 小于 `row_count` 时,健康度为 `100 * (1 - update_rows / row_count)` 。 + +列统计信息: + +| 信息 | 描述 | +| :------------ | :------------------------- | +| `row_count` | 列的总行数 | +| `data_size` | 列的总⻓度(单位 byte) | +| `avg_size_byte` | 列的平均⻓度(单位 bytes) | +| `ndv` | 列 num distinct value | +| `min` | 列最小值 | +| `max` | 列最⼤值 | +| `null_count` | 列 null 个数 | +| `histogram` | 列直方图 | + +接下来将简单介绍其中出现的直方图等数据结构,以及详细介绍统计信息的收集和维护。 + +## 直方图简介 + +直方图(histogram)是一种用于描述数据分布情况的工具,它将数据根据大小分成若干个区间(桶),并使用简单的统计量来表示每个区间中数据的特征。是数据库中的一种重要的统计信息,可以描述列中的数据分布情况。直方图最典型的应用场景是通过估算查询谓词的选择率来帮助优化器选择最优的执行计划。 + +在 Doris 中,会对每个表具体的列构建一个等高直方图(Equi-height Histogram)。直方图包括一系列桶, 其中每个桶的统计量包括桶的上下界、桶包含的元素数量、前面桶所有元素的数量、桶中不同值的个数。具体可以参考 SQL 函数 `histogram` 或者 `hist` 的使用说明。 + +> 采用等高直方图的分桶方法,每个桶中的数值频次之和都应该接近于总行数的 `1/N`。但是,如果严格遵守等高原则进行分桶,会出现某些值落在桶的边界上的情况,导致同一个值出现在两个不同的桶中。这种情况会对选择率的估算造成干扰。因此,在实现中,Doris 对等高直方图的分桶方法进行了修改:如果将一个值加入到某个桶中导致该桶中的数据频次超过了总行数的 `1/N`,则根据哪种情况更接近 `1/N`,将该值放入该桶或下一个桶中。 + +## 收集统计信息 + +### 手动收集 + +⽤户通过 `ANALYZE` 语句触发手动收集任务,根据提供的参数,收集指定的表或列的统计信息。 + +列统计信息收集语法: + +```SQL +ANALYZE [ SYNC ] TABLE table_name + [ (column_name [, ...]) ] + [ [ WITH SYNC ] [ WITH INCREMENTAL ] [ WITH SAMPLE PERCENT | ROWS ] [ WITH PERIOD ] ] + [ PROPERTIES ("key" = "value", ...) ]; +``` + +列直方图收集语法: + +```SQL +ANALYZE [ SYNC ] TABLE table_name Review Comment: Same as above -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org