成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

“小而美” 的分析庫-DuckDB 初探

數據庫 其他數據庫
DuckDB 是一個 In-Process 的 OLAP 數據庫,可以理解為 AP 版本的 SQLite,但其底層是列式存儲。2019 年 SIGMOD 有一篇 Demo 論文介紹 DuckDB:an embedded analytical database。

DuckDB 是近期非?;鸬囊豢?AP 數據庫,其獨特的定位很有趣。甚至有數據庫產品考慮將其納入進來,作為分析能力的擴展。本文就針對這一數據庫做個小評測。

1. DuckDB 數據庫概述

1).DuckDB 產生背景

DuckDB 是一個 In-Process 的 OLAP 數據庫,可以理解為 AP 版本的 SQLite,但其底層是列式存儲。2019 年 SIGMOD 有一篇 Demo 論文介紹 DuckDB:an embedded analytical database。隨著單機內存的變大,大部分 OLTP 數據庫都能在內存中放得下,而很多 OLAP 也有在單機就能搞定的趨勢。單臺服務器的內存很容易達到 TB,加上 SSD,搞個幾十甚至上百 TB 很容易。DuckDB 就是為了填補這個空白而生的。

2).DuckDB 開源情況

DuckDB 采用 MIT 協議開源,是荷蘭 CWI 數據庫組的一個項目,學術氣息比較濃厚,項目的組織很有教科書的感覺,架構很清晰,所以非常適合閱讀學習。我從 OSS Insight 拉個一個 Star 數對比,可以看到 DuckDB 發展非常迅速。

圖片圖片

3).DuckDB 特點

DuckDB是一個免費的、開源的、嵌入式數據庫管理系統,專為數據分析和在線分析處理而設計。這意味著以下幾點:

  • 它是免費的開源軟件,因此任何人都可以使用和修改代碼。
  • 它是嵌入式的,這意味著DBMS(數據庫管理系統)與使用它的應用程序在同一進程中運行。這使得它快速且易于使用。
  • 它針對數據分析和OLAP(在線分析處理)進行了優化,而不僅僅是像典型數據庫那樣只針對事務數據。這意味著數據按列而不是行組織以優化聚合和分析。
  • 它支持標準SQL,因此可以在數據上運行查詢、聚合、連接和其他SQL函數。
  • 它在進程中運行,即在應用程序本身內運行,而不是作為單獨的進程運行。這消除了進程間通信的開銷。
  • 與SQLite一樣,它是一個簡單的、基于文件的數據庫,因此不需要單獨安裝服務器。只需將庫包含在應用程序中即可。

4).DuckDB 優點

  • DuckDB 易于安裝、部署和使用。沒有需要配置的服務器,可在應用程序內部嵌入運行,這使得它易于集成到不同編程語言環境中。
  • DuckDB 盡管它很簡單,但DuckDB具有豐富的功能集。它支持完整的SQL標準、事務、二級索引,并且與流行的數據分析編程語言如 Python 和 R 集成良好。
  • DuckDB 是免費的,任何人都可以使用和修改它,這降低了開發人員和數據分析師采用它的門檻。
  • DuckDB 兼容性很好,幾乎無依賴性,甚至可在瀏覽器中運行。
  • DuckDB 具有靈活的擴展機制,這對于直接從 CSV、JSON、Parquet、MySQL 或直接從 S3 讀取數據特別重要,能夠大大提高開發人員的體驗。
  • DuckDB 可提供數據超出內存限制但小于磁盤容量規模下的工作負載,這樣分析工作可通過 "便宜"的硬件來完成。

2. DuckDB 數據庫架構

圖片圖片

DuckDB 數據庫可分為多個組件:Parser、Logical Planner、Optimizer、Physical Planner、Execution Engine、Transaction and Storage Managers。

1).Parser

DuckDB SQL Parser 源自 Postgres SQL Parser。

2).Logical Planner

包含了兩個過程 binder、plan generator。前者是解析所有引用的 schema 中的對象(如 table 或 view)的表達式,將其與列名和類型匹配。后者將 binder 生成的 AST 轉換為由基本 logical query 查詢運算符組成的樹,就得到了一顆 type-resolved logical query plan。

3).Optimizer

優化器部分,會采用多種優化手段對 logical query plan 進行優化,最終生成 physical plan。例如,其內置一組 rewrite rules 來簡化 expression tree,例如執行公共子表達式消除和常量折疊。針對表關聯,會使用動態規劃進行 join order 的優化,針對復雜的 join graph 會 fallback 到貪心算法會消除所有的 subquery。

4).Execution Engine

DuckDB 最開始采用了基于 Pull-based 的 Vector Volcano 的執行引擎,后來切換到了 Push-based 的 pipelines 執行方法。DuckDB 采用了向量化計算來來加速計算,具有內部實現的多種類型的 vector 以及向量化的 operator。另外出于可移植性原因,沒有采用 JIT,因為 JIT引擎依賴于大型編譯器庫(例如LLVM),具有額外的傳遞依賴。

5).Transactions

DuckDB 通過 MVCC 提供了 ACID 的特性,實現了HyPer專門針對混合OLAP / OLTP系統定制的可串行化MVCC 變種 。該變種立即 in-place 更新數據,并將先前狀態存儲在單獨的 undo buffer 中,以供并發事務和 abort 使用。

6).Persistent Storage

DuckDB 使用面向讀取優化的 DataBlocks 存儲布局(單個文件)。邏輯表被水平分區為 chunks of columns,并使用輕量級壓縮方法壓縮成 physical block 。每個塊都帶有每列的min/max 索引,以便快速確定它們是否與查詢相關。此外,每個塊還帶有每列的輕量級索引,可以進一步限制掃描的值數量。

3. DuckDB 初體驗

1).部署安裝

DuckDB 提供了非常簡單的安裝方法,從官網 duckdb.org 直接下載安裝解壓即可使用。此外,DuckDB 還可以內置在多種開發語言中使用,下文會以 Python 舉例說明。

圖片圖片

2).啟動數據庫

DuckDB 啟動非常簡單,直接將安裝包解壓后執行即可。

[root@hfserver1 soft]# ./duckdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.

上文提示連接到內存庫。默認情況下,DuckDB 是運行在內存數據庫中,這意味著創建的任何表都存儲在內存中,而不是持久化到磁盤上??梢酝ㄟ^啟動命令行參數的方式,將 DuckDB 連接到磁盤上的持久化數據庫文件。任何寫入該數據庫連接的數據都將保存到磁盤文件中,并在重新連接到同一文件時重新加載。

[root@hfserver1 soft]# ls -al *db
-rwxr-xr-x 1 root root 44784232 Mar 18 20:47 duckdb
-rw-r--r-- 1 root root 18886656 Apr  9 16:06 testdb
[root@hfserver1 soft]# ./duckdb testdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
D PRAGMA database_list;
┌───────┬─────────┬─────────┐
│  seq         │  name            │  file            │
│ int64        │ varchar          │ varchar          │
├───────┼─────────┼─────────┤
│  1080        │ testdb           │ testdb           │
└───────┴─────────┴─────────┘

上面示例啟動到一個文件中,并通過 PRAGMA 命令查看下當前運行庫。

3).簡單 CRUD

[root@hfserver1 soft]# ./duckdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
-- 創建一張表
D create table t1( a int,b int);

-- 查看表
D .tables
t1

-- 插入數據
D insert into t1 values(1,1);

-- 修改輸出格式
D .mode table

-- 查看數據
D select * from t1;
+---+---+
| a | b |
+---+---+
| 1 | 1 |
+---+---+

-- 更新數據
D update t1 set b=2 where a=1;

-- 查看數據
D select * from t1;
+---+---+
| a | b |
+---+---+
| 1 | 2 |
+---+---+

-- 查看表結構
D describe t1;
+-------------+-------------+------+-----+---------+-------+
| column_name | column_type | null | key | default | extra |
+-------------+-------------+------+-----+---------+-------+
| a        | INTEGER   | YES  |   |      |     |
| b        | INTEGER   | YES  |   |      |       |
+-------------+-------------+------+-----+---------+-------+

4).數據加載

DuckDB 除了支持通常的insert插入數據外,也支持從CSV、JSON、Parquet、MySQL 等數據源中直接查詢或導入數據。

-- 讀取外部數據
D select * from read_csv('tmp.csv');
+----+-------+
| id | name  |
+----+-------+
| 1  | user1 |
| 2  | user2 |
| 3  | user3 |
+----+-------+

-- 加載數據到本地
D create table csv_table as select * from read_csv('tmp.csv');
D select count(*) from csv_table;
+--------------+
| count_star() |
+--------------+
| 3         |
+--------------+

-- COPY 復制數據
D COPY csv_table FROM 'tmp.csv';
D select count(*) from csv_table;
+--------------+
| count_star() |
+--------------+
| 6        |
+--------------+

5).應用集成

DuckDB 有個很強大的功能,就是可以方便的集成進應用,其支持常見的C、Java、Python、Go等。下文通過 Python 做個示例。

[root@hfserver1 soft]# pip install duckdb

[root@hfserver1 soft]# cat test.py
import duckdb

con = duckdb.connect("file.db")
con.sql("CREATE TABLE test (i INTEGER)")
con.sql("INSERT INTO test VALUES (42)")
con.table("test").show()
con.close()

[root@hfserver1 soft]# python test.py
┌───────┐
│   i          │
│ int32        │
├───────┤
│    42        │
└───────┘
6).插件擴展

DuckDB 通過插件進行能力的擴展,其支持很多不同的插件,能夠通過 INSTALL 和 LOAD來進行開關,可以使用 shared library 的方式進行加載。很多核心特性都是通過插件來實現的,例如:time zone, json, sqlite_scanner 等。下圖是 DuckDB 內置的一些插件。

圖片圖片

下文通過插件訪問 MySQL 庫做個示例。

[root@hfserver1 soft]# ./duckdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.

D install mysql;
100% ▕████████████████████████████████████████████████████████████▏
D
-- 加載本地的 MySQL 數據庫
D ATTACH 'host=localhost user=root port=3307 database=test' AS mysqldb (TYPE MYSQL);
D use mysqldb;
D show tables;
┌────────────────────┐
│        name                     │
│      varchar                      │
├────────────────────┤
│ AA                              │
│ COMMITTEE                          │
...

7).性能對比

DuckDB 定位是一款分析型數據庫,下文針對 DuckDB 與 MySQL 做個簡單的查詢性能對比。測試環境在MySQL中構建一張大表(百萬級)執行聚合查詢,然后通過插件功能導入到 DuckDB 中跑下同樣的示例。從跑出的數據來看,有十余倍的提升。

-- MySQL 環境
mysql> select count(*) from big_emp;
+----------+
| count(*) |
+----------+
|  1000000 |
+----------+

mysql> show create table big_emp\G;
*************************** 1. row ***************************
       Table: big_emp
Create Table: CREATE TABLE `big_emp` (
  `empno` int NOT NULL,
  `ename` varchar(30) DEFAULT NULL,
  `job` varchar(9) DEFAULT NULL,
  `mgr` int DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` int DEFAULT NULL,
  `comm` int DEFAULT NULL,
  `deptno` int DEFAULT NULL,
  PRIMARY KEY (`empno`),
  KEY `fk_deptno` (`deptno`),
  KEY `idx_sal` (`sal`),
  CONSTRAINT `fk_deptno1` FOREIGN KEY (`deptno`) REFERENCES `big_dept` (`deptno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select * from big_emp limit 3;
+-------+--------+-------+-------+------------+-------+------+---------+
| empno | ename  | job  | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-------+-------+------------+-------+------+--------+
|    1 | user1  | job  |    1 | 2000-01-01 |    1 |   1 |   925 |
|    2 | user2  | job  |    1 | 2000-01-01 |    1 |   1 |   594 |
|    3 | user3  | job  |    1 | 2000-01-01 |    1 |   1 |   307 |
+-------+--------+-------+------+-------------+-------+------+---------+

-- 構建 DuckDB 環境
[root@hfserver1 soft]# ./duckdb testdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
D ATTACH 'host=localhost user=root port=3307 database=test' AS mysqldb (TYPE MYSQL);
D create table big_emp as select * from mysqldb.big_emp;
100% 

-- 查詢對比
[root@hfserver1 soft]# time mysql -e "select deptno,count(*) from big_emp group by deptno" test
real    0m0.192s
user    0m0.014s
sys     0m0.000s

[root@hfserver1 soft]# time ./duckdb testdb -c "select deptno,count(*) from big_emp group by deptno"
real    0m0.015s
user    0m0.010s
sys     0m0.009sselect count(*) from big_emp;\n+----------+\n| count(*) |\n+----------+\n|  1000000 |\n+----------+\n\nmysql> show create table big_emp\\G;\n*************************** 1. row ***************************\n       Table: big_emp\nCreate Table: CREATE TABLE " big_emp="big_emp" n="n" empno="empno" int="int" not="not" null="null" ename="ename" varchar="varchar" default="default" job="job" mgr="mgr" hiredate="hiredate" date="date" sal="sal" comm="comm" deptno="deptno" primary="primary" key="key" fk_deptno="fk_deptno" idx_sal="idx_sal" cnotallow="constraint" fk_deptno1="fk_deptno1" foreign="foreign" references="references" big_dept="big_dept" engine="InnoDB" charset="latin1\n1" row="row" in="in" set="set" sec="sec" select="select" from="from" limit="limit" user1="user1" user2="user2" user3="user3" duckdb="duckdb" soft="soft" testdb="testdb" a89d97db8="a89d97db8" quot="quot" for="for" usage="usage" hints="hints" attach="attach" host="localhost" user="root" port="3307" database="test'" as="as" mysqldb="mysqldb" type="type" mysql="mysql" create="create" table="table" time="time" e="e" group="group" by="by" test="test" m0="m0" c="c" data-lark-record-format="docx/text" class="" style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">
-- MySQL 環境
mysql> select count(*) from big_emp;
+----------+
| count(*) |
+----------+
|  1000000 |
+----------+

mysql> show create table big_emp\G;
*************************** 1. row ***************************
       Table: big_emp
Create Table: CREATE TABLE `big_emp` (
  `empno` int NOT NULL,
  `ename` varchar(30) DEFAULT NULL,
  `job` varchar(9) DEFAULT NULL,
  `mgr` int DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` int DEFAULT NULL,
  `comm` int DEFAULT NULL,
  `deptno` int DEFAULT NULL,
  PRIMARY KEY (`empno`),
  KEY `fk_deptno` (`deptno`),
  KEY `idx_sal` (`sal`),
  CONSTRAINT `fk_deptno1` FOREIGN KEY (`deptno`) REFERENCES `big_dept` (`deptno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select * from big_emp limit 3;
+-------+--------+-------+-------+------------+-------+------+---------+
| empno | ename  | job  | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-------+-------+------------+-------+------+--------+
|    1 | user1  | job  |    1 | 2000-01-01 |    1 |   1 |   925 |
|    2 | user2  | job  |    1 | 2000-01-01 |    1 |   1 |   594 |
|    3 | user3  | job  |    1 | 2000-01-01 |    1 |   1 |   307 |
+-------+--------+-------+------+-------------+-------+------+---------+

-- 構建 DuckDB 環境
[root@hfserver1 soft]# ./duckdb testdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
D ATTACH 'host=localhost user=root port=3307 database=test' AS mysqldb (TYPE MYSQL);
D create table big_emp as select * from mysqldb.big_emp;
100% 

-- 查詢對比
[root@hfserver1 soft]# time mysql -e "select deptno,count(*) from big_emp group by deptno" test
real    0m0.192s
user    0m0.014s
sys     0m0.000s

[root@hfserver1 soft]# time ./duckdb testdb -c "select deptno,count(*) from big_emp group by deptno"
real    0m0.015s
user    0m0.010s
sys     0m0.009sselect count(*) from big_emp;\n+----------+\n| count(*) |\n+----------+\n|  1000000 |\n+----------+\n\nmysql> show create table big_emp\\G;\n*************************** 1. row ***************************\n       Table: big_emp\nCreate Table: CREATE TABLE " big_emp="big_emp" n="n" empno="empno" int="int" not="not" null="null" ename="ename" varchar="varchar" default="default" job="job" mgr="mgr" hiredate="hiredate" date="date" sal="sal" comm="comm" deptno="deptno" primary="primary" key="key" fk_deptno="fk_deptno" idx_sal="idx_sal" cnotallow="constraint" fk_deptno1="fk_deptno1" foreign="foreign" references="references" big_dept="big_dept" engine="InnoDB" charset="latin1\n1" row="row" in="in" set="set" sec="sec" select="select" from="from" limit="limit" user1="user1" user2="user2" user3="user3" duckdb="duckdb" soft="soft" testdb="testdb" a89d97db8="a89d97db8" quot="quot" for="for" usage="usage" hints="hints" attach="attach" host="localhost" user="root" port="3307" database="test'" as="as" mysqldb="mysqldb" type="type" mysql="mysql" create="create" table="table" time="time" e="e" group="group" by="by" test="test" m0="m0" c="c" data-lark-record-format="docx/text" class="" style="margin: 0px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important;">
-- MySQL 環境
mysql> select count(*) from big_emp;
+----------+
| count(*) |
+----------+
|  1000000 |
+----------+

mysql> show create table big_emp\G;
*************************** 1. row ***************************
       Table: big_emp
Create Table: CREATE TABLE `big_emp` (
  `empno` int NOT NULL,
  `ename` varchar(30) DEFAULT NULL,
  `job` varchar(9) DEFAULT NULL,
  `mgr` int DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` int DEFAULT NULL,
  `comm` int DEFAULT NULL,
  `deptno` int DEFAULT NULL,
  PRIMARY KEY (`empno`),
  KEY `fk_deptno` (`deptno`),
  KEY `idx_sal` (`sal`),
  CONSTRAINT `fk_deptno1` FOREIGN KEY (`deptno`) REFERENCES `big_dept` (`deptno`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> select * from big_emp limit 3;
+-------+--------+-------+-------+------------+-------+------+---------+
| empno | ename  | job  | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-------+-------+------------+-------+------+--------+
|    1 | user1  | job  |    1 | 2000-01-01 |    1 |   1 |   925 |
|    2 | user2  | job  |    1 | 2000-01-01 |    1 |   1 |   594 |
|    3 | user3  | job  |    1 | 2000-01-01 |    1 |   1 |   307 |
+-------+--------+-------+------+-------------+-------+------+---------+

-- 構建 DuckDB 環境
[root@hfserver1 soft]# ./duckdb testdb
v0.10.1 4a89d97db8
Enter ".help" for usage hints.
D ATTACH 'host=localhost user=root port=3307 database=test' AS mysqldb (TYPE MYSQL);
D create table big_emp as select * from mysqldb.big_emp;
100% 

-- 查詢對比
[root@hfserver1 soft]# time mysql -e "select deptno,count(*) from big_emp group by deptno" test
real    0m0.192s
user    0m0.014s
sys     0m0.000s

[root@hfserver1 soft]# time ./duckdb testdb -c "select deptno,count(*) from big_emp group by deptno"
real    0m0.015s
user    0m0.010s
sys     0m0.009s

8).參數管理

DuckDB 運維管理是比較簡單的,需要調整的一般就是參數部分??赏ㄟ^下面步驟來查看及修改。

-- 查看參數
D select name,value from duckdb_settings();
+-----------------------------------+-------------------------------------------+
|      ame                |         value                  |
+-----------------------------------+-------------------------------------------+
| access_mode                | automatic                       |
| allow_persistent_secrets       | true                          |
| checkpoint_threshold         | 16.0 MiB                        |
| debug_checkpoint_abort        | none                           |
| debug_force_external         | false                          |
| debug_force_no_cross_product    | false                          |
...
| Calendar                  | gregorian                       |
+-----------------------------------+-------------------------------------------+

-- 修改參數
D set threads=10;

-- 查看單個參數
D SELECT current_setting('threads') AS threads;
+---------+
| threads |
+---------+
| 10    |
+---------+
9).數據字典

DuckDB 仿照 MySQL 實現一組數據字典,此外也提供部分函數作為補充。

-- information_schema
information_schema.schemata: Database, Catalog and Schema
information_schema.tables: Tables and Views
information_schema.columns: Columns
information_schema.character_sets: Character Sets
information_schema.key_column_usage: Key Column Usage
information_schema.referential_constraints: Referential Constraints
information_schema.table_constraints: Table Constraints

-- catalog function
current_catalog() 
Return the name of the currently active catalog. Default is memory.
current_schema()        
Return the name of the currently active schema. Default is main.
current_schemas(boolean)        
Return list of schemas. Pass a parameter of true to include implicit schemas.
10).Pragma 擴展

PRAGMA 語句是DuckDB從SQLite中采用的SQL擴展。PRAGMA語句可以以與常規SQL語句類似的方式發出。PRAGMA命令可能會改變數據庫引擎的內部狀態,并可能影響引擎的后續執行或行為。

-- 數據庫信息
D PRAGMA database_list;
+------+------+---------------------------------------+
| seq  | name |                 file                  |
+------+------+---------------------------------------+
| 1080 | file | ...file.db                            |
+------+------+---------------------------------------+

-- 數據庫信息(大?。?D CALL pragma_database_size();
+---------------+---------------+------------+--------------+-------------+-------------+----------+--------------+--------------+
| database_name | database_size | block_size | total_blocks | used_blocks | free_blocks | wal_size | memory_usage | memory_limit |
+---------------+---------------+------------+--------------+-------------+-------------+----------+--------------+--------------+
| file          | 512.0 KiB     | 262144     | 2            | 2           | 0           | 0 bytes  | 256.0 KiB    | 25.0 GiB     |
+---------------+---------------+------------+--------------+-------------+-------------+----------+--------------+--------------+

-- 所有表信息
D PRAGMA show_tables;
+------+
| name |
+------+
| t1   |
| t2   |
| test |
+------+

-- 表詳細信息
D PRAGMA show_tables_expanded;
+----------+--------+------+--------------+--------------------+-----------+
| database | schema | name | column_names |    column_types    | temporary |
+----------+--------+------+--------------+--------------------+-----------+
| file     | main   | t1   | [a, b]       | [INTEGER, INTEGER] | false     |
| file     | main   | t2   | [a, b]       | [INTEGER, INTEGER] | false     |
| file     | main   | test | [i]          | [INTEGER]          | false     |
+----------+--------+------+--------------+--------------------+-----------+

-- 函數信息
D PRAGMA functions;
D PRAGMA functions;
┌────────────┬─────────┬────────────────────────┬─────────┬─────────────┬──────────────┐
│    name    │  type   │       parameters       │ varargs │ return_type │ side_effects │
│  varchar   │ varchar │       varchar[]        │ varchar │   varchar   │   boolean    │
├────────────┼─────────┼────────────────────────┼─────────┼─────────────┼──────────────┤
│ !__postfix │ SCALAR  │ [INTEGER]              │         │ HUGEINT     │ false        │
│ !~~        │ SCALAR  │ [VARCHAR, VARCHAR]     │         │ BOOLEAN     │ false        │
│ !~~*       │ SCALAR  │ [VARCHAR, VARCHAR]     │         │ BOOLEAN     │ false        │
│ %          │ SCALAR  │ [SMALLINT, SMALLINT]   │         │ SMALLINT    │ false        │
│ %          │ SCALAR  │ [UBIGINT, UBIGINT]     │         │ UBIGINT     │ false        │
│ %          │ SCALAR  │ [UINTEGER, UINTEGER]   │         │ UINTEGER    │ false        │
...

-- 表結構
D PRAGMA table_info('t1');
+-----+------+---------+---------+------------+-------+
| cid | name |  type   | notnull | dflt_value |  pk   |
+-----+------+---------+---------+------------+-------+
| 0   | a    | INTEGER | false   |            | false |
| 1   | b    | INTEGER | false   |            | false |
+-----+------+---------+---------+------------+-------+

-- 版本與平臺
D PRAGMA version;
+-----------------+------------+
| library_version | source_id  |
+-----------------+------------+
| v0.10.1         | 4a89d97db8 |
+-----------------+------------+

D PRAGMA platform;
+---------------+
|   platform    |
+---------------+
| windows_amd64 |
+---------------+

-- Profiling
PRAGMA enable_profiling;
SET profiling_mode = 'detailed';
SET enable_profiling = 'query_tree';    //logical query plan:
SET enable_profiling = 'query_tree_optimizer';    //physical query plan:
PRAGMA disable_profiling;

-- Optimizer
PRAGMA disable_optimizer;
PRAGMA enable_optimizer;

-- Storage Info
D PRAGMA storage_info('t1');
+--------------+-------------+-----------+-------------+------------+--------------+-------+-------+--------------+------------------------------------------------------+-------------+------------+----------+--------------+--------------+
| row_group_id | column_name | column_id | column_path | segment_id | segment_type | start | count | compression  |  stats                                               | has_updates | persistent | block_id | block_offset | segment_info |
+--------------+-------------+-----------+-------------+------------+--------------+-------+-------+--------------+------------------------------------------------------+-------------+------------+----------+--------------+--------------+
| 0            | a           | 0         | [0]         | 0          | INTEGER      | 0     | 3     | Uncompressed | [Min: 1, Max: 3][Has Null: false, Has No Null: true] | false       | true       | 1        | 0            |              |
| 0            | a           | 0         | [0, 0]      | 0          | VALIDITY     | 0     | 3     | Constant     | [Has Null: false, Has No Null: true]                 | false       | true       | -1       | 0            |              |
| 0            | b           | 1         | [1]         | 0          | INTEGER      | 0     | 3     | Uncompressed | [Min: 1, Max: 3][Has Null: false, Has No Null: true] | false       | true       | 1        | 16           |              |
| 0            | b           | 1         | [1, 0]      | 0          | VALIDITY     | 0     | 3     | Constant     | [Has Null: false, Has No Null: true]                 | false       | true       | -1       | 0            |              |
+--------------+-------------+-----------+-------------+------------+--------------+-------+-------+--------------+------------------------------------------------------+-------------+------------+----------+--------------+--------------+

11).性能調優

DuckDB 性能調優主要涉及到參數、執行計劃等。這里簡單說明下使用 Explain 命令查看執行計劃。

-- 查看執行計劃
D explain select deptno,count(*) from big_emp group by deptno;

┌─────────────────────────────┐
│┌───────────────────────────┐│
││       Physical Plan                             ││
│└───────────────────────────┘│
└─────────────────────────────┘
┌───────────────────────────┐
│         PROJECTION                            │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─                 │
│__internal_decompress_integ                           │
│     ral_integer(#0, 1)                           │
│             #1                            │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│   PERFECT_HASH_GROUP_BY                            │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─                 │
│             #0                            │
│        count_star()                           │
└─────────────┬─────────────┘
┌─────────────┴─────────────┐
│         PROJECTION                            │
│   ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─                 │
│           deptno                            │


責任編輯:武曉燕 來源: 韓鋒頻道
相關推薦

2023-10-08 16:28:36

數據庫DuckDB

2017-11-18 23:48:36

戴爾

2018-07-11 10:48:04

智能辦公

2024-06-06 12:57:11

2015-07-14 09:11:11

Web服務器Siege

2016-06-20 17:48:49

戴爾SC4020

2016-11-15 13:52:19

2013-05-29 08:59:54

移動互聯網創業移動創業

2015-09-24 10:28:17

魅族品牌形象小而美

2016-01-15 14:30:27

七牛

2013-12-24 10:07:54

信息安全安全寶

2014-06-23 10:36:32

2017-06-22 15:50:12

互聯網

2024-06-17 13:39:45

2017-08-10 14:55:33

前端JavaScript 函數

2017-04-13 09:20:07

移動互聯網產品指標分析

2015-04-22 11:36:36

Xamarin

2010-09-28 11:11:09

Flachcache

2021-04-14 10:47:12

區塊鏈比特幣以太坊
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品一区二区网址 | 国产精品久久久久久久7电影 | 欧美影院久久 | 精品伊人久久 | 中文字幕 视频一区 | 一级全黄视频 | 日韩一区二区三区精品 | 亚洲精品在线视频 | 鸳鸯谱在线观看高清 | 亚洲国产精品久久久久秋霞不卡 | 日韩一区二区三区在线观看视频 | 高清一区二区三区 | 日韩精品成人一区二区三区视频 | 91久久精品国产91久久 | 国产在线视频一区二区 | 久久久高清 | 污视频在线免费观看 | 二区三区视频 | 天天操天天玩 | 欧美一级一区 | 久久一区精品 | 国产精品99久久久精品免费观看 | 国产精品日韩欧美一区二区三区 | 亚洲精品久久久久久久久久久 | 亚洲国产成人精品女人久久久 | 欧美日批 | 麻豆av免费观看 | a毛片| 神马久久久久久久久久 | 少妇黄色 | 成人免费一区二区三区视频网站 | 国产欧美日韩在线 | 欧美日韩一卡 | 97国产精品 | 国产又爽又黄的视频 | 人人叉 | 影音先锋久久 | 久久久久国产精品 | 欧美高清视频一区 | 人妖一区 | 女人av|