Skip to content

Prepared statements #3495

Open

Description

@mickhansen
  • Review driver support
  • Migrate to node-mysql2
  • Estimate refactor time

Activity

  1. added
    type: feature
    status: in discussion
    P1: important
    on Apr 9, 2015
  2. self-assigned this
    on Apr 9, 2015
  3. mbroadst commented on Apr 9, 2015

    @mbroadst
    Contributor

    my 2cents is you should add "estimate migration to node-mysql2" to that list hombre

  4. sushantdhiman commented on Jun 24, 2016

    @sushantdhiman
    Contributor

    Driver support review report

    Driver Prepared Statements Related Docs
    node-mysql2 ✅ Link 1
    node-postgres ✅ Link 1
    node-sqlite3 ✅ Link 1
    tedious ✅ Link 1
  5. mickhansen commented on Jul 20, 2016

    @mickhansen
    ContributorAuthor

    The biggest issue is where statements, they can be deeply nested and we'd need some way to pass around the query aswell as the values.

    Generally we should probably figure out some syntax for passing around. {query: '', replacements: []} is used in https://github.com/sequelize/sequelize/blob/master/lib/sequelize.js#L546 and could be mergable.

  6. sushantdhiman commented on Jul 20, 2016

    @sushantdhiman
    Contributor

    Yes, we need some data store which will keep the values while replacing the ? in query. Its order will matter too.

    We can implement it but main issue I think is performance. How does pooling plays with prepared statements. AFAIK prepared statement will be reused only on same connection. But on other hand currently database is preparing plan for each query everytime. With prepared statements there will be some chances that query might be reused.

  7. sushantdhiman commented on Jul 20, 2016

    @sushantdhiman
    Contributor

    I am starting to migrate sequelize to node-mysql2 as our first step.

    Once its done we can think about an API to keep all the values for prepared statements. Or is it possible to pick these parameters out of raw statement before executing ?

  8. mickhansen commented on Jul 20, 2016

    @mickhansen
    ContributorAuthor

    @sushantdhiman Prepared statements might have a minor performance impact if your pools are not saturated, however if that's the case you probably don't care about the few ms difference.

    Picking them out of the raw statement would likely be too error prone and they're already escaped at that point.

  9. sushantdhiman commented on Jul 20, 2016

    @sushantdhiman
    Contributor

    @mickhansen node-mysql2 migration is done.

    Do you have any suggestion how to capture all the parameters passed to methods, should we use cls like system OR may be maintain a parameters field in options which will be passed down to query?

  10. felixfbecker commented on Jul 20, 2016

    @felixfbecker
    Contributor

    Preparing the statement of course costs a bit time but the following executions will be faster because the SQL does not need to be parsed anymore. So for a running Node app the overall performance will be faster after the first queries.

    I also want to point out here that in Postgres you actually need to name a prepared statement to get it reused each time, which is not the case in mysql. So we have to come up with a naming scheme for the queries in the postgres dialect that is unique for every query, but the same for different values. So if you pass different attributes it will be a different query because you can't use placeholders for column names, for a different where logic it will also be a different query, but for different where values it will be the same query.

    You can omit the name but then the SQL will be parsed each time and you get no performance benefit.

  11. sushantdhiman commented on Jul 20, 2016

    @sushantdhiman
    Contributor

    @felixfbecker node-mysql2 use a hashing technique to save the queries, I wan't aware about pg, May be we can use the any lru library for this.

    With prepared statement once the query changed , even if these are changes in attributes new query plan will be prepared. Thats something we can't control. But idea is that our most of queries always looks the same but have only different values (to bind) thus making them reusable

  12. felixfbecker commented on Jul 20, 2016

    @felixfbecker
    Contributor

    Well, for a first implementation in PG we could just use parameterized queries without PREPARE, which is more secure and has the same performance as a normal query. But we still need to firgure out how to pass values and query around. Then a better implementation is to use PREPARE. pg abstracts around this by simply providing a name option to the query method, and the the query will be prepared once and then reused.

    If we don't care about meaningful query names, we could just use a hashing function to hash the SQL string. It will be the same for the same query.

    The query generator should also be deterministic, e.g if you pass the same options it should result in the same query, even if you change the key order in the where clause it should result in the same WHERE condition. Object.keys() reports keys in the order they are defined, maybe some sorting needs to be done here.

  13. mickhansen commented on Jul 20, 2016

    @mickhansen
    ContributorAuthor

    Definitely no CLS, params and sql should be passed around explicitely, i'll likely make a stab at this soon.

    Let's not get too hung up on prepared statements atm and focus on getting the underlying stuff right so we can atleast rely on bind parameters rather than our own escaping.

  14. 26 remaining items

  15. TooBug commented on Jul 2, 2018

    @TooBug

    Any update on this?

    Seems .query() function has supported parameterized query, But the Model operations not.

  16. sushantdhiman commented on Jul 3, 2018

    @sushantdhiman
    Contributor

    With v5 all INSERT / UPDATE statements are using bind parameters. Hopefully SELECT will be converted to use bind internally as well

    https://github.com/sequelize/sequelize/blob/master/docs/upgrade-to-v5.md#others

  17. gabegorelick commented on Feb 6, 2019

    @gabegorelick
    Contributor

    Is there a way to disable usage of prepared statements? Their use is incompatible with connections poolers like pgbouncer (at least in transaction or statement mode).

    EDIT: this is not the case when using node-pg since Sequelize will use anonymous prepared statements that only live for the length of the transaction. These are perfectly fine to use with transaction pooling.

  18. AmbroiseVuaridel commented on Apr 4, 2019

    @AmbroiseVuaridel

    @sushantdhiman any update on the SELECT statements and prepared statements ?

  19. dustin-rcg commented on Apr 6, 2019

    @dustin-rcg

    Is anyone else bothered by the fact that using a single '$' as a bind parameter delimiter causes great potential for false positives? The implementation for mssql matches any pattern with a '$' followed by a regex word character as a bind parameter, even inside SQL string literals, and gives errors about missing bind parameters. It's also common to have '$' appear in mssql system column names (such as __$start_lsn used in change data capture) so using simply '$' as a delimiter was not a good decision IMO.

    Why wasn't a book-ended bind parameter delimiter that is much less likely to cause false positives chosen, such as @<param>? A good example of bind parameter syntax is pg-promise.

    Right now, I've wrapped my queries in a function that doubles all '$' when followed by a regex word character before passing to sequalize.query. This works for me, since I invented my own binding syntax @<param> which I subsequently replace with $param. The bind syntax should be improved before pushing it further into Sequelize.

  20. mschipperheyn commented on Jun 4, 2019

    @mschipperheyn

    Are prepared statements on the radar for where statements (not raw queries)? If so, when?

  21. mschipperheyn commented on Jun 5, 2019

    @mschipperheyn

    @sushantdhiman Thinking a little bit about prepared statements, I remember @mickhansen talk somewhere about the challenges of implementing this with include. Many people are moving to GraphQl and with GraphQL this is actually not / less of an issue since the recommended query retrieval strategy is not to use includes but instead use a dataloader.

    This means that if you guys would implement as a first step, Prepared Statements on the root queries only, keeping variable interpolation for includes, you would already cover 90% of the requirements of Graphql users. Probably non Graphql users as well bc include where statements are usually only a small part of database interaction. The include part could be a roadmap item.

  22. added and removed
    status: in discussion
    on Jul 26, 2019
  23. added theissue type on Mar 9, 2025
  24. removed
    type: feature
    on Mar 9, 2025
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

    P1: important

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions