简体   繁体   English

在表中插入相关数据比预期的慢

[英]Inserting related data in tables is slower than expected

I created a program which records list of family. 我创建了一个记录家庭名单的程序。

The records are written from an index card. 记录是从索引卡写入的。

In the index card. 在索引卡中。 It contains the Ancestor and its relative. 它包含祖先及其亲属。

I have two tables 我有两张桌子

table 1 表格1

Information about the ancestor, like name, ID (auto increment), filename, username(the one in the PC) 有关祖先的信息,例如名称,ID(自动递增),文件名,用户名(PC中的一个)

table 2 表2

Information about the relatives, like name, ancestor_id(from table 1) 有关亲戚的信息,例如姓名,ancestor_id(来自表1)

There are 20 people using the program. 有20个人使用该程序。 So i need to get the ID generated when the ancestor is inserted before inserting the relatives. 所以我需要获得插入祖先时生成的ID,然后再插入亲戚。

sometimes there are 10 relatives in the index. 有时索引中有10个亲戚。 and sometimes there are 20 有时有20

this is my stored procedure 这是我的存储过程

INSERT INTO tbl_ancestor(an,`as`, dd, cn, su, mm, stn, re, yy, st,  filename, username, `zipName`, `batchName`, `folderName`, `remark`) 
        VALUES(_an, _as, _dd, _cn, _su, _mm, _stn, _re, _yy, _st, _fname, _username, _zipName, _batchName, _folderName, _remark);
        SELECT ID, filename, username, zipName, batchName, foldername FROM tbl_ancestor;

But when the record in the database becomes 10000 it waits for 5 seconds for the message to prompt that the data has been saved. 但是,当数据库中的记录变为10000它将等待5秒钟,以使该消息提示您已保存数据。

I only use select statement. 我只使用select语句。 Because i thought it will help me solve the problem if i use a linq in my c# code to get the ID of that ancestor to be used my the relatives. 因为我认为如果我在C#代码中使用linq来获取该祖先的ID以供亲戚使用,它将帮助我解决问题。 But the speed is the same. 但是速度是一样的。

Is this an error in my query? 这是我的查询错误吗?

or in my c# codes? 或在我的C#代码中?

foreach (var item in tag.RelativeList)
{
    using (MySqlCommand cmd = new MySqlCommand("sp_insert_relative", conn)
    {
        CommandType = System.Data.CommandType.StoredProcedure
    })
    {
        if (conn.State == System.Data.ConnectionState.Closed)
            conn.Open();

        cmd.Parameters.AddWithValue("_ancestor_id", id);
        cmd.Parameters.AddWithValue("_rgn", item.RGN);
        cmd.Parameters.AddWithValue("_rsn", item.RSN);
        cmd.Parameters.AddWithValue("_sgn", item.SGN);
        cmd.Parameters.AddWithValue("_ssn", item.SN);
        cmd.Parameters.AddWithValue("_username", tag.Username);
        cmd.Parameters.AddWithValue("_fname", System.IO.Path.GetFileName(tag.FileName));
        cmd.ExecuteNonQuery();
    }
}

Is there any way to solve this? 有什么办法解决这个问题? To make my program faster? 为了使我的程序更快? I can't think of a way to solve it. 我想不出解决办法。 I've been searching also. 我也一直在搜寻。 But i can't find a concrete solution. 但是我找不到具体的解决方案。 Or maybe i'm using a wrong search keyword? 或者,也许我使用了错误的搜索关键字?

          using (MySqlTransaction trans = conn.BeginTransaction())
          {
              foreach (var item in tag.RelativeList)
              {
        using (MySqlCommand cmd = new MySqlCommand("sp_insert_relative",conn)
        {
            CommandType = System.Data.CommandType.StoredProcedure
        })
        {
            if (conn.State == System.Data.ConnectionState.Closed)
                conn.Open();

            cmd.Parameters.AddWithValue("_ancestor_id", id);
            cmd.Parameters.AddWithValue("_rgn", item.RGN);
            cmd.Parameters.AddWithValue("_rsn", item.RSN);
            cmd.Parameters.AddWithValue("_sgn", item.SGN);
            cmd.Parameters.AddWithValue("_ssn", item.SN);
            cmd.Parameters.AddWithValue("_username", tag.Username);
            cmd.Parameters.AddWithValue("_fname", System.IO.Path.GetFileName(tag.FileName));
            cmd.ExecuteNonQuery();
        }
trans.commit
    }
}

You can use MysqlTransaction To fasten the insertion OR update a data into your Database. 您可以使用MysqlTransaction来加快插入速度,或将数据更新到数据库中。

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

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