全部產品
Search
文件中心

PolarDB:DML支援GLOBAL INDEX SCAN

更新時間:Jul 06, 2024

當分區表中存在GLOBAL INDEX時,可以使用GLOBAL INDEX SCAN進行DML(DELETE、UPDATE)操作。

使用指南

  • 建立分區表和全域索引(GLOBAL INDEX):
    CREATE TABLE gi_ora
    (
        a int,
        b int,
        c int GENERATED ALWAYS AS (b * 2),
        d int DEFAULT 0,
        e int,
        f text,
        g text
    ) PARTITION BY RANGE (a)
    (
        partition p0 values less than (50),
        partition p1 values less than (100),
        partition p2 values less than (200),
        partition p3 values less than (300),
        partition p4 values less than (400),
        partition p5 values less than (500),
        partition p6 values less than (600),
        partition p7 values less than (700),
        partition p8 values less than (800)
    );
    
    INSERT INTO gi_ora(a,b,d,e,f,g) SELECT i,i,i, (random()*1000)::int,(random()*30000000)::int::text,(random()*30000000)::int::text FROM generate_series(1,799) i ;
    
    CREATE INDEX ON gi_ora(b) global;
    ANALYZE gi_ora;
  • 使用GLOBAL INDEX SCAN對指定資料進行刪除操作:

    EXPLAIN (costs off, verbose on)  DELETE FROM gi_ora WHERE b = 198;
    顯示結果如下:
                                     QUERY PLAN
    ----------------------------------------------------------------------------
     DELETE ON global_index_dml.gi_ora
       ->  GLOBAL INDEX SCAN USING gi_ora_global_idx ON global_index_dml.gi_ora
             Output: gi_ora.tableoid, gi_ora.ctid
             Index Cond: (gi_ora.b = 198)
    (4 rows)
  • 使用GLOBAL INDEX SCAN對指定資料進行更新操作:

    EXPLAIN (costs off, verbose on)  UPDATE gi_ora SET a = a - 10 WHERE b = 198;
    顯示結果如下:
                                     QUERY PLAN
    ----------------------------------------------------------------------------
     UPDATE ON global_index_dml.gi_ora
       ->  GLOBAL INDEX SCAN USING gi_ora_global_idx ON global_index_dml.gi_ora
             Output: (gi_ora.a - 10), gi_ora.tableoid, gi_ora.ctid
             Index Cond: (gi_ora.b = 198)
    (4 rows)