2017-08-09 24 views
0

我有查詢的性能涉及掙扎精心慢一個「簡單」的左連接上int -column和tstzrange簡單的INT加盟+ tstzrange欄上只約1萬行

SELECT 
     table_1.id_col 
    , table_1.time_range 
    , table_1.other_col_1 
    , table_2.other_col_2 
FROM table_1 
LEFT JOIN table_2 
ON table_1.id_col = table_2.id_col 
AND table_1.time_range = table_2.time_range 

這種查詢採用〜80-100秒運行爲最終結果集的〜一百萬行table_1table_2是相同的數量級上)

該查詢的一個更復雜的012部查詢(實際上選擇這些行的一個小子集),但我已經提出了呈現瓶頸的部分。

我已經添加(我認爲)是這些列的組合的適當索引(GIST-index),但從解釋我猜這將被丟棄,當我基本上加入幾乎所有的行。

有沒有提高性能的方法?

就像爲物理順序掃描順序掃描一樣?

我的表:

CREATE TABLE data.table_1 (
    table_1_id SERIAL NOT NULL, 
    id_col INTEGER NOT NULL, 
    time_range TSTZRANGE NOT NULL, 
    other_col_1 INTEGER, 
    PRIMARY KEY (table_1_id), 
); 

CREATE INDEX idx_table_1_id_col ON data.table_1 (id_col); 
CREATE INDEX idx_table_1_time_range ON data.table_1 USING gist (time_range); 
CREATE INDEX idx_table_1_id_col_time_range ON data.table_1 USING gist (id_col, time_range); 

CREATE TABLE data.table_2 (
    table_2_id SERIAL NOT NULL, 
    id_col INTEGER NOT NULL, 
    time_range TSTZRANGE NOT NULL, 
    other_col_2 DOUBLE PRECISION, 
    PRIMARY KEY (table_2_id), 
); 

CREATE INDEX idx_table_2_id_col ON data.table_2 (id_col); 
CREATE INDEX idx_table_2_time_range ON data.table_2 USING gist (time_range); 
CREATE INDEX idx_table_2_id_col_time_range ON data.table_2 USING gist (id_col, time_range); 

下面是詳細的說明:

[ 
    { 
    "Plan": { 
     "Node Type": "Hash Join", 
     "Join Type": "Left", 
     "Startup Cost": 198185.10, 
     "Total Cost": 4163704.54, 
     "Plan Rows": 73508636, 
     "Plan Width": 20, 
     "Actual Startup Time": 31055.086, 
     "Actual Total Time": 89488.540, 
     "Actual Rows": 1015568, 
     "Actual Loops": 1, 
     "Output": ["table_1.id_col", "table_1.other_col_1", "table_2.other_col_2"], 
     "Hash Cond": "((table_1.id_col = table_2.id_col) AND (table_1.time_range = table_2.time_range))", 
     "Shared Hit Blocks": 165149, 
     "Shared Read Blocks": 632793, 
     "Shared Dirtied Blocks": 0, 
     "Shared Written Blocks": 0, 
     "Local Hit Blocks": 0, 
     "Local Read Blocks": 0, 
     "Local Dirtied Blocks": 0, 
     "Local Written Blocks": 0, 
     "Temp Read Blocks": 38220, 
     "Temp Written Blocks": 37966, 
     "I/O Read Time": 0.000, 
     "I/O Write Time": 0.000, 
     "Plans": [ 
     { 
      "Node Type": "Seq Scan", 
      "Parent Relationship": "Outer", 
      "Relation Name": "table_1", 
      "Schema": "data", 
      "Alias": "table_1", 
      "Startup Cost": 0.00, 
      "Total Cost": 1492907.36, 
      "Plan Rows": 73508636, 
      "Plan Width": 34, 
      "Actual Startup Time": 24827.453, 
      "Actual Total Time": 77143.930, 
      "Actual Rows": 904431, 
      "Actual Loops": 1, 
      "Output": ["table_1.id_col", "table_1.other_col_1", "table_1.time_range"], 
      "Shared Hit Blocks": 165147, 
      "Shared Read Blocks": 592674, 
      "Shared Dirtied Blocks": 0, 
      "Shared Written Blocks": 0, 
      "Local Hit Blocks": 0, 
      "Local Read Blocks": 0, 
      "Local Dirtied Blocks": 0, 
      "Local Written Blocks": 0, 
      "Temp Read Blocks": 0, 
      "Temp Written Blocks": 0, 
      "I/O Read Time": 0.000, 
      "I/O Write Time": 0.000 
     }, 
     { 
      "Node Type": "Hash", 
      "Parent Relationship": "Inner", 
      "Startup Cost": 88292.64, 
      "Total Cost": 88292.64, 
      "Plan Rows": 4817164, 
      "Plan Width": 34, 
      "Actual Startup Time": 6204.927, 
      "Actual Total Time": 6204.927, 
      "Actual Rows": 4817085, 
      "Actual Loops": 1, 
      "Output": ["table_2.other_col_2", "table_2.id_col", "table_2.time_range"], 
      "Hash Buckets": 65536, 
      "Original Hash Buckets": 65536, 
      "Hash Batches": 128, 
      "Original Hash Batches": 128, 
      "Peak Memory Usage": 2930, 
      "Shared Hit Blocks": 2, 
      "Shared Read Blocks": 40119, 
      "Shared Dirtied Blocks": 0, 
      "Shared Written Blocks": 0, 
      "Local Hit Blocks": 0, 
      "Local Read Blocks": 0, 
      "Local Dirtied Blocks": 0, 
      "Local Written Blocks": 0, 
      "Temp Read Blocks": 0, 
      "Temp Written Blocks": 31422, 
      "I/O Read Time": 0.000, 
      "I/O Write Time": 0.000, 
      "Plans": [ 
      { 
       "Node Type": "Seq Scan", 
       "Parent Relationship": "Outer", 
       "Relation Name": "table_2", 
       "Schema": "data", 
       "Alias": "table_2", 
       "Startup Cost": 0.00, 
       "Total Cost": 88292.64, 
       "Plan Rows": 4817164, 
       "Plan Width": 34, 
       "Actual Startup Time": 0.650, 
       "Actual Total Time": 3769.157, 
       "Actual Rows": 4817085, 
       "Actual Loops": 1, 
       "Output": ["table_2.other_col_2", "table_2.id_col", "table_2.time_range"], 
       "Shared Hit Blocks": 2, 
       "Shared Read Blocks": 40119, 
       "Shared Dirtied Blocks": 0, 
       "Shared Written Blocks": 0, 
       "Local Hit Blocks": 0, 
       "Local Read Blocks": 0, 
       "Local Dirtied Blocks": 0, 
       "Local Written Blocks": 0, 
       "Temp Read Blocks": 0, 
       "Temp Written Blocks": 0, 
       "I/O Read Time": 0.000, 
       "I/O Write Time": 0.000 
      } 
      ] 
     } 
     ] 
    }, 
    "Planning Time": 0.350, 
    "Triggers": [ 
    ], 
    "Execution Time": 89689.809 
    } 
] 
+0

難道你不能在這個查詢中直接放置一些條件(我想你會過濾這些結果)? –

+0

@洛倫佐卡塔拉諾,但是它是通過CTE引發的條件間接完成的。我基本上有一些其他的表,其中上述子集加入英寸(如果這是有道理的) – salient

+0

看起來像一個正常的連接,我不能確切地告訴平原說,但我看到像「計劃行」:73508636,什麼這是否意味着? –

回答

0

排序物理使用CLUSTER減少查詢時間降到〜5秒其是行的數據,考慮到我將進一步選擇行的子集:

CLUSTER table_1 USING idx_table_1_id_col_time_range; 
CLUSTER table_2 USING idx_table_2_id_col_time_range;