lwpro2 posted: " i was using PreparedStatement to load some data out of sql database, which has been working fine for quite some time. Till recently, for one day's data, the query is not able to finish (hang on reading the response from database) "stackTrace"
i was using PreparedStatement to load some data out of sql database, which has been working fine for quite some time. Till recently, for one day's data, the query is not able to finish (hang on reading the response from database)
the same query however works well with plain run in the sql tool, or if run using plain java statement.
and until I have added a dummy where clause ``... and 1=1``, then suddenly the preparedstatement is able to return the result timely again.
in the beginning, i thought it was the dummy clause which made a difference strangely. turns out, this was a problem with sql server parameter sniffing.
the dummy where clause worked only because sql server now see it as a different query, hence not using the previous cached execution plan.
this can be reproduced by adding `
option (recompile)
to the query. this will trigger sql server to drop the previous cached execution plan, as such, even the original query, without the dummy where clause is now back to performing again.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.