Repository navigation
Description
Issue Creation Checklist
[x] I have read the contribution guidelines
Feature Description
When using postgres, allow for a JSONB query to executed using the ->> operator, rather than #>> when there is a single path.
Is your feature request related to a problem? Please describe.
In our use case, we are typically querying by a single path in a JSONB field. For instance:
SELECT foo FROM bar WHERE value->>'field' = 'baz'
where sequelize would write the query as:
SELECT foo FROM bar WHERE value#>>'{field}' = 'baz'
which is a single path entry, but ends up being 8% slower on our production and test databases when using #>> versus ->>.
In addition, it means that we must keep copies of indexes that for each type of query, #>> and ->>. This means that there is about 100GB of duplicated index data. Those indexes exist only for sequelize, and about 6x the number of executed queries are not using sequelize, and thus using the ->> specific indexes.
Given the 8% performance drop (with our database and data) when using #>>'{field}', it does not make sense for us to move those non-sequelize queries to use #>>'{field}'.
In addition, we use multiple languages, and would like to have a single solution across all languages when using a single path.
Describe the solution you'd like
My preferred solution would to have the ability to drop to using ->>'field' when there is a single path. I understand that this change might disrupt others who are only using sequelize and have built indexes using #>>'{field}' for those use cases, so it may make sense to add a feature flag or configuration and not make the change be the default for all users.
A simple case would be to make a change on query-generator.js:
case 'postgres':
if (paths.length > 1) {
pathStr = this.escape(`{${paths.join(',')}}`);
return `(${quotedColumn}#>>${pathStr})`;
} else {
pathStr = this.escape(`${paths[0]}`);
return `(${quotedColumn}->>${pathStr})`;
}instead of:
case 'postgres':
pathStr = this.escape(`{${paths.join(',')}}`);
return `(${quotedColumn}#>>${pathStr})`;Why should this be in Sequelize
For environments where multiple languages are used, or a combination of sequelize and non-sequelize, it would be very helpful to be able to use only a single set of indexes in this case, as well as use the optimized query path. In our case, indexes are 9GB-14GB each, and when duplicated use a lot of additional space, memory, and cpu time across all fields that are indexed. When under load, this becomes a much bigger issue, especially with the 8% penalty.
We would like to optimize for the fastest query, ->>'field', and having this option available in sequelize would help significantly.
Describe alternatives/workarounds you've considered
- switch all queries and other ORMs to use
#>>'{field}', but this gives us a performance penalty of 8% (on our database/data), and requires us to maintain forks of other ORMs for other languages that would implement->>'field' - maintain our own fork sequelize for this single feature
- keep the status quo, which gives us a performance penalty when not under load, and a much larger performance penalty due to the size of the indexes when under load
Feature Request Checklist
Is this feature dialect-specific?
- No. This feature is relevant to Sequelize as a whole.
- Yes. This feature only applies to the following dialect(s): Postgres
Would you be willing to implement this feature by submitting a Pull Request?
- Yes, I have the time and I know how to start. (but if done as a configuration or enabling a feature might cause questions to be asked while creating the PR)
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Activity
WikiRik commentedon Feb 8, 2022
For this relatively easy performance gain, I would say that it's better to have this by default. Since it might break some people's indexes it would be good to consider it a breaking change and add it to the v7 alpha.
Looking forward to your PR!