生产数据库中,大批数据更新时应分批执行

实际生产中,可能由于需求的变化需要对数据库中的数据做统一的更新。这时由于项目已经上线好久时间,数据库中的数据也已经很多了。这时的更新操作如果一次执行,可能对数据库造成数据阻塞,给用户的浏览造成问题。比较好的做法是每更新一批数据后,就停顿一定的时间。
         public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
 
            Entities db = new Entities();
 
            int page = 1;
 
            while (true )
            {
                int take = page * 1000;
                int skip = (page - 1) * 1000;
                var links = db.XXX.OrderBy(p=>p.ContentID).Take(take).Skip(skip).Select(p => new { p.ContentID, p.DownIDs }).ToList();
 
                if (links.Count() == 0)
                {
                    break;
                }
 
               
                for (int i = 0; i < links.Count; i++)
                {
                    string[] downIDs = new string[] { };
                    if (string .IsNullOrEmpty(links[i].DownIDs))
                    {
                        continue;
                    }
 
                    downIDs = links[i].DownIDs.Split( ',');
 
 
                    for (int j = 0; j < downIDs.Length; j++)
                    {
                        int contentId = links[i].ContentID;
                        int downId = Convert .ToInt32(downIDs[j]);
                        var query = db.YYY.FirstOrDefault(p => p.ContentID == contentId && p.DownID == downId);
                        if (query == null )
                        {
                            db.YYY.Add( new Y{ ContentID = contentId, DownID = downId, DateAdded = DateTime .Now });
 
                        }
                    }
 
                }
                db.SaveChanges();
                context.Response.Write( "<br>");
                context.Response.Write(links.Count());
                context.Response.Flush();
                page++;
                System.Threading. Thread.Sleep(100);
            }
posted @ 2012-10-15 22:10  永动机  阅读(839)  评论(0编辑  收藏  举报