I’m struggling with a performance issue for a copy feature that my application provides to the users. I’m not sure I’m going about it the right way. I don’t have a lot of experience in Hibernate.
An over-simplified layout would be:
Project ---> (One to Many) Items ---> (One to Many) Tasks
Project ---> (One to Many) Variables
Items <---> Variables (Many to Many)
So, a project can have many items, and each item can have many tasks. A project can have variables (no duplicates), and there is a many to many between items and variables (they are attached to one another).
There could be several hundred items per project, with several (20-30) tasks per item, and several hundred variables per project. I added a function that allows users to check which items and tasks they want to copy from one project to another. There cannot be any duplicates.
Here’s the process I perform for each item that is checked:
- Check to see if the item exists in the target project, if it does, add it, otherwise, don’t. I used the session.createFilter to query the target project (by item name). If the item does not exist, I add it. (
object.save()), and add it to the target project’s items collection. - If the item has variables attached to it, I check the target project for those variables (again
session.createFilter()), add them if they are not there, then add them to the item.variables collection
I’m not sure what, if anything, I can do to speed this up. I’m assuming doing all the checks first isn’t helping things. I don’t know if the createFilter is the best way for checking that items/variables exist in the target project.
I’ve tried changing the way the objects are added to collections. I would add them each one at a time (targetProject.add(item)), now I’m storing them in an arraylist and when all items are iterated through, I then do a targetProject.addAll(items). I couldn’t tell a difference, though.
Is there a better way of doing this? I’m using Ehcache as a second-level caching. I’m also still on Hibernate 3.2.5.
Any help is appreciated.
It looks like your code issues too many database queries due to frequent use of
createFilter().It would be better to fetch all needed objects into memory by a few queries, and then perform necessary checks using appropriate in-memory data structures (
Maps,Sets).