[Solved] Improve underestimated CTE estimates or replace CTE scan in Postgres 10.1

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Calling Functions With Indexed Columns (query line: 7): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `edge` is indexed, the index won’t be used as it’s wrapped with the function `floatrange`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 8): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `edge` is indexed, the index won’t be used as it’s wrapped with the function `ST_3DDWithin`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  4. Use UNION ALL instead of UNION (query line: 38): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
CREATE INDEX treenode_idx_id ON "treenode" ("id");
CREATE INDEX treenode_edge_idx_project_id ON "treenode_edge" ("project_id");
The optimized query:
WITH bb_edge AS (SELECT
        te.id 
    FROM
        treenode_edge te 
    WHERE
        te.edge && ST_MakeEnvelope(-133095.0, -55879.0, 239973.8, 176863.4) 
        AND floatrange(ST_ZMin(te.edge), ST_ZMax(te.edge), '[]') && floatrange(82550.0, 82600.0, '[)') 
        AND ST_3DDWithin(te.edge, ST_MakePolygon(ST_MakeLine(ARRAY[ST_MakePoint(-133095.0, -55879.0, 82575.0), ST_MakePoint(239973.8, -55879.0, 82575.0), ST_MakePoint(239973.8, 176863.4, 82575.0), ST_MakePoint(-133095.0, 176863.4, 82575.0), ST_MakePoint(-133095.0, -55879.0, 82575.0)]::geometry[])), 25.0) 
        AND te.project_id = 1) SELECT
        t1.id,
        t1.parent_id,
        t1.location_x,
        t1.location_y,
        t1.location_z,
        t1.confidence,
        t1.radius,
        t1.skeleton_id,
        EXTRACT(EPOCH 
    FROM
        t1.edition_time),
        t1.user_id 
    FROM
        (SELECT
            bb_edge.id 
        FROM
            bb_edge 
        UNION
        (
            SELECT
                t.parent_id 
            FROM
                bb_edge e 
            JOIN
                treenode t 
                    ON t.id = e.id
            ) 
    UNION
    SELECT
        UNNEST(ARRAY[6429696,
        1030658,
        5140907,
        6817796]::bigint[])
) all_nodes(id) 
JOIN
treenode t1 
    ON all_nodes.id = t1.id

Related Articles



* original question posted on StackOverflow here.