What does 'separate' in sequelize mean?
Additional to @robertklep response:
As you know now it separates an otherwise joined query.
This means that it would be more performant in some situations where you have many joins and nested joins (can have a huge impact in some). Nested joins make Sequelize take more time in a single big query than running multiple small ones. The problem is pointed out as a deduplication operation:
See here: Slow associations in SequelizeJS
I found this in the current code:
If true, runs a separate query to fetch the associated instances, only supported for hasMany associations
To elaborate: by default, to retrieve the related model instance, Sequelize will use a SQL JOIN. By enabling separate
, Sequelize will perform a separate query for each of the associated models, and join the resulting documents in code (instead of letting the database perform the join).
Assume I have Product
model with a hasMany
association to the Tag
model ("a product can have many tags associated with it").
Here's the "regular" query:
SELECT
`product`.`id`,
`product`.`title`,
`tags`.`id` AS `tags.id`,
`tags`.`name` AS `tags.name`,
`tags`.`productId` AS `tags.productId`
FROM `products` AS `product`
LEFT OUTER JOIN `tags` AS `tags`
ON
`product`.`id` = `tags`.`productId`;
Here are the separate : true
queries:
SELECT
`product`.`id`,
`product`.`title`
FROM `products` AS `product`;
SELECT
`id`,
`name`,
`productId`
FROM `tags` AS `tag`
WHERE
`tag`.`productId` IN (1);