Skip to content

Feature: Flag to turn off subquery #1756

Closed

Description

mickhansen changed title to reflect the feature we decided on would solve this

        db.table.find({
            where: {
                id: id
            },
            limit: 50,
            include: [{
                model: db.table1,
                as: 'table1'
            }, {
                model: db.table2,
                as: 'table2'
            }],
            order: [
                [{
                    model: db.table1,
                    as: 'table1'
                }, 'date', 'desc']
            ]
        }).success(function(data) {

Gives me a query like:

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?

Activity

  1. mickhansen commented on May 12, 2014

    @mickhansen
    Contributor

    @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.

  2. mickhansen commented on May 12, 2014

    @mickhansen
    Contributor

    @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.

  3. seth-admittedly commented on Jun 26, 2014

    @seth-admittedly
    Contributor

    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.

  4. mickhansen commented on Jun 26, 2014

    @mickhansen
    Contributor

    @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.

  5. seth-admittedly commented on Jun 26, 2014

    @seth-admittedly
    Contributor

    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.

  6. seth-admittedly commented on Jun 26, 2014

    @seth-admittedly
    Contributor

    Or our current hack, which is to return all the records then do the limit/offset in code, which is obviously less than ideal.

  7. mickhansen commented on Jun 26, 2014

    @mickhansen
    Contributor

    Yeah, ideally we do want everything to be flaggable.

  8. changed the title [-]LIMIT in wrong place when using eager loading[/-] [+]Feature: Flag to turn off subquery[/+] on Jul 7, 2014
  9. seth-admittedly commented on Jul 7, 2014

    @seth-admittedly
    Contributor

    Could this be fixed by just adding a new option "limitTotal", which would apply a limit to the outermost select?

  10. mickhansen commented on Jul 7, 2014

    @mickhansen
    Contributor

    @seth-admittedly that could work - although i'm more inclined to a more generic subquery: false flag or similar.

  11. mickhansen commented on Sep 7, 2014

    @mickhansen
    Contributor

    No unit testing and only a cursory test but it should now be possible to do:

    Model.findAll({
      subQuery: false,
      // ... include ... etc
    });
    
  12. cmorell commented on Dec 17, 2014

    @cmorell

    Isn't the best way, but if you write Limit X after an order by clause, query works correctly

    findAll({ where:where_criteria, include: array_db_to_be_included, order: 'createdAt DESC limit '+constants.max_request_query})
    
  13. mickhansen commented on Dec 17, 2014

    @mickhansen
    Contributor

    @cmorell not if you have :M includes which result in row duplication.

  14. sebastienbarre commented on Dec 29, 2016

    @sebastienbarre

    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).

  15. ghost added a commit that references this issue on Jun 2, 2017
  16. notflip commented on Dec 1, 2018

    @notflip

    Can anyone explain what subQuery: false does?

  17. ralphpig commented on Dec 10, 2018

    @ralphpig

    @notflip

    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.

  18. notflip commented on Dec 11, 2018

    @notflip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: feature

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions