Performance gets affected when using 'limit' as it applies limit after the table join
See original GitHub issueSummary
Since ‘limit’ is getting applied after the table joins, the performance is greatly affected. If the limit for the first table is applied and then the table join happens, it would greatly improve the performance.
Steps to reproduce
Sample database schema
Click to expand!
GraphQL
Click to expand!
query MyQuery {
users(limit: 10, offset: 0) {
works {
id
name
updated_at
created_at
}
name
id
is_active
updated_at
username
created_at
}
}
Generated SQL
Click to expand!
SELECT
coalesce(json_agg("root"), '[]') AS "root"
FROM
(
SELECT
row_to_json(
(
SELECT
"_5_e"
FROM
(
SELECT
"_4_root.ar.root.works"."works" AS "works",
"_0_root.base"."name" AS "name",
"_0_root.base"."id" AS "id",
"_0_root.base"."is_active" AS "is_active",
"_0_root.base"."updated_at" AS "updated_at",
"_0_root.base"."username" AS "username",
"_0_root.base"."created_at" AS "created_at"
) AS "_5_e"
)
) AS "root"
FROM
(
SELECT
*
FROM
"public"."users"
WHERE
('true')
) AS "_0_root.base"
LEFT OUTER JOIN LATERAL (
SELECT
coalesce(json_agg("works"), '[]') AS "works"
FROM
(
SELECT
row_to_json(
(
SELECT
"_2_e"
FROM
(
SELECT
"_1_root.ar.root.works.base"."id" AS "id",
"_1_root.ar.root.works.base"."name" AS "name",
"_1_root.ar.root.works.base"."updated_at" AS "updated_at",
"_1_root.ar.root.works.base"."created_at" AS "created_at"
) AS "_2_e"
)
) AS "works"
FROM
(
SELECT
*
FROM
"public"."works"
WHERE
(("_0_root.base"."id") = ("user_id"))
) AS "_1_root.ar.root.works.base"
) AS "_3_root.ar.root.works"
) AS "_4_root.ar.root.works" ON ('true')
LIMIT
10 OFFSET ('0') :: integer
) AS "_6_root"
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:12 (4 by maintainers)
Top Results From Across the Web
mysql - does LIMIT have effect before or after JOIN?
The LIMIT will happen at the same time with the join : MySQL will not read through the entire logins table, but fetch...
Read more >mysql - Does using LIMIT improve the performance and is it ...
Doing LIMIT inside subqueries may not always be the answer because of the cardinality of indexes, the data content, and the result set...
Read more >Performance Tuning SQL Queries | Advanced SQL
Learn how to conduct SQL performance tuning by reducing table size, simplifying joins, & the EXPLAIN command in this advanced SQL tutorial.
Read more >Chapter 4. Query Performance Optimization
The best solution is to add a LIMIT clause to the query. ... You can decompose a join by running multiple single-table queries...
Read more >Performance Considerations for Join Queries
Queries involving join operations often require more tuning than queries that refer to only one table. The maximum size of the result set...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This issue was tagged as a priority high but still has not been fixed. I wonder everyone forgot it.
I’m also experiencing this problem. I copied the query from the “analyze” page and moved limit to the “main” select statement, and the query time went from about ~20 seconds to less than a second.