Selecting a fetch strategy Optimization step by step

Selecting a fetch strategy Optimization step by step First, enable the Hibernate SQL log. You should also be prepared to read, understand, and evaluate SQL queries and their performance characteristics for your specific database schema: Will a single outer-join operation be faster than two selects? Are all the indexes used properly, and what is the cache hit-ratio inside the database? Get your DBA to help you with that performance evaluation; only he has the knowledge to decide what SQL execution plan is the best. (If you want to become an expert in this area, we recommend the book SQL Tuning by Dan Tow, [Tow, 2003].) The two configuration properties hibernate.format_sql and hibernate. use_sql_comments make it a lot easier to read and categorize SQL statements in your log files. Enable both during optimization. Next, execute use case by use case of your application and note how many and what SQL statements are executed by Hibernate. A use case can be a single screen in your web application or a sequence of user dialogs. This step also involves collecting the object retrieval methods you use in each use case: walking the object links, retrieval by identifier, HQL, and Criteria queries. Your goal is to bring down the number (and complexity) of SQL statements for each use case by tuning the default fetch plan and fetching strategy in metadata. It s time to define your fetch plan. Everything is lazy loaded by default. Consider switching to lazy=”false” (or FetchType.EAGER) on many-to-one, one-to-one, and (sometimes) collection mappings. The global fetch plan defines the objects that are always eagerly loaded. Optimize your queries and enable eager fetching if you need eagerly loaded objects not globally, but in a particular procedure a use case only. Once the fetch plan is defined and the amount of data required by a particular use case is known, optimize how this data is retrieved. You may encounter two common issues: The SQL statements use join operations that are too complex and slow. First optimize the SQL execution plan with your DBA. If this doesn t solve the problem, remove fetch=”join” on collection mappings (or don t set it in the first place). Optimize all your many-to-one and one-to-one associations by considering if they really need a fetch=”join” strategy or if the associated object should be loaded with a secondary select. Also try to tune with the global hibernate.max_fetch_depth configuration option, but keep in mind that this is best left at a value between 1 and 5.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Leave a Reply