sqlite 60000 rows into the database only less than 2 seconds

clipboard

clipboard

My name is dic.db database and program together

CREATE TABLE [dic](
  [ID] INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE ON CONFLICT REPLACE,
  [Word] the TEXT  UNIQUE  the ON The CONFLICT the REPLACE,
  [English phonetic] the TEXT  the COLLATE the NOCASE,
  [American phonetic] the TEXT  the COLLATE the NOCASE,
  [Explain] the TEXT );

clipboard

clipboard

So spell out insert sql string should look like this

INSERT  or Replace INTO [DIC] (word English phonetic, American phonetic symbol, interpretation) values ( ' zooks ', ' [zuːks] ', ' [zuːks] ', ' acting as a mild vows sigh phrase; ');

C#代码


  //批量Insert Into
        private void buttonInsertInto_Click(object sender, EventArgs e) {

            sw.Reset();
            sw.Start();
            string ConnectionString = "Data Source = dic.db; Version=3;";
            SqliteHelper sqlite = new SqliteHelper(ConnectionString);
            sqlite.BatchExecuteCmd(richTextBox4.Lines);

            sw.Stop();
            MessageBox.Show("批量插入SQL语句耗时:" + ( sw.ElapsedMilliseconds / 1000f ).ToString("0.0000 sec"));

        }

        //纯替换
        Stopwatch sw = new Stopwatch();
        private void buttonReplace_Click(object sender, EventArgs e) {

            sw.Start();

            //用的是替换 速度是瞬间的 没有判断,先移除所有空行


            //瞬间插入6万行 用替换的办法
            //zookeeper	['zuːkiːpə(r)]	['zuːkiːpər]	n.动物园管理员;
            //替换成这样
            //insert or replace into [dic] (单词,英式音标,美式音标,解释) 
            //values ('zookeeper','['zuːkiːpə(r)]','	['zuːkiːpər]','n.动物园管理员;');

            string s = richTextBox4.Text;
            //1/3最左边替换成 insert or replace into [dic] (单词,英式音标,美式音标,解释) values (' 
            Regex r = new Regex("^", RegexOptions.Multiline);
            s = r.Replace(s, "insert or replace into [dic] (单词,英式音标,美式音标,解释) values ('");

            //2/3 tab 替换成 ','  下面的空格并不是空格而是复制的tab空格
            r = new Regex("	", RegexOptions.Multiline);
            s = r.Replace(s, "','");

            //3/3最右边替换成 ');
            r = new Regex("$", RegexOptions.Multiline);
            s = r.Replace(s, "');");

            richTextBox4.Text = s;
            sw.Stop();
            MessageBox.Show( "替换耗时:" + ( sw.ElapsedMilliseconds / 1000f ).ToString("0.0000 sec"));
        }
61,991 lines with optical paste it are 2 seconds

clipboard

61991 row cursor replace it with 7.8 seconds if for the words to be small level, you have to be six hours, got to seven hours?

clipboard

SQL command batch execution took only 1.2 seconds

clipboard

clipboard



  /// <the Summary> 
        SQL statement after the /// format conversion, things execution sentence statement This statement is instantaneous even 10,000, too 
        /// a sentence converted into a SQL statement is quite slow 
        /// INSERT INTO table 2 (Keyword, Trans, phrase) VALUES ( 'undoubtedly', ' interpretation', 'phrases'); 
        /// </ Summary> 
        /// <param name = "sqlArr"> each row is complete iNSERT INTO statement, each insert a </ param> 
        /// <returns> SQL statement returns an exception </ returns> 
        public  String BatchExecuteCmd ( String [] sqlArr)
        {
            StringBuilder sb = new StringBuilder();
            using (SQLiteConnection conn = new SQLiteConnection(ConnectionString))
            {
                SQLiteCommand cmd = new SQLiteCommand(conn);
                {
                    conn.Open();
                    using (DbTransaction trans = conn.BeginTransaction())
                    {
                        for (int i = 0; i < sqlArr.Length; i++)
                        {
                            try
                            {
                                cmd.CommandText = sqlArr[i];
                                cmd.ExecuteNonQuery();
                            }
                            catch
                            {
                                //下面是插入失败的内容
                                sb.Append(sqlArr[i] + "\n");
                                //trans.Rollback();//回滚事务
                                //MessageBox.Show(EX.Message);
                            }
                        }
                        trans.Commit();
                        //richTextBox2.Text = sb.ToString();
                    }
                }
            }
            return sb.ToString();
        }

    }


sqlite bulk insert speed is quite fast, and converted into a sql statement is quite time-consuming (used for line by line traversing, then then split apart in each row, this process was very time-consuming, because there are 60,000 lines, what not to do light finish again have an hour).

Guess you like

Origin www.cnblogs.com/xe2011/p/12109842.html