Skip to content

Index usage for tag queries without values #30

@pnorman

Description

@pnorman

https://github.com/iandees/xapi-servlet/blob/master/src/main/java/com/yellowbkpk/geo/xapi/db/Selector.java#L37 builds a query string using exist(tags, key) but this does not use the GIN index set up in the documentation so ends up doing a sequential scan. A query with tags ? key will use the index.

In this example the cost is 21 times greater for the exist() query.

xapi=# EXPLAIN (FORMAT YAML) SELECT * FROM ways WHERE tags ? 'source_1';
                   QUERY PLAN
-------------------------------------------------
 - Plan:                                        +
     Node Type: "Bitmap Heap Scan"              +
     Relation Name: "ways"                      +
     Alias: "ways"                              +
     Startup Cost: 4541.63                      +
     Total Cost: 551334.20                      +
     Plan Rows: 151436                          +
     Plan Width: 1167                           +
     Recheck Cond: "(tags ? 'source_1'::text)"  +
     Plans:                                     +
       - Node Type: "Bitmap Index Scan"         +
         Parent Relationship: "Outer"           +
         Index Name: "idx_ways_tags"            +
         Startup Cost: 0.00                     +
         Total Cost: 4503.77                    +
         Plan Rows: 151436                      +
         Plan Width: 0                          +
         Index Cond: "(tags ? 'source_1'::text)"
(1 row)
xapi=# EXPLAIN (FORMAT YAML) SELECT * FROM ways WHERE exist(tags,'source_1');
                 QUERY PLAN
---------------------------------------------
 - Plan:                                    +
     Node Type: "Seq Scan"                  +
     Relation Name: "ways"                  +
     Alias: "ways"                          +
     Startup Cost: 0.00                     +
     Total Cost: 11578309.22                +
     Plan Rows: 50478774                    +
     Plan Width: 1167                       +
     Filter: "exist(tags, 'source_1'::text)"
(1 row)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions