MySQL的;实体框架;插入太慢

时间:2015-05-17 14:02:16

标签: c# mysql entity-framework visual-studio-2013

对不起,这可能是一个活泼的问题,但我不知道如何解决这个问题。我有MySQL数据库,我使用Entity FrameWork工作。模型看起来像这样:http://img42.com/yr36w+(Stackoverflow不想加载图片,所以我会给你链接)。

我有控制器,在我的数据库中添加内容和一些细节,通过整个表格。 它创造了:

  1. 用户
  2. 关于他的详细信息(姓名,地址等)
  3. 他的内容(我的电脑上的图像上有0..10个链接)
  4. 一篇文章
  5. 将此帖子中的链接添加到他的部分内容
  6. 从帖子添加链接到0..10个用户(用户,喜欢这篇文章)
  7. 我希望通过这个迭代至少进行10000次(创建10k完全伴随用户)。我从第一版开始做了一些改进,它们大大减少了演奏时间,但无论如何它需要太长时间。现在在5k之后,它需要3秒才能完成迭代。

    我不知道如何进一步改善。所有方法都同样有效,但我会给你整个控制器以便更好地理解。你能说出我的代码有问题点吗?请帮助,对我来说非常有用,因为我真的不知道要朝哪个方向移动。

    string images = @"C:\Users\Kirill\Documents\Visual Studio 2013\Projects\MySQLMVC2\MySQLMVC2\Images";
        private static courseworkEntities db = new courseworkEntities();
        static Random randomizer = new Random();
        int postCategoryLength = db.postcategory.Count();
        int usersLength = db.user.Count();
        int countryLength = db.country.Count();
    
    
       private void createUser()
        {
            user newUser = db.user.Create();
            newUser.login = getRandomString(10);
            newUser.password = getRandomString(10);
            newUser.email = string.Concat(getRandomString(6), "@gmail.com".ToString());
            db.user.Add(newUser);
            db.SaveChanges();
            usersLength += 1;
            int iduser = newUser.iduser;
            createUserDetails(iduser);
            int howMuch = randomizer.Next(11);
            for(int i=0;i<howMuch;i++)
            createContent(iduser);
            createPost(iduser, howMuch);
        }
    
        private userdetails createUserDetails(int iduser)
        {
            userdetails newUserDetails = db.userdetails.Create();
            newUserDetails.Surname = getRandomString(10);
            newUserDetails.Name = getRandomString(10);
            newUserDetails.Phone = getRandomString(9);
            newUserDetails.iduserDetails = iduser;
            db.userdetails.Add(newUserDetails);
            db.SaveChanges();
            createAddress(newUserDetails.iduserDetails);
            return newUserDetails;
        }
    
        private post createPost(int iduser, int amountOfContent)
        {
            post newPost = db.post.Create();
            newPost.ownerId = iduser;
            int idPostCategory = randomizer.Next(1, postCategoryLength);
            newPost.postCategory = idPostCategory;
            newPost.Name = getRandomString(10);
            newPost.Description = getRandomString(10);
            newPost.CreateDateTime = DateTime.Now;
            db.post.Add(newPost);
            db.SaveChanges();
            int idpost = newPost.idpost;
            amountOfContent = randomizer.Next(0, amountOfContent+1);
            for (int i = 0; i < amountOfContent; i++)
            {
                createPostContent(newPost);
            }
            amountOfContent = randomizer.Next(0, Math.Min(usersLength, 10));
            for (int i = 0; i < amountOfContent; i++)
            {
                createUserLiked(idpost);
            }
            return newPost;
        }
    
        private postcontent createPostContent(post post)
        {
            postcontent newPostContent = db.postcontent.Create();
            newPostContent.postID = post.idpost;
            content content = post.user.content.ElementAt(randomizer.Next(0, post.user.content.Count()));
            newPostContent.contentId = content.idcontent;
            db.postcontent.Add(newPostContent);
            db.SaveChanges();
            return newPostContent;
        }
    
        private usersliked createUserLiked(int idpost)
        {
            usersliked newUsersLiked = db.usersliked.Create();
            newUsersLiked.postId = idpost;
            //int length = db.user.Count();
            //user user = post.user;
            int id = randomizer.Next(29, usersLength);
            //user user = db.user.Find(id);
            newUsersLiked.userId = id;
            db.usersliked.Add(newUsersLiked);
            db.SaveChanges();
            return newUsersLiked;
        }
    
        private address createAddress(int iduserDetails)
        {
            address newAddress = db.address.Create();
            newAddress.addressLine = getRandomString(20);
            newAddress.city = getRandomString(10);
            newAddress.postalCode = getRandomInt(6);
            newAddress.idaddress = iduserDetails;
            int idCountry = randomizer.Next(1, countryLength);
            newAddress.countryId = idCountry;
            db.address.Add(newAddress);
            db.SaveChanges();
            return newAddress;
        }
    
        private content createContent(int iduser)
        {
            content newContent = db.content.Create();
            newContent.ownerId = iduser;
            newContent.path = System.IO.Path.Combine(images, String.Concat(randomizer.Next(1, 500001).ToString(), ".jpg"));
            db.content.Add(newContent);
            db.SaveChanges();
            return newContent;
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(int model)
        {
            for (int i = 0; i < model;i++)
                createUser();
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    

0 个答案:

没有答案