簡體   English   中英

改善插入時的sqlite.net性能

[英]improve sqlite.net performance at insert

我有要存儲在數據庫中的記錄數。
數據庫的架構是這樣的
像這樣的人表:

CREATE TABLE IF NOT EXISTS Person (id INTEGER PRIMARY KEY, name TEXT)

和這樣的文件表:

CREATE TABLE IF NOT EXISTS File (id INTEGER PRIMARY KEY, FileName TEXT,FilePath TEXT, PersonID NUMERIC CONSTRAINT person_file REFERENCES [Person]([id])ON DELETE NO ACTION ON UPDATE NO ACTION)


我使用此功能在兩個表中進行插入


對於3700條記錄,此代碼在筆記本電腦上花費了140秒,

public long BuildDB(List<DB.Person> persons, List<DB.File> Files, FrmTrain form)
        {
            long result = -1;
            try
            {

                long personID = 0;
                using (SQLiteConnection sqlconnection = new SQLiteConnection("Data Source=" + DbPath + ";Version=3;"))
                {
                    sqlconnection.Open();
                    SQLiteCommand PersonCommand = sqlconnection.CreateCommand();
                    SQLiteParameter personParam = new SQLiteParameter();
                    PersonCommand.CommandText = "INSERT INTO Person('Name') VALUES(?)";
                    PersonCommand.Parameters.Add(personParam);


                    SQLiteCommand FileCommand = sqlconnection.CreateCommand();
                    SQLiteParameter FileParam1 = new SQLiteParameter("@filename");
                    SQLiteParameter FileParam2 = new SQLiteParameter("@filepath");
                    SQLiteParameter FileParam3 = new SQLiteParameter("@personid");

                    FileCommand.CommandText = "INSERT INTO file(FileName,FilePath,PersonID) VALUES(@filename,@filepath,@personid)";
                    FileCommand.Parameters.Add(FileParam1);
                    FileCommand.Parameters.Add(FileParam2);
                    FileCommand.Parameters.Add(FileParam3);
                    for (int i = 0; i < persons.Count; i++)
                    {
                        using (SQLiteTransaction _SQLiteTransaction = sqlconnection.BeginTransaction())
                        {
                            personParam.Value = persons[i].Name;
                            PersonCommand.ExecuteNonQuery();
                            personID = sqlconnection.LastInsertRowId;

                            foreach (var item in Files.Where(f => f.PersonID == personID))
                            {
                                FileParam1.Value = item.FileName;
                                FileParam2.Value = item.FilePath;
                                FileParam3.Value = item.PersonID;
                                FileCommand.ExecuteNonQuery();

                            }
                            _SQLiteTransaction.Commit();
                        }
                        form.Progress();
                    }
                    sqlconnection.Close();
                }
                result = 1;

            }
            catch (Exception e)
            {
                result = 0;
                throw;
            }



            return result;

        }


首先,我編寫了沒有事務的代碼,並花了大約500秒的時間。

無論如何,有沒有改善性能的方法?

嘗試對所有插入的人員使用一個SQLiteTransaction,僅使用一次提交。

        try
        {

            long personID = 0;
            using (SQLiteConnection sqlconnection = new SQLiteConnection("Data Source=" + DbPath + ";Version=3;"))
            {
                sqlconnection.Open();
                SQLiteCommand PersonCommand = sqlconnection.CreateCommand();
                SQLiteParameter personParam = new SQLiteParameter();
                PersonCommand.CommandText = "INSERT INTO Person('Name') VALUES(?)";
                PersonCommand.Parameters.Add(personParam);


                SQLiteCommand FileCommand = sqlconnection.CreateCommand();
                SQLiteParameter FileParam1 = new SQLiteParameter("@filename");
                SQLiteParameter FileParam2 = new SQLiteParameter("@filepath");
                SQLiteParameter FileParam3 = new SQLiteParameter("@personid");

                FileCommand.CommandText = "INSERT INTO file(FileName,FilePath,PersonID) VALUES(@filename,@filepath,@personid)";
                FileCommand.Parameters.Add(FileParam1);
                FileCommand.Parameters.Add(FileParam2);
                FileCommand.Parameters.Add(FileParam3);
                using (SQLiteTransaction _SQLiteTransaction = sqlconnection.BeginTransaction())
                {
                    for (int i = 0; i < persons.Count; i++)
                    {
                        personParam.Value = persons[i].Name;
                        PersonCommand.ExecuteNonQuery();
                        personID = sqlconnection.LastInsertRowId;

                        foreach (var item in Files.Where(f => f.PersonID == personID))
                        {
                            FileParam1.Value = item.FileName;
                            FileParam2.Value = item.FilePath;
                            FileParam3.Value = item.PersonID;
                            FileCommand.ExecuteNonQuery();
                        }
                    }
                    _SQLiteTransaction.Commit();
                    form.Progress();
                }
                sqlconnection.Close();
            }
            result = 1;

        }
        catch (Exception e)
        {
            result = 0;
            throw;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM