You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT * FROM (SELECT * FROM "table" WHERE "table"."id"='1' LIMIT 50) AS "table" LEFT OUTER JOIN "table1" AS "table1" ON "table"."id" = "table1"."table_id" LEFT OUTER JOIN "table2" AS "table2" ON "table"."id" = "table2"."table_id" ORDER BY "table2"."date" desc;
Instead, I want the LIMIT 50 to be at the end of the entire query, not inside the table subselect. How do I get the LIMIT to be part of the entire query?
@blairvanderhoof that's not possible - why do you want to place the limit on the outer query? it's effectively useless for limiting the main table if you do that.
@blairvanderhoof Hm that's not quite possible, a limit works on the amount of rows, if you have two hasMany includes the rows will duplicate and the limit is effectively worthless. I think you might have to do a subquery where you fetch the ids you want for the include tables via a subquery.
The limit on the outer query still makes sense if you want to order on the joined table and know that there's only one dependent row for each row in the main table. Just got tripped up by this and not sure how to do an order based on a related table now.
@seth-admittedly subquery should only be triggered if there's a :M association - Is your association :M but you know there will only be one result? That's a hard assumption for Sequelize to make.
Yes, that's the case. I agree it would be difficult/impossible for Sequelize to know, but it would be a huge help have to the functionality be a flaggable.
No unit testing and only a cursory test but it should now be possible to do:
Model.findAll({
subQuery: false,
// ... include ... etc
});
Reacted by Aliaksei Litsvin, Igor Rybas, Miguel Stevens, Mike, NNSTH, Nicolas Gramlich, Ashkan Azarnik, Pedro Machado, Ken Lin, Terry Tu Nguyen and 9 more
Reacted by Antony Jones, Mike, Nicolas Gramlich and Terry Tu Nguyen
Reacted by Phạm Quang Minh, Freddy Sidauruk, Tomasz Racia, Aditya Kresna Permana, Rehan Sheikh, Antony Jones, Mike, Nicolas Gramlich, Terry Tu Nguyen, Asif Iqbal and 1 more
Woah there seems to be both a include.subQuery and a options.subQuery, both undocumented. options.subQuery set to false did the trick for me, or include.duplicating set to false (which I couldn't find in the doc either).
Reacted by Vadim Sorokin, Phillip Chan, Phạm Quang Minh, Mike Cranston, Viral Shah, Sophie LONGO, Jason Athanasoglou, Vladimir Petukhov, Ozan Müyesseroğlu, Dietrich G and 10 more
Reacted by Leonardo Freitas dos Santos and Felipe Lima
ghost added a commit that references this issue on Jun 2, 2017
It stops Sequelize from using subqueries on :M includes (my understanding at least)
Here is another issue describing it: #7778
For example. This
A.findAll({include: [{model: B},{model: C}]});
Will generate something like this
SELECT"A".*FROM
(
SELECT"A".<some_columns>
...
FROM"A"AS"A"
) AS"A"INNER JOIN B ...
INNER JOIN C ...
...
subQuery: false should force it to generate something like this
SELECT"A".<some_columns>
...
FROM"A"AS"A"INNER JOIN B ...
INNER JOIN C ...
...
Depending on your query, this can reduce cost quite a bit. I recently had a query generated by Sequelize that had an estimated low end cost of 200,000.
After removing the unnecessary subQuery, it reduce the query's low end cost to 700. Much better.
On Tue, 11 Dec 2018, 01:27 Michael Nelson ***@***.*** wrote:
@notflip <https://github.com/notflip>
It stops Sequelize from using subqueries on :M includes (my understanding
at least)
Here is another issue describing it: #7778
<#7778>
For example. This
A.findAll({
include: [{
model: B
}, {
model: C
}]
});
Will generate something like this
SELECT
"A".*FROM
(
SELECT
"A".<some_columns>
...
FROM
"A" AS "A"
) AS "A"INNER JOIN B ...INNER JOIN C ...
...
subQuery: false should force it to generate something like this
SELECT
"A".<some_columns>
...FROM "A" AS "A"INNER JOIN B ...INNER JOIN C ...
...
Depending on your query, this can reduce cost quite a bit. I recently had
a query generated by Sequelize that had an estimated low end cost of
200,000.
After removing the unnecessary subQuery, it reduce the query's low end
cost to 700. Much better.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1756 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AD9E0Celiiqp0HqODiGllAaGXQmiKfjlks5u3qeegaJpZM4B6JOG>
.
Activity
mickhansen commentedon May 12, 2014
@blairvanderhoof that's not possible - why do you want to place the limit on the outer query? it's effectively useless for limiting the main table if you do that.
mickhansen commentedon May 12, 2014
@blairvanderhoof Hm that's not quite possible, a limit works on the amount of rows, if you have two hasMany includes the rows will duplicate and the limit is effectively worthless. I think you might have to do a subquery where you fetch the ids you want for the include tables via a subquery.
seth-admittedly commentedon Jun 26, 2014
The limit on the outer query still makes sense if you want to order on the joined table and know that there's only one dependent row for each row in the main table. Just got tripped up by this and not sure how to do an order based on a related table now.
mickhansen commentedon Jun 26, 2014
@seth-admittedly subquery should only be triggered if there's a :M association - Is your association :M but you know there will only be one result? That's a hard assumption for Sequelize to make.
seth-admittedly commentedon Jun 26, 2014
Yes, that's the case. I agree it would be difficult/impossible for Sequelize to know, but it would be a huge help have to the functionality be a flaggable.
seth-admittedly commentedon Jun 26, 2014
Or our current hack, which is to return all the records then do the limit/offset in code, which is obviously less than ideal.
mickhansen commentedon Jun 26, 2014
Yeah, ideally we do want everything to be flaggable.
[-]LIMIT in wrong place when using eager loading[/-][+]Feature: Flag to turn off subquery[/+]seth-admittedly commentedon Jul 7, 2014
Could this be fixed by just adding a new option "limitTotal", which would apply a limit to the outermost select?
mickhansen commentedon Jul 7, 2014
@seth-admittedly that could work - although i'm more inclined to a more generic
subquery: falseflag or similar.mickhansen commentedon Sep 7, 2014
No unit testing and only a cursory test but it should now be possible to do:
cmorell commentedon Dec 17, 2014
Isn't the best way, but if you write Limit X after an order by clause, query works correctly
mickhansen commentedon Dec 17, 2014
@cmorell not if you have :M includes which result in row duplication.
sebastienbarre commentedon Dec 29, 2016
Woah there seems to be both a
include.subQueryand aoptions.subQuery, both undocumented.options.subQueryset to false did the trick for me, orinclude.duplicatingset to false (which I couldn't find in the doc either).Sequelize - FIX/SubQuery and Raw (#16657)
notflip commentedon Dec 1, 2018
Can anyone explain what subQuery: false does?
ralphpig commentedon Dec 10, 2018
@notflip
It stops Sequelize from using subqueries on :M includes (my understanding at least)
Here is another issue describing it: #7778
For example. This
Will generate something like this
subQuery: falseshould force it to generate something like thisDepending on your query, this can reduce cost quite a bit. I recently had a query generated by Sequelize that had an estimated low end cost of
200,000.After removing the unnecessary subQuery, it reduce the query's low end cost to
700. Much better.notflip commentedon Dec 11, 2018