Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Comments #3643

Closed
davewasmer opened this issue Feb 14, 2019 · 10 comments · Fixed by #6892
Closed

Query Comments #3643

davewasmer opened this issue Feb 14, 2019 · 10 comments · Fixed by #6892

Comments

@davewasmer
Copy link
Contributor

davewasmer commented Feb 14, 2019

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

Would you be open to a PR that added support for "naming" queries? I.e. something like:

Post.createQueryBuilder()
  .comment(`Lookup all the posts in ${ category } category`)
  .where({ category })
  .getSql()

/*
-- Lookup all the posts in foo category
SELECT ...

It would only be stored / used in TypeORM, obviously not submitted to the driver layer.

I've found a few times where I'm trying to diagnose a API call that makes multiple complex queries and is failing due to an SQL error. I end up trying to read the generated SQL and trying to infer where in my code that query came from. It would be nice to be able to label the queries such that I could easily trace them back when the error causes TypeORM to dump getSql() output.

@Diluka
Copy link
Contributor

Diluka commented Feb 15, 2019

Since TypeORM is highly influenced by Hibernate. The Named Query is already defined by JPA and been implemented in Hibernate. I suggest change the name of this feature into query comment.

Post.createQueryBuilder()
  .comment(`Lookup all the posts in ${ category } category`)
  .where({ category })
  .getSql()

@Kononnable
Copy link
Contributor

It would only be stored / used in TypeORM, obviously not submitted to the driver layer.

You propose to log this comment only in some TypeOrm functions or add it as comment in sql query(so it will be visible in profilers and similar tools)?

@davewasmer
Copy link
Contributor Author

I suggest change the name of this feature into query comment.

Sure, happy to go with a different name for the feature. "query comment" or "named query" are both fine by me.

You propose to log this comment only in some TypeOrm functions or add it as comment in sql query(so it will be visible in profilers and similar tools)?

You make a good point - if it's possible to add it as a comment to the sql query directly that would be nice for the reasons you mentioned.

However, I'm not familiar with the driver layer at all, so I'm not sure if that concept makes sense. Do the driver's typically submit a string of SQL over the database connection, such that we can just add a standard SQL comment? Or do they parse the query into some kind of over-the-wire format that ends up stripping out extraneous info (i.e. stripping out comments)?

@davewasmer
Copy link
Contributor Author

@Kononnable would you be up for a looking a PR for this? Happy to take a stab at it, but don't want to waste effort if the idea will be turned down regardless.

@Kononnable
Copy link
Contributor

Didn't tested it but I think we should be able to add comments to generated queries.
However if your query is failing wouldn't it be easier to just log a callstack? With callstack it should be pretty easy to find the right place. I think using a logger would be the easiest solution for your problem.

/**
* Logs query that is failed.
*/
logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) {

@davewasmer
Copy link
Contributor Author

@Kononnable unfortunately, logging from there isn’t helpful. We only know the query failed when the DB returns an error, meaning it is necessarily async. The call stack from the logging method only traces back to the driver, and no farther.

I suppose once async stack traces land in node v12 and TypeORM starts shipping native async/await in its compiled output, then yes, logging a stacktrace would help.

But even then, I could imagine query comments being useful in certain production environments as well, or when trying to diagnose slow queries even.

Sounds like you’re open to a PR?

@Kononnable
Copy link
Contributor

Oh, I didn't think about async or event-driven scenarios. Stack trace might not be so useful there.
I think possibility of adding comments will be a good idea. They make almost no overhead and it allow us to use all of DBA tools to diagnose performance problems(without comments we still can use such tools, but it would take more time).

@sgronblo

This comment has been minimized.

@imnotjames imnotjames self-assigned this Oct 11, 2020
@imnotjames imnotjames changed the title Feature request: named queries Query Comments Oct 11, 2020
@imnotjames
Copy link
Contributor

imnotjames commented Oct 12, 2020

I'm going to implement this. I've seen this used MANY times not just for general debugging, but also for distributed tracing, and for experimenting with query plans using features like Query plan stability or whatever the SQL Server studio provides - don't remember the name of it. Another big use case is for explicit routing when you're using ProxySQL - by using the regex rules you can route to a specific set of nodes based on the content of a comment.

This is separate from optimizer hints included in the queries, which this won't really support (most of those are comments in the format of /*+ foo */ but interspersed in certain areas of the query)

Work in progress branch can be found here - it's following the .comment("hello world") notation in the query builder.

imnotjames added a commit to imnotjames/typeorm that referenced this issue Oct 12, 2020
add a `comment` method to the QueryBuilder so we can include an
arbitrary comment in our queries for a variety of purposes -  from
query plan stability to SQL reverse proxy routing, to debugging.
the comment builder is supported in selects, inserts, deletes, and
updates

this is directly inspired by the functionality supported by hibernate
to handle SQL Query comments

it uses C-style queries which are ANSI SQL 2003 & supported in all
of the dialects of SQL that we support as drivers

fixes typeorm#3643
imnotjames added a commit to imnotjames/typeorm that referenced this issue Oct 12, 2020
add a `comment` method to the QueryBuilder so we can include an
arbitrary comment in our queries for a variety of purposes -  from
query plan stability to SQL reverse proxy routing, to debugging.
the comment builder is supported in selects, inserts, deletes, and
updates

this is directly inspired by the functionality supported by hibernate
to handle SQL Query comments

it uses C-style queries which are ANSI SQL 2003 & supported in all
of the dialects of SQL that we support as drivers

fixes typeorm#3643
pleerock pushed a commit that referenced this issue Oct 15, 2020
add a `comment` method to the QueryBuilder so we can include an
arbitrary comment in our queries for a variety of purposes -  from
query plan stability to SQL reverse proxy routing, to debugging.
the comment builder is supported in selects, inserts, deletes, and
updates

this is directly inspired by the functionality supported by hibernate
to handle SQL Query comments

it uses C-style queries which are ANSI SQL 2003 & supported in all
of the dialects of SQL that we support as drivers

fixes #3643
zaro pushed a commit to zaro/typeorm that referenced this issue Jan 12, 2021
add a `comment` method to the QueryBuilder so we can include an
arbitrary comment in our queries for a variety of purposes -  from
query plan stability to SQL reverse proxy routing, to debugging.
the comment builder is supported in selects, inserts, deletes, and
updates

this is directly inspired by the functionality supported by hibernate
to handle SQL Query comments

it uses C-style queries which are ANSI SQL 2003 & supported in all
of the dialects of SQL that we support as drivers

fixes typeorm#3643
@JinyanLai
Copy link

is it possible to add comment to insert, save, update
thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants