-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


PostgreSQL 6

, 18 2017 . 10:39 +
erogov 10:39

PostgreSQL 6


    PostgreSQL, : -, B- GiST. SP-GiST.

    SP-GiST


    . GiST . : , generalized search trees, , .

    SP space partitioning, . , , . , , , .

    SP-GiST , . (quadtree), k- (k-D tree), (trie).



    , SP-GiST , , , . ( B- GiST).

    . , , , . , ( B-, ) . , , - . , - .

    , GiST, ( , , ) , .

    SP-GiST ; . , , . ; , .

    SP-GiST (TID). ( ), : .

    , . , , .

    , , .

    GiST, , , . , ( , - ). , .

    . , ; . .

    , -. , , .

    :


    (quadtree) . () . .

    -, openflights.org. , , , , point.


    ...


    ...


    , .

    , GiST. :



    , ; . . . , . ; , .



    , , .

    postgres=# create table points(p point);
    CREATE TABLE

    postgres=# insert into points(p) values
    (point '(1,1)'), (point '(3,2)'), (point '(6,3)'),
    (point '(5,5)'), (point '(7,8)'), (point '(8,6)');
    INSERT 0 6

    postgres=# create index points_quad_idx on points using spgist(p);
    CREATE INDEX

    quad_point_ops, :

    postgres=# select amop.amopopr::regoperator, amop.amopstrategy
    from pg_opclass opc, pg_opfamily opf, pg_am am, pg_amop amop
    where opc.opcname = 'quad_point_ops'
    and opf.oid = opc.opcfamily
    and am.oid = opf.opfmethod
    and amop.amopfamily = opc.opcfamily
    and am.amname = 'spgist'
    and amop.amoplefttype = opc.opcintype;
    amopopr | amopstrategy
    -----------------+--------------
    <<(point,point) |1
    >>(point,point) |5
    ~=(point,point) |6
    <^(point,point) | 10
    >^(point,point) | 11
    <@(point,box) |8
    (6 rows)

    , , select * from points where p >^ point '(2,7)' ( , ).



    , . >^ (2,7) (4,4) , .

    , , . (6,6), .



    (8,6) (7,8), (7,8). .

    (4,4) , .

    postgres=# set enable_seqscan = off;
    SET
    postgres=# explain (costs off) select * from points where p >^ point '(2,7)';
    QUERY PLAN
    ------------------------------------------------
    Index Only Scan using points_quad_idx on points
    Index Cond: (p >^ '(2,7)'::point)
    (2 rows)


    SP-GiST gevel, . : - PostgreSQL. : gevel pageinspect (). .

    -, .

    demo=# create index airports_coordinates_quad_idx on airports_ml using spgist(coordinates);
    CREATE INDEX

    , -, :

    demo=# select * from spgist_stats('airports_coordinates_quad_idx');
    spgist_stats
    ----------------------------------
    totalPages:33 +
    deletedPages:0+
    innerPages:3+
    leafPages: 30 +
    emptyPages:2+
    usedSpace: 201.53 kbytes+
    usedInnerSpace:2.17 kbytes+
    usedLeafSpace: 199.36 kbytes+
    freeSpace: 61.44 kbytes +
    fillRatio: 76.64% +
    leafTuples:5993 +
    innerTuples: 37 +
    innerAllTheSame: 0+
    leafPlaceholders:725+
    innerPlaceholders: 0+
    leafRedirects: 0+
    innerRedirects:0
    (1 row)

    -, :

    demo=# select tid, n, level, tid_ptr, prefix, leaf_value
    from spgist_print('airports_coordinates_quad_idx') as t(
    tid tid,
    allthesame bool,
    n int,
    level int,
    tid_ptr tid,
    prefix point,--
    node_label int,-- ( )
    leaf_value point --
    )
    order by tid, n;
    tid | n | level | tid_ptr |prefix|leaf_value
    ---------+---+-------+---------+------------------+------------------
    (1,1) | 0 | 1 | (5,3) | (-10.220,53.588) |
    (1,1) | 1 | 1 | (5,2) | (-10.220,53.588) |
    (1,1) | 2 | 1 | (5,1) | (-10.220,53.588) |
    (1,1) | 3 | 1 | (5,14)| (-10.220,53.588) |
    (3,68)| | 3 | || (86.107,55.270)
    (3,70)| | 3 | || (129.771,62.093)
    (3,85)| | 4 | || (57.684,-20.430)
    (3,122) | | 4 | || (107.438,51.808)
    (3,154) | | 3 | || (-51.678,64.191)
    (5,1) | 0 | 2 | (24,27) | (-88.680,48.638) |
    (5,1) | 1 | 2 | (5,7) | (-88.680,48.638) |
    ...

    . spgist_print , , , .

    : k-


    .

    . : . . , : . , .

    , , . , .

    k- , k- (k-D tree).

    :


    ...


    ...


    , .

    , kd_point_ops:

    postgres=# create index points_kd_idx on points using spgist(p kd_point_ops);
    CREATE INDEX


    , quad_point_ops.


    , , :

    demo=# select tid, n, level, tid_ptr, prefix, leaf_value
    from spgist_print('airports_coordinates_kd_idx') as t(
    tid tid,
    allthesame bool,
    n int,
    level int,
    tid_ptr tid,
    prefix float,--
    node_label int,-- ( )
    leaf_value point --
    )
    order by tid, n;
    tid | n | level | tid_ptr | prefix |leaf_value
    ---------+---+-------+---------+------------+------------------
    (1,1) | 0 | 1 | (5,1) | 53.740 |
    (1,1) | 1 | 1 | (5,4) | 53.740 |
    (3,113) | | 6 | || (-7.277,62.064)
    (3,114) | | 6 | || (-85.033,73.006)
    (5,1) | 0 | 2 | (5,12)|-65.449 |
    (5,1) | 1 | 2 | (5,2) |-65.449 |
    (5,2) | 0 | 3 | (5,6) | 35.624 |
    (5,2) | 1 | 3 | (5,3) | 35.624 |
    ...


    :


    SP-GiST (radix tree) . , , , .

    , : postgrespro.ru, postgrespro.com, postgresql.org planet.postgresql.org.

    postgres=# create table sites(url text);
    CREATE TABLE

    postgres=# insert into sites values ('postgrespro.ru'),('postgrespro.com'),('postgresql.org'),('planet.postgresql.org');
    INSERT 0 4

    postgres=# create index on sites using spgist(url);
    CREATE INDEX

    :



    , . , stgres p + o + stgres.

    , , ( , ).

    text_ops , b-tree: , , :

    postgres=# select amop.amopopr::regoperator, amop.amopstrategy
    from pg_opclass opc, pg_opfamily opf, pg_am am, pg_amop amop
    where opc.opcname = 'text_ops'
    and opf.oid = opc.opcfamily
    and am.oid = opf.opfmethod
    and amop.amopfamily = opc.opcfamily
    and am.amname = 'spgist'
    and amop.amoplefttype = opc.opcintype;
    amopopr | amopstrategy
    -----------------+--------------
    ~<~(text,text)|1
    ~<=~(text,text) |2
    =(text,text)|3
    ~>=~(text,text) |4
    ~>~(text,text)|5
    <(text,text)| 11
    <=(text,text) | 12
    >=(text,text) | 14
    >(text,text)| 15
    (9 rows)

    , , .

    B- , , .

    : select * from sites where url like 'postgresp%ru'. :

    postgres=# explain (costs off) select * from sites where url like 'postgresp%ru';
    QUERY PLAN
    ------------------------------------------------------------------------------
    Index Only Scan using sites_url_idx on sites
    Index Cond: ((url ~>=~ 'postgresp'::text) AND (url ~<~ 'postgresq'::text))
    Filter: (url ~~ 'postgresp%ru'::text)
    (3 rows)

    , postgresp postgresq (Index Cond), (Filter).

    , p . : p + l ( , ) p + o + stgres ().

    stgres , postgres + p + ro. () postgres + q ( ).

    ro. , : postgrespro.com postgrespro.ru. .




    :

    postgres=# select * from spgist_print('sites_url_idx') as t(
    tid tid,
    allthesame bool,
    n int,
    level int,
    tid_ptr tid,
    prefix text, --
    node_label smallint, --
    leaf_value text--
    )
    order by tid, n;


    spgist ( ):

    amname | name| pg_indexam_has_property
    --------+---------------+-------------------------
    spgist | can_order | f
    spgist | can_unique| f
    spgist | can_multi_col | f
    spgist | can_exclude | t


    SP-GiST . , ( GiST). .

    :

    name| pg_index_has_property
    ---------------+-----------------------
    clusterable | f
    index_scan| t
    bitmap_scan | t
    backward_scan | f

    GiST .

    , , :

    name| pg_index_column_has_property
    --------------------+------------------------------
    asc| f
    desc | f
    nulls_first| f
    nulls_last | f
    orderable| f
    distance_orderable | f
    returnable | t
    search_array | f
    search_nulls | t

    , . SP-GiST ; .

    SP-GiST , . , - , - .


    , . , NULL . :

    postgres=# explain (costs off)
    select * from sites where url is null;
    QUERY PLAN
    ----------------------------------------------
    Index Only Scan using sites_url_idx on sites
    Index Cond: (url IS NULL)
    (2 rows)

    SP-GiST . , spgist, : . ; .

    , , - . , .


    , PostgreSQL SP-GiST:

    • box_ops.
      , 16. GiST , : GiST , , ( ) .
    • range_ops.
      : , .


    .
    Original source: habrahabr.ru (comments, light).

    https://habrahabr.ru/post/337502/

    :  

    : [1] []
     

    :
    : 

    : ( )

    :

      URL