5

PreparedStatement is a precompiled statement so it will increase jdbc performance at runtime but still confused about

  1. What is precompiled SQL Statement? What it does?
  2. Why precompiled statement is faster than usual SQL Statement?
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74

1 Answers1

13

The following steps are used by any database while running an SQL query:

  1. Convert given SQL query into DB format
  2. Check for syntax
  3. Check for semantics
  4. Prepare execution plan
  5. Set the run-time values into the query
  6. Run the query and fetch the output

In case of Statement interface all above 6 steps need to be followed for each query execution. However, in case of PreparedStatement interface steps 1 to 4 will be done only once and steps 5 & 6 will be repeated for each execution of the query. This makes the PreparedStatement faster.

Hope this answers your question...

Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
Pavan Kumar K
  • 1,360
  • 9
  • 11
  • But execution plan depends on parameter values (I checked with Postgres), so how can PreparedStatement include it and then set parameters values? – Ekaterina May 18 '23 at 14:47