[Solved] Why query in wrapped PLPGSQL function is much slower than itself?

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 Correlated Subqueries (query line: 14): A correlated subquery is a subquery that contains a reference (column: uid) to a table that also appears in the outer query. Usually correlated queries can be rewritten with a join clause, which is the best practice. The database optimizer handles joins much better than correlated subqueries. Therefore, rephrasing the query with a join will allow the optimizer to use the most efficient execution plan for the query.
  2. Avoid Subqueries (query line: 18): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Avoid Subqueries (query line: 29): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  4. 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.
Optimal indexes for this query:
CREATE INDEX aircraftdata_idx_action ON "public"."AircraftData" ("action");
CREATE INDEX aircraftdata_idx_uid ON "public"."AircraftData" ("uid");
The optimized query:
SELECT
        A.uid AS uid,
        A.max_time AS time,
        ST_AsText(ST_Collect(ARRAY(SELECT
            public."AircraftData"."position" 
        FROM
            public."AircraftData" 
        WHERE
            public."AircraftData"."uid" = A.uid 
            AND "time" BETWEEN 1617608651642 AND 1617657618453 
        ORDER BY
            "time" DESC LIMIT 254))) AS trajectory 
    FROM
        (SELECT
            M.uid AS uid,
            M.max_time AS max_time 
        FROM
            (SELECT
                public."AircraftData"."uid",
                MAX("time") AS max_time 
            FROM
                public."AircraftData" 
            WHERE
                "time" BETWEEN 1617608651642 AND 1617657618453 
            GROUP BY
                public."AircraftData"."uid") M 
        INNER JOIN
            (
                SELECT
                    public."AircraftData"."uid" 
                FROM
                    public."AircraftData" 
                WHERE
                    "time" > 1617657618453 
                GROUP BY
                    public."AircraftData"."uid"
            ) N 
                ON M.uid = N.uid
            ) A 
    INNER JOIN
        public."AircraftData" B 
            ON A.uid = B.uid 
            AND A.max_time = B.time 
            AND B.action != 2 
    ORDER BY
        "time" ASC

Related Articles



* original question posted on StackOverflow here.