object taking time to get persisted in hibernate
I have three entities, in which i try to save only 1 entity right now. All the three entities are shown below :-
1. Student Entity
<class name="com.school.Student" table="TABLE_STUDENT">
<id name="id" type="long">
<column name="ST_ID" />
<generator class="native" />
</id>
<property name="name" type="string" column="ST_NAME"/>
<many-to-one name="studentSection" class="com.school.Section" fetch="select">
<column name="SECTION_ID" not-null="true" />
</many-to-one>
<many-to-one name="studentSportsTeam" class="co开发者_Go百科m.school.SportsTeam" fetch="select">
<column name="SPORTS_TEAM" not-null="true" />
</many-to-one>
</class>
2. Section Entity
<class name="com.school.Section" table="TABLE_SECTION">
<id name="sectionId" type="string">
<column name="SECTION_ID" />
<generator class="assigned" />
</id>
<property name="floor" type="string" column="SEC_FLOOR"/>
<property name="capcacity" type="int" column="SEC_CAPACITY"/>
<set name="studentDetails" inverse="true" lazy="true" table="TABLE_STUDENT" fetch="select">
<key>
<column name="SECTION_ID" not-null="true" />
</key>
<one-to-many class="com.school.Student" />
</set>
</class>
3. SprotsTeam Entity :-
<class name="com.school.SportsTeam" table="TABLE_SPORTS">
<id name="sportsTeamId" type="string">
<column name="SPORTS_TEAM" />
<generator class="assigned" />
</id>
<property name="noOfPlayers" type="int" column="SPORTS_PLAYER_NUM"/>
<property name="captainName" type="string" column="SPORTS_CAPTAIN_NAME"/>
<set name="playerDetails" inverse="true" lazy="true" table="TABLE_STUDENT" fetch="select">
<key>
<column name="SPORTS_TEAM" not-null="true" />
</key>
<one-to-many class="com.school.Student" />
</set>
</class>
Now if i try to save Student
Entity with proper Section
and SportsTeam
details, it takes a lot of time to persist it into the database. Currently i am running it for around 10000 students and this process (only persisting) takes around 15 mins. I added some loggers to calculate the complete time.
Now i need to reduce this time, as we will shorty move from 10,000 to 1 million records, and as calculated it takes very long time.. I need to reduce the time , how can i do that??
As Required, also the schema is as below :-
TABLE STUDENT :
ST_ID NUMBER,
ST_NAME VARCHAR(40),
SECTION_ID VARCHAR(10),
SPORTS_TEAM VARCHAR(10)
TABLE_SECTION :
SECTION_ID VARCHAR(10),
SEC_FLOOR VARCHAR(2),
SEC_CAPACITY NUMBER
TABLE_SPORTS :
SPORTS_TEAM VARCHAR(10),
SPORTS_PLAYER_NUM NUMBER,
SPORTS_CAPTAIN_NAME VARCHAR(40)
Please help
Consider batch inserts.
Moreover, if this batch inserts is just for once i.e. as a part of migration process, then IMO, you can consider dropping all primary keys -- and any other indices, on the tables, and then insert the records. After that re-create all those. Hopefully, you would notice a significant improvement.
I think you are trying to import the Student Entity with Section and SportsTeam. At this time if you set the values from the input data to the elements of the Entities and call save on Student entity then it would result into creation of a Student, Section and SportsTeam records in the data base. Which means if you save 10000 student entity then you are effectvely creating 10000 SportsTeam record and 10000 Section record.
Instead I would suggest you to follow these steps: 1. Read a line from the input data 2. query db (through hql) for the already exiting SportsTeam and Section 3. If no records found for SportsTeam and Section then create them 3. Create a new student record and set the SportsTeam and Section from step2. 4. Save the student record.
Also I would suggest you to optimize the creation and save of Students objects in a batch of say 1000 in a single transaction. Closing the hibernate transaction after a batch and releasing the objects would help increase the utilization of DB/Network as well as memory.
I would specify a length for string properties; otherwise they may be implemented as clobs
精彩评论