snowflakedb / snowflake-jdbc

Snowflake JDBC Driver
Apache License 2.0
178 stars 170 forks source link

SNOW-1003959: Slow query in spring boot not showing the SQL executed. #1601

Closed shawnlu-ikigai closed 10 months ago

shawnlu-ikigai commented 10 months ago

Please answer these questions before submitting your issue. In order to accurately debug the issue this information is required. Thanks!

  1. What version of JDBC driver are you using? 3.14.3

  2. What operating system and processor architecture are you using? Mac

  3. What version of Java are you using? java17

  4. What did you do? I am following this page: https://vladmihalcea.com/hibernate-slow-query-log/ I added following config for a spring boot application:

    logging.level.org.hibernate.SQL_SLOW.level=INFO
    spring.jpa.properties.hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS=500
  5. What did you expect to see?

    I am expecting to see the SQL execute:

    2024-01-10 10:21:52.239 INFO  82501 --- [   scheduling-1] org.hibernate.SQL_SLOW                   : SlowQuery: 1459 milliseconds. SQL: 'SELECT * FROM TABLE WHERE ...' []

    However, What I am seeing looks as following:

    2024-01-10 10:21:52.239 INFO  82501 --- [   scheduling-1] org.hibernate.SQL_SLOW                   : SlowQuery: 1459 milliseconds. SQL: '**HikariProxyPreparedStatement@1803888113 wrapping net.snowflake.client.jdbc.SnowflakePreparedStatementV1@644732**' []
  6. Can you set logging to DEBUG and collect the logs?

    https://community.snowflake.com/s/article/How-to-generate-log-file-on-Snowflake-connectors

  7. What is your Snowflake account identifier, if any? (Optional)

shawnlu-ikigai commented 10 months ago

I checked PgPreparedStatement.java for postgres and found there is a toString method:

  public String toString() {
    if (preparedQuery == null) {
      return super.toString();
    }

    return preparedQuery.query.toString(preparedParameters);
  }

There is no toString method in SnowflakePreparedStatementV1.java. adding that method might fix the issue already. That's my assumption. Hoping it will helps.

sfc-gh-wfateem commented 10 months ago

@shawnlu-ikigai thanks for reporting this. Would you agree that this is more of an enhancement request rather than a bug? I think that output you're seeing is exactly because as you pointed out, we didn't override the toString() method, so it should theoretically be a straightforward thing to implement to address your problem. I'll take a look.

shawnlu-ikigai commented 10 months ago

Hi @sfc-gh-wfateem Thank you for timely response. Yes, enhancement makes more sense.

sfc-gh-wfateem commented 10 months ago

@shawnlu-ikigai I made the change and test it locally so PR #1604 will address your problem. However, I just had an after thought and I think I can improve on this by adding a query ID in addition to the SQL text.

Do you think that will be helpful as well?

shawnlu-ikigai commented 10 months ago

@sfc-gh-wfateem that's really quick man. Yeah query ID for sure will help. When do you think this will be merged and pushed to mvnrepository?

sfc-gh-wfateem commented 10 months ago

@shawnlu-ikigai the change was just merged but it's past the code freeze for January's release, so this will make its way in the JDBC driver's February release.