These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Inefficient SQL when deleting parent and its children.
PostPosted: Fri Oct 26, 2007 4:43 am 
Newbie

Joined: Fri Oct 26, 2007 4:09 am
Posts: 1
I've been looking at my SQL Profiler output when deleting my Parent object that has a collection of children mapped as a set, and want to know if I can modify the mapping files to improve the generated SQL.

Currently, if I delete my parent object, nhibernate correctly removes the child items before deleting the parent item (thus avoiding an FK constraint error), but it does it one child at a time, instead of doing a "Delete from CHI_Child where PAR_Parent_ID = 36".

Is there any way I can improve performance here?

Many thanks in advance,

Phil


Hibernate version: 1.2.0.4000


The only code used between the start and close session is this:
Code:
//iParent references the hibernate Session
oParent = iParent.GetId(parentId);
iParent.Delete(oParent);


Mapping documents:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" default-lazy="true">
   
   <class lazy="true" table="[PAR_Parent]" name="MyDB.Core.Domain.DataAccess.Parent, MyDBLib">
      <id name="ParentID" access="property" column="[PAR_ParentID]">
         <generator class="identity" />
      </id>

      <set name="Children"  cascade="all" inverse="true">
         <key column="PAR_ParentID"/>
         <one-to-many class="MyDB.Core.Domain.DataAccess.Child, MyDBLib"/>
      </set>
      
   </class>
   
   <class lazy="true" table="[CHI_Child]" name="MyDB.Core.Domain.DataAccess.Child, MyDBLib">
      <id name="ChildID" access="property" column="[CHI_ChildID]">
         <generator class="identity" />
      </id>

      <!-- Foreign keys -->
      <many-to-one name="RefParent" class="MyDB.Core.Domain.DataAccess.Parent, MyDBLib" not-null="true">
         <column name="PAR_ParentID" />
      </many-to-one>      
   </class>
</hibernate-mapping >



Name and version of the database you are using:SQL Server 2000

The generated SQL (show_sql=true):
exec sp_executesql N'DELETE FROM [CHI_Child] WHERE [CHI_ChildID] = @p0', N'@p0 bigint', @p0 = 29
go
exec sp_executesql N'DELETE FROM [CHI_Child] WHERE [CHI_ChildID] = @p0', N'@p0 bigint', @p0 = 30
go
exec sp_executesql N'DELETE FROM [CHI_Child] WHERE [CHI_ChildID] = @p0', N'@p0 bigint', @p0 = 31
go
exec sp_executesql N'DELETE FROM [CHI_Child] WHERE [CHI_ChildID] = @p0', N'@p0 bigint', @p0 = 32
go
exec sp_executesql N'DELETE FROM [PAR_Parent] WHERE [PAR_ParentID] = @p0', N'@p0 bigint', @p0 = 36
go


Top
 Profile  
 
 Post subject: Re: Inefficient SQL when deleting parent and its children.
PostPosted: Sun Oct 28, 2007 2:09 pm 
Newbie

Joined: Tue Oct 11, 2005 8:02 am
Posts: 6
philg wrote:

Is there any way I can improve performance here?


You can read about this topic here:
http://www.hibernate.org/hib_docs/nhibe ... ch-updates
and here:
http://www.ayende.com/Blog/archive/7269.aspx

_________________
Best regards,
Thomas

--------------------------------------------------------
NHibernate based .NET application generator
http://www.nconstruct.com
--------------------------------------------------------


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.