使用并行提高查詢效率
更新時間 2025-02-05 09:36:58
最近更新時間: 2025-02-05 09:36:58
分享文章
本頁介紹天翼云TeleDB數據庫使用并行提高查詢效率的最佳實踐。
在大表查詢時,為充分利用服務器資源,可以嘗試開啟并行,多進程并發查詢,提升查詢效率。在執行計劃中,算子前增加關鍵字Partial,同時有關鍵字 Workers Planned: xx,表示SQL使用了并行。
例如下面的SQL,未開啟并行。
teledb=# explain select count(1) from teledb_1;
QUERY PLAN
---------------------------------------------------------------------------------------
Finalize Aggregate (cost=118.81..118.83 rows=1 width=8)
-> Remote Subquery Scan on all (dn001,dn002) (cost=118.80..118.81 rows=1 width=0)
-> Partial Aggregate (cost=18.80..18.81 rows=1 width=8)
-> Seq Scan on teledb_1 (cost=0.00..18.80 rows=880 width=0)
(4 rows)
下面是開啟并行查詢后的執行計劃。
teledb=# explain select count(1) from teledb_1;
QUERY PLAN
----------------------------------------------------------------------------------------------------
Parallel Finalize Aggregate (cost=14728.45..14728.46 rows=1 width=8)
-> Parallel Remote Subquery Scan on all (dn001,dn002) (cost=14728.33..14728.45 rows=1 width=0)
-> Gather (cost=14628.33..14628.44 rows=1 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=13628.33..13628.34 rows=1 width=8)
-> Parallel Seq Scan on teledb_1 (cost=0.00..12586.67 rows=416667 width=0)
(6 rows)
建議不要全局開啟并行,僅在需要開啟并行的具體SQL上開啟,可通過會話級設置并行,或hint方式指定并行。
會話級設置如:
set max_parallel_workers_per_gather=2;
執行SQL;
set max_parallel_workers_per_gather=0;