c# - MySql插入忽略性能

标签 c# mysql sql-server-ce

我必须将 90Mb 的数据插入 MySql 表中,并且我正在使用 INSERT IGNORE 命令来避免重复键的异常。性能是每秒8条记录,但看起来很慢。我可以加快速度吗?

附:由于我从 sql 紧凑数据库读取数据,因此我正在每条记录插入一条记录

using (SqlCeConnection sqlConnection = new SqlCeConnection(connectionstrCe))
            {
                sqlConnection.Open();
                SqlCeCommand cmdCe = sqlConnection.CreateCommand();
                {
                    {

                        mySQLConnection.Open();

                        foreach (KeyValuePair<string, List<string>> t in tablesCeNames) //reading the tables property from the dictionary - column names and datatypes
                        {

                            string tableData = t.Key; 
                            List<string> columnData = t.Value;
//get the values from the table I want to transfer the data
                                cmdText = "SELECT * FROM " + tableData;
//compose the mysql command
                                cmdCe.CommandText = cmdText;

                                SqlCeDataReader dataReader = cmdCe.ExecuteReader(); //read
//InsertTable is a method that get the datareader and convert all the data from this table in a list array with the values to insert
                                inputValues = InsertTables(dataReader);


                                MySql.Data.MySqlClient.MySqlTransaction transakcija;
                                transakcija = mySQLConnection.BeginTransaction();

                                worker.ReportProgress(4, inputValues.Count);

                                foreach (string val in inputValues)//foreach row of values of the data table
                                {
                                    cmdSqlText = "INSERT IGNORE INTO " + tableData + "("; //compose the command for sql

                                    foreach (string cName in columnData)  //forach column in a table
                                    {
                                        string[] data = cName.Split(' ');
                                        if (!data[0].ToString().Equals("Id"))
                                        {
                                            cmdSqlText += data[0].ToString() + ","; //write the column names of the values that will be inserted  
                                        }
                                    }
                                    cmdSqlText = cmdSqlText.TrimEnd(',');
                                    cmdSqlText += ") VALUES (";
//val contains the values of this current record that i want to insert
                                    cmdSqlText += val;  //final command with insert ignore and the values of one record
                                    if (!val.Equals(""))
                                    {
                                        try
                                        {
                                            new MySql.Data.MySqlClient.MySqlCommand(cmdSqlText, mySQLConnection, transakcija).ExecuteNonQuery(); //execute insert on sql database
                                            WriteToTxt("uspješno upisano u mysql" + t.Key);
                                        }
                                        catch (MySql.Data.MySqlClient.MySqlException sqlEx)
                                        {
                                        }
                                    }
                                }

                                if (TablicaSveOK)
                                {
                                    transakcija.Commit();

                                }
                                else
                                {
                                    transakcija.Rollback();
                                }
                            }
                        }

                        if (mySQLConnection.State != System.Data.ConnectionState.Closed)
                        {
                            mySQLConnection.Close();
                        }
                    }

最佳答案

如何将数据从 Sql 获取到文件并使用 LOAD DATA?

http://dev.mysql.com/doc/refman/5.0/es/load-data.html

关于c# - MySql插入忽略性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17989470/

相关文章:

c# - Silverlight:使用 UI Automation 模拟 RadioButton Click 事件?

c# - 没有声音时停止录音

c# - 哪里有一个很好的 SQL CE 复制演示?

c# - 使用 LINQPad 将数据插入 SQL CE 数据库,Linq-to-sql,随着时间的推移变得越来越慢,我能做些什么呢?

c# - 不支持关键字 : 'attachdbfilename' - MDF Database File (C#)

c# - Xamarin 自定义键盘

c# - 模拟离散房间中的减压

mysql - 从数据库表中获取每月的记录

python - Djangosyncdb 在应用 auth.0001_initial 时失败

php - cakePHP 和 EasyPHP 无法烘焙