简体   繁体   English

保存到数据库需要太多时间

[英]Saving to database taking too much time

I have to insert some record in database but it is taking too much time around 20-25 seconds. 我必须在数据库中插入一些记录,但是这花费了大约20-25秒的太多时间。 I have two methods first contains a foreach loop with list type parameter and second method has some Linq query to save and delete some record. 我有两种方法,第一种方法包含一个带有列表类型参数的foreach循环,第二种方法具有一些Linq查询来保存和删除一些记录。 The code is given below. 代码如下。

 public void InsertAssignment(List<TIVGroupItem> Item)
        {
            using (var tr = session.BeginTransaction())
            {
                try
                {
                    foreach (var item in Item)
                    {
                        ak_tIVGroupItemSelector_Insert(item);
                    }                
                    tr.Commit();
                }
                catch (Exception ex)
                {
                    tr.Rollback();
                    CusException cex = new CusException(ex);
                    cex.Write();
                }
            }

        }

second method 第二种方法

 public void ak_tIVGroupItemSelector_Insert(TIVGroupItem GroupItem)
        {
            try
            {
                var result = (from i in session.Query<TIVGroupItem>()
                              where i.Id == GroupItem.Id
                              select i).SingleOrDefault();
                if (result != null)
                    session.Delete(result);
                session.Save(GroupItem);
            }
            catch (Exception ex)
            { CusException cex = new CusException(ex);
                    cex.Write();
            }
        }

Item list contains some records. 项目列表包含一些记录。 So how can I improve my performance with this type of problem. 因此,我该如何改善此类问题的表现。 Is there any way to increase my performance to save the record in database? 有什么方法可以提高我的性能以将记录保存到数据库中?

With default NHibernate settings, every time you query NHibernate will check if the session contains any changes that might affect the outcome of the query. 使用默认的NHibernate设置,每次查询NHibernate时都会检查会话是否包含任何可能影响查询结果的更改。 So as the number of objects active in the session grows, if you do repeated querying, performance will drop. 因此,随着会话中活动对象的数量增加,如果您重复查询,性能将会下降。

Avoid doing queries in a loop. 避免循环查询。 It's better to query for multiple objects earlier. 最好早些查询多个对象。

Furthermore, you appear to be loading an object by primary key, for which you should use session.Get(id) instead. 此外,您似乎正在通过主键加载对象,对此应使用session.Get(id)。 It is much faster, since it will bypass the dirty-checking mentioned above, and also look in the session's first-level cache first. 它的速度要快得多,因为它将绕过上面提到的脏检查,并且还将首先查看会话的一级缓存。

But like others said, you appear to be deleting and object and then immediately putting it back - ie it should normally be an update instead of delete+insert. 但是,就像其他人所说的那样,您似乎正在删除并成为对象,然后立即将其放回原位-即,通常它应该是更新而不是delete + insert。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM