简体   繁体   中英

Sequelize is grouping results when I filter the attributes

I have a table with several columns, and when I do a findAll using the attributes option (to filter out some of the columns) it seems like sequelize is grouping the results by the values of those columns.

On the log, I can see the SQL statement created, and it's not grouping by anything.

Any idea of what might be happening?

Edit

Adding findAll code.

 findAll: function (options, expanded, fields) {
    var deferred = Q.defer();
    var findAllOptions = {};

    if(fields != null) {
       findAllOptions.attributes = fields;
    }
    if(expanded != null) {
       findAllOptions.include = expanded;
    }
    if(options.offset != undefined) {
       findAllOptions.offset = options.offset;
    }
    if(options.limit != undefined) {
       findAllOptions.limit = options.limit;
    }

    if (this.Class.Model !== null) {
       this.Class.Model.findAll(findAllOptions).success(deferred.resolve).error(deferred.reject);
    } else {
       //--- error handling not important
    }

    return deferred.promise;
 }

I'm also using Q for promises, although I don't think that matters.

When you say "grouping", I'm assuming that you mean "ordering" (the difference being that grouping takes multiple rows and puts them together into 1 row where certain values are equal, and ordering by, takes all the rows with the same value and puts them in order, which winds up "grouping" rows with the same values in that column together)

When not using an "order by", postgresql will return the data in whatever order it finds most efficient. If your query has contains a predicate (part of a where statement) that references a column that's part of an index, it's likely that the the data will be returned in the same order of the index itself.

IE if I run the query:

select * from person

I may get the following results in the following order:

ID            FIRST            LAST
------------  ---------------- -----------------
1             John             Zane
2             Jack             Smith
3             Aaron            Smith
4             Darren           Thompson
5             Dick             Smith

They're likely to be returned in order because they are simply selected from the table in the quickest way that postgresql can find them, which might just happen to be the order in which they were inserted.

Now if I ran the query:

select * from person
where last='Smith'

Assuming that I have an index on person(last,first), it's highly likely that I get the following results, in order:

ID            FIRST            LAST
------------  ---------------- -----------------
3             Aaron            Smith
5             Dick             Smith
2             Jack             Smith

The reason is that the optimizer might use the index to retrieve the data, and the index has the rows listed in that order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM