# 手动触发多策略 Compaction、视图查询功能上线 | Greptime 双周精选

Source: https://greptime.cn/blogs/2024-06-05-biweekly

## 内容概述

作为一个成长中的开源项目，GreptimeDB 的进展离不开来自全球的社区贡献者们，感谢各位！

最近的内容更新如下：

- **Compaction 新增选项**：支持通过 SQL 命令手动触发不同的 Compaction 策略，从而更好地组织数据，提升查询性能

- **视图查询支持**：支持创建和查询视图，简化了复杂查询，提升了数据管理效率

- **Scanner 性能提升**：通过并行处理 Row Group，大幅提高 SST 文件的扫描效率

## 社区贡献者名单

在过去的两周里，GreptimeDB 共合并了 **49 个** PR，其中有 **6 位**独立贡献者，累计 **8 个** PR 被成功合并，还有很多待合并的 PR。

祝贺以下各位在过去 2 周内成为我们活跃的贡献者：

- @[confoc](https://github.com/confoc) ([gtctl#200](https://github.com/GreptimeTeam/gtctl/pull/200) [gtctl#203](https://github.com/GreptimeTeam/gtctl/pull/203))

- @[CookiePieWw](https://github.com/CookiePieWw) ([db#4008](https://github.com/GreptimeTeam/greptimedb/pull/4008))

- @[etolbakov](https://github.com/etolbakov) ([db#4035](https://github.com/GreptimeTeam/greptimedb/pull/4035))

- @[irenjj](https://github.com/irenjj) ([db#4042](https://github.com/GreptimeTeam/greptimedb/pull/4042) [docs#982](https://github.com/GreptimeTeam/docs/pull/982))

- @[LYZJU2019](https://github.com/LYZJU2019) ([db#4050](https://github.com/GreptimeTeam/greptimedb/pull/4050))

- @[taobo](https://github.com/realtaobo) ([db#3957](https://github.com/GreptimeTeam/greptimedb/pull/3957))

👏 欢迎 @[confoc](https://github.com/confoc) @[LYZJU2019](https://github.com/LYZJU2019) 作为新的贡献者加入社区并成功合并了首个 PR，还有更多来自其他独立贡献者的 PR 正在等待合并。

<img src="/zh/blogs/2024-06-05-biweekly/image1.png" alt="Contributor of GreptimeDB" style="width: 80%; margin: 0 auto"/>

同时衷心感谢我们所有的成员和贡献者！是你们的付出让我们的项目得以成功，也是你们让 GreptimeDB 成为一个更优质的产品。让我们一起努力，建立一个更棒的社区！

## PR 亮点

### [db#3988](https://github.com/GreptimeTeam/greptimedb/pull/3988) 支持不同策略的手动 Compaction

这个 PR 引入了通过 SQL 命令手动触发不同类型的 Compaction 的功能。新支持的 SQL 语法如下：

```sql
SELECT COMPACT_TABLE(<table_name>, [<compact_type>], [<options>])
```

目前支持的 `compact_type` 选项包括：

- `regular`：触发类似于 Flush 操作的标准 Compaction。

- `strict_window`：严格按照指定时间窗口划分 SST 文件。

`<options>` 是特定类型的 Compaction 设置。例如，对于 `strict_window`，选项指定 Compaction 窗口的秒数。

### [db#3952](https://github.com/GreptimeTeam/greptimedb/pull/3952) 支持视图查询功能

这个 PR 引入了对视图查询的支持，大大简化并提高了处理复杂查询的效率。通过这个更新，用户现在可以使用直观的 SQL 语法创建和查询视图。该功能将复杂的逻辑封装成可重用的虚拟表，提供了更好的数据安全性、性能优化和数据复杂性抽象。

新支持的 SQL 语法如下：

- 创建视图

```sql
CREATE [OR REPLACE] [IF NOT EXISTS] VIEW <view_name> AS <SELECT_statement>;
```

- 查询视图

```sql
SELECT * FROM <view_name>;
```

### [db#3957](https://github.com/GreptimeTeam/greptimedb/pull/3957) 为 gRPC 服务添加 TLS 支持

这个 PR 通过增加 TLS 支持增强了 gRPC 服务的安全性。

gRPC 服务器 TLS 配置：


```toml
[grpc.tls]
## TLS mode.
mode = "enable"
## Certificate file path.
cert_path = "/path/to/certfile"
## Private key file path.
key_path = "/path/to/keyfile"
## Watch for Certificate and key file change and auto reload.
watch = false
```

### [db#3992](https://github.com/GreptimeTeam/greptimedb/pull/3992) 通过并行扫描提升 Unordered Scanner 性能

该 PR 通过并行读取 Parquet Row Group 显著提升了 `UnorderedScan` 的性能。基于指定的并行度设置，实现了行组和内存表的并发处理，从而提高了效率和响应速度，优化了数据读取速度。

### [db#4024](https://github.com/GreptimeTeam/greptimedb/pull/4024) 为 Metasrv 引入轮询选择器

这个 PR 引入了 `RoundRobin` 选择器，以公平和顺序的策略选择对等节点以便 Metasrv 在数据节点之间进行负载分配。通过基于 `node_id` 轮询数据节点，它提供了一种与现有的 `LoadBased` 和 `LeaseBased` 选择器不同的方法。这种方法将平均分配负载，但不建议在正式生产环境中使用，因为它没有考虑每个数据节点的实际负载情况。

### [db#4019](https://github.com/GreptimeTeam/greptimedb/pull/4019) 启用 HTTP 服务器的 TCP Keepalive 功能

这个 PR 为 HTTP 服务器引入了 TCP Keepalive 功能，解决了在某些客户环境中观察到的悬空 HTTP 连接问题。这些连接在客户端 Pod 销毁后仍保持 `established` 状态，消耗了大量内存资源。通过实现 TCP Keepalive，闲置连接将在一小时后关闭，从而减少因不正常的关闭或云环境中常见的网络问题引起的问题。为了保持简单性并避免复杂化配置文件，Keepalive 选项是硬编码的。

## Good First Issue

### [db#3884 移除 query crate 中不必要的 traits 和 wrapper 类型](https://github.com/GreptimeTeam/greptimedb/issues/3884) 

大多数实现只是将请求转发到 Datafusion。由于我们与 Datafusion 高度耦合，并且没有计划支持另一个查询引擎，所以我们可以删除这些类型。

关键词：重构

难度：简单

## 关于 Greptime

[Greptime 格睿科技](https://greptime.cn/)专注于为可观测、物联网及车联网等领域提供实时、高效的数据存储和分析服务，帮助客户挖掘数据的深层价值。目前基于云原生的时序数据库 [GreptimeDB](https://github.com/GreptimeTeam/greptimedb) 已经衍生出多款适合不同用户的解决方案，更多信息或 demo 展示请联系下方小助手（微信号：greptime）。

欢迎对开源感兴趣的朋友们参与贡献和讨论，从带有 good first issue 标签的 issue 开始你的开源之旅吧～期待在开源社群里遇见你！添加小助手微信即可加入“技术交流群”与志同道合的朋友们面对面交流哦~

Star us on GitHub Now: https://github.com/GreptimeTeam/greptimedb

官网：https://greptime.cn/

文档：https://docs.greptime.cn/

Twitter: https://twitter.com/Greptime

Slack: https://greptime.com/slack

LinkedIn: https://www.linkedin.com/company/greptime/
