posted 11 years ago
As far as I know there is no way to "pre-issue" SQL from Hibernate to the database before the first query � you can bypass that by using stored functions/procedures. The general performance guidelines are:
Tune you SQL or tune your HQL to generate the best SQL; enlist the help of a DBA and/or consult something like SQL Tuning to come up with the best SQL possible (with can vary across database implementations). Parameterize your SQL, rather than generating the SQL dynamically (the fact that your queries are only slow the first time suggest that you have already done this � however your SQL still may not be optimally tuned). Higher-end databases will cache the parameterized SQL � to get the full benefit your application needs to use connection sharing, i.e. all application instances logon to the DB with exactly the same user credentials. The only way to bypass the "first query penalty" is to move the query into a stored function/procedure. That way the compiled function/procedure is already sitting in the database. However don't get caught in the "procedural thinking" trap inside the stored procedure/function. Properly tuned relational queries will still be much faster inside the stored procedure/function than iterating through large result-set cursors.