最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

postgresql insert into select無(wú)法使用并行查詢的解決

 更新時(shí)間:2021年01月08日 10:40:23   作者:瀚高PG實(shí)驗(yàn)室  
這篇文章主要介紹了postgresql insert into select無(wú)法使用并行查詢的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

本文信息基于PG13.1。

從PG9.6開(kāi)始支持并行查詢。PG11開(kāi)始支持CREATE TABLE … AS、SELECT INTO以及CREATE MATERIALIZED VIEW的并行查詢。

先說(shuō)結(jié)論:

換用create table as 或者select into或者導(dǎo)入導(dǎo)出。

首先跟蹤如下查詢語(yǔ)句的執(zhí)行計(jì)劃:

select count(*) from test t1,test1 t2 where t1.id = t2.id ;
postgres=# explain analyze select count(*) from test t1,test1 t2 where t1.id = t2.id ;
                                    QUERY PLAN                                    
--------------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate (cost=34244.16..34244.17 rows=1 width=8) (actual time=683.246..715.324 rows=1 loops=1)
  -> Gather (cost=34243.95..34244.16 rows=2 width=8) (actual time=681.474..715.311 rows=3 loops=1)
     Workers Planned: 2
     Workers Launched: 2
     -> Partial Aggregate (cost=33243.95..33243.96 rows=1 width=8) (actual time=674.689..675.285 rows=1 loops=3)
        -> Parallel Hash Join (cost=15428.00..32202.28 rows=416667 width=0) (actual time=447.799..645.689 rows=333333 loops=3)
           Hash Cond: (t1.id = t2.id)
           -> Parallel Seq Scan on test t1 (cost=0.00..8591.67 rows=416667 width=4) (actual time=0.025..74.010 rows=333333 loops=3)
           -> Parallel Hash (cost=8591.67..8591.67 rows=416667 width=4) (actual time=260.052..260.053 rows=333333 loops=3)
              Buckets: 131072 Batches: 16 Memory Usage: 3520kB
              -> Parallel Seq Scan on test1 t2 (cost=0.00..8591.67 rows=416667 width=4) (actual time=0.032..104.804 rows=333333 loops=3)
 Planning Time: 0.420 ms
 Execution Time: 715.447 ms
(13 rows)

可以看到走了兩個(gè)Workers。

下邊看一下insert into select:

postgres=# explain analyze insert into va select count(*) from test t1,test1 t2 where t1.id = t2.id ;     
                                  QUERY PLAN                                  
--------------------------------------------------------------------------------------------------------------------------------------------------
 Insert on va (cost=73228.00..73228.02 rows=1 width=4) (actual time=3744.179..3744.187 rows=0 loops=1)
  -> Subquery Scan on "*SELECT*" (cost=73228.00..73228.02 rows=1 width=4) (actual time=3743.343..3743.352 rows=1 loops=1)
     -> Aggregate (cost=73228.00..73228.01 rows=1 width=8) (actual time=3743.247..3743.254 rows=1 loops=1)
        -> Hash Join (cost=30832.00..70728.00 rows=1000000 width=0) (actual time=1092.295..3511.301 rows=1000000 loops=1)
           Hash Cond: (t1.id = t2.id)
           -> Seq Scan on test t1 (cost=0.00..14425.00 rows=1000000 width=4) (actual time=0.030..421.537 rows=1000000 loops=1)
           -> Hash (cost=14425.00..14425.00 rows=1000000 width=4) (actual time=1090.078..1090.081 rows=1000000 loops=1)
              Buckets: 131072 Batches: 16 Memory Usage: 3227kB
              -> Seq Scan on test1 t2 (cost=0.00..14425.00 rows=1000000 width=4) (actual time=0.021..422.768 rows=1000000 loops=1)
 Planning Time: 0.511 ms
 Execution Time: 3745.633 ms
(11 rows)

可以看到并沒(méi)有Workers的指示,沒(méi)有啟用并行查詢。

即使開(kāi)啟強(qiáng)制并行,也無(wú)法走并行查詢。

postgres=# set force_parallel_mode =on;
SET
postgres=# explain analyze insert into va select count(*) from test t1,test1 t2 where t1.id = t2.id ;
                                  QUERY PLAN                                  
--------------------------------------------------------------------------------------------------------------------------------------------------
 Insert on va (cost=73228.00..73228.02 rows=1 width=4) (actual time=3825.042..3825.049 rows=0 loops=1)
  -> Subquery Scan on "*SELECT*" (cost=73228.00..73228.02 rows=1 width=4) (actual time=3824.976..3824.984 rows=1 loops=1)
     -> Aggregate (cost=73228.00..73228.01 rows=1 width=8) (actual time=3824.972..3824.978 rows=1 loops=1)
        -> Hash Join (cost=30832.00..70728.00 rows=1000000 width=0) (actual time=1073.587..3599.402 rows=1000000 loops=1)
           Hash Cond: (t1.id = t2.id)
           -> Seq Scan on test t1 (cost=0.00..14425.00 rows=1000000 width=4) (actual time=0.034..414.965 rows=1000000 loops=1)
           -> Hash (cost=14425.00..14425.00 rows=1000000 width=4) (actual time=1072.441..1072.443 rows=1000000 loops=1)
              Buckets: 131072 Batches: 16 Memory Usage: 3227kB
              -> Seq Scan on test1 t2 (cost=0.00..14425.00 rows=1000000 width=4) (actual time=0.022..400.624 rows=1000000 loops=1)
 Planning Time: 0.577 ms
 Execution Time: 3825.923 ms
(11 rows)

原因在官方文檔有寫:

The query writes any data or locks any database rows. If a query contains a data-modifying operation either at the top level or within a CTE, no parallel plans for that query will be generated. As an exception, the commands CREATE TABLE … AS, SELECT INTO, and CREATE MATERIALIZED VIEW which create a new table and populate it can use a parallel plan.

解決方案有如下三種:

1.select into

postgres=# explain analyze select count(*) into vaa from test t1,test1 t2 where t1.id = t2.id ;
                                    QUERY PLAN                                    
--------------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate (cost=34244.16..34244.17 rows=1 width=8) (actual time=742.736..774.923 rows=1 loops=1)
  -> Gather (cost=34243.95..34244.16 rows=2 width=8) (actual time=740.223..774.907 rows=3 loops=1)
     Workers Planned: 2
     Workers Launched: 2
     -> Partial Aggregate (cost=33243.95..33243.96 rows=1 width=8) (actual time=731.408..731.413 rows=1 loops=3)
        -> Parallel Hash Join (cost=15428.00..32202.28 rows=416667 width=0) (actual time=489.880..700.830 rows=333333 loops=3)
           Hash Cond: (t1.id = t2.id)
           -> Parallel Seq Scan on test t1 (cost=0.00..8591.67 rows=416667 width=4) (actual time=0.033..87.479 rows=333333 loops=3)
           -> Parallel Hash (cost=8591.67..8591.67 rows=416667 width=4) (actual time=266.839..266.840 rows=333333 loops=3)
              Buckets: 131072 Batches: 16 Memory Usage: 3520kB
              -> Parallel Seq Scan on test1 t2 (cost=0.00..8591.67 rows=416667 width=4) (actual time=0.058..106.874 rows=333333 loops=3)
 Planning Time: 0.319 ms
 Execution Time: 783.300 ms
(13 rows)

2.create table as

postgres=# explain analyze create table vb as select count(*) from test t1,test1 t2 where t1.id = t2.id ;
                                   QUERY PLAN                                    
-------------------------------------------------------------------------------------------------------------------------------------------------------
 Finalize Aggregate (cost=34244.16..34244.17 rows=1 width=8) (actual time=540.120..563.733 rows=1 loops=1)
  -> Gather (cost=34243.95..34244.16 rows=2 width=8) (actual time=537.982..563.720 rows=3 loops=1)
     Workers Planned: 2
     Workers Launched: 2
     -> Partial Aggregate (cost=33243.95..33243.96 rows=1 width=8) (actual time=526.602..527.136 rows=1 loops=3)
        -> Parallel Hash Join (cost=15428.00..32202.28 rows=416667 width=0) (actual time=334.532..502.793 rows=333333 loops=3)
           Hash Cond: (t1.id = t2.id)
           -> Parallel Seq Scan on test t1 (cost=0.00..8591.67 rows=416667 width=4) (actual time=0.018..57.819 rows=333333 loops=3)
           -> Parallel Hash (cost=8591.67..8591.67 rows=416667 width=4) (actual time=189.502..189.503 rows=333333 loops=3)
              Buckets: 131072 Batches: 16 Memory Usage: 3520kB
              -> Parallel Seq Scan on test1 t2 (cost=0.00..8591.67 rows=416667 width=4) (actual time=0.023..77.786 rows=333333 loops=3)
 Planning Time: 0.189 ms
 Execution Time: 565.448 ms
(13 rows)

3.或者通過(guò)導(dǎo)入導(dǎo)出的方式,例如:

psql -h localhost -d postgres -U postgres -c "select count(*) from test t1,test1 t2 where t1.id = t2.id " -o result.csv -A -t -F ","
psql -h localhost -d postgres -U postgres -c "COPY va FROM 'result.csv' WITH (FORMAT CSV, DELIMITER ',', HEADER FALSE, ENCODING 'windows-1252')"

一些場(chǎng)景下也會(huì)比非并行快。

到此這篇關(guān)于postgresql insert into select無(wú)法使用并行查詢的解決的文章就介紹到這了,更多相關(guān)postgresql insert into select并行查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PostgreSQL中ON?CONFLICT的使用及一些擴(kuò)展用法

    PostgreSQL中ON?CONFLICT的使用及一些擴(kuò)展用法

    Postgres?ON?CONFLICT是PostgreSQL數(shù)據(jù)庫(kù)中的一個(gè)功能,用于處理插入或更新數(shù)據(jù)時(shí)的沖突情況,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL中ON?CONFLICT的使用及一些擴(kuò)展用法的相關(guān)資料,需要的朋友可以參考下
    2024-06-06
  • PostgreSQL數(shù)據(jù)庫(kù)備份與恢復(fù)的四種辦法

    PostgreSQL數(shù)據(jù)庫(kù)備份與恢復(fù)的四種辦法

    在數(shù)據(jù)為王的時(shí)代,數(shù)據(jù)庫(kù)中存儲(chǔ)的信息堪稱企業(yè)的生命線,而PostgreSQL作為一款廣泛應(yīng)用的開(kāi)源數(shù)據(jù)庫(kù),學(xué)會(huì)如何妥善進(jìn)行備份與恢復(fù)操作,是每個(gè)開(kāi)發(fā)者與運(yùn)維人員必備的技能,今天,咱們就深入探究一下PostgreSQL相關(guān)的備份恢復(fù)策略,并附上豐富的代碼示例
    2025-01-01
  • postgresql 刪除重復(fù)數(shù)據(jù)案例詳解

    postgresql 刪除重復(fù)數(shù)據(jù)案例詳解

    這篇文章主要介紹了postgresql 刪除重復(fù)數(shù)據(jù)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 查看postgresql數(shù)據(jù)庫(kù)用戶系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法

    查看postgresql數(shù)據(jù)庫(kù)用戶系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法

    這篇文章主要介紹了查看postgresql數(shù)據(jù)庫(kù)用戶系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • PGSQL查詢最近N天的數(shù)據(jù)及SQL語(yǔ)句實(shí)現(xiàn)替換字段內(nèi)容

    PGSQL查詢最近N天的數(shù)據(jù)及SQL語(yǔ)句實(shí)現(xiàn)替換字段內(nèi)容

    PostgreSQL提供了WITH語(yǔ)句,允許你構(gòu)造用于查詢的輔助語(yǔ)句,下面這篇文章主要給大家介紹了關(guān)于PGSQL查詢最近N天的數(shù)據(jù)及SQL語(yǔ)句實(shí)現(xiàn)替換字段內(nèi)容的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • PostgreSQL自動(dòng)更新時(shí)間戳實(shí)例代碼

    PostgreSQL自動(dòng)更新時(shí)間戳實(shí)例代碼

    最近有這么一個(gè)工程,需要使用postgresql數(shù)據(jù)庫(kù),在數(shù)據(jù)庫(kù)中的好幾個(gè)表中都需要時(shí)間戳這個(gè)字段,這篇文章主要給大家介紹了關(guān)于PostgreSQL自動(dòng)更新時(shí)間戳的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • PostgreSQL 復(fù)制表的 5 種方式詳解

    PostgreSQL 復(fù)制表的 5 種方式詳解

    PostgreSQL 提供了多種不同的復(fù)制表的方法,它們的差異在于是否需要復(fù)制表結(jié)構(gòu)或者數(shù)據(jù),這篇文章主要介紹了PostgreSQL 復(fù)制表的 5 種方式,需要的朋友可以參考下
    2023-01-01
  • 關(guān)于PostgreSQL JSONB的匹配和交集問(wèn)題

    關(guān)于PostgreSQL JSONB的匹配和交集問(wèn)題

    這篇文章主要介紹了PostgreSQL JSONB的匹配和交集問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Postgresql中json和jsonb類型區(qū)別解析

    Postgresql中json和jsonb類型區(qū)別解析

    在我們的業(yè)務(wù)開(kāi)發(fā)中,可能會(huì)因?yàn)樘厥狻練v史,偷懶,防止表連接】經(jīng)常會(huì)有JSON或者JSONArray類的數(shù)據(jù)存儲(chǔ)到某列中,這個(gè)時(shí)候再PG數(shù)據(jù)庫(kù)中有兩種數(shù)據(jù)格式可以直接一對(duì)多或者一對(duì)一的映射對(duì)象,接下來(lái)通過(guò)本文介紹Postgresql中json和jsonb類型區(qū)別,需要的朋友可以參考下
    2024-06-06
  • 詳解如何優(yōu)化在PostgreSQL中對(duì)于日期范圍的查詢

    詳解如何優(yōu)化在PostgreSQL中對(duì)于日期范圍的查詢

    在 PostgreSQL 中,處理日期范圍的查詢是常見(jiàn)的操作,然而,如果不進(jìn)行適當(dāng)?shù)膬?yōu)化,這些查詢可能會(huì)導(dǎo)致性能問(wèn)題,特別是在處理大型數(shù)據(jù)集時(shí),本文章將詳細(xì)討論如何優(yōu)化在 PostgreSQL 中對(duì)于日期范圍的查詢,需要的朋友可以參考下
    2024-07-07

最新評(píng)論

吕梁市| 科技| 平谷区| 迁西县| 观塘区| 宜兴市| 华容县| 扶绥县| 蕉岭县| 高陵县| 米泉市| 洪江市| 聂荣县| 老河口市| 保康县| 望奎县| 南木林县| 南充市| 扬中市| 太白县| 南京市| 秦安县| 莒南县| 台州市| 肇庆市| 诸城市| 德令哈市| 临武县| 鹤庆县| 黄骅市| 鄱阳县| 揭东县| 隆尧县| 准格尔旗| 新建县| 红河县| 柞水县| 垣曲县| 黄山市| 武川县| 卓资县|