sqlite sqlitecommand ExecuteNonQuery vs ExecuteNonQueryAsync c#

[亡魂溺海] 提交于 2020-06-09 05:25:07

问题


I am using c# sqlite version 3. My application requires saving up to 20 columns of data at 4000Hz. To accomplish this I am using threads that take commands from a queue and periodically write/read to the database. Currently, I am just working on the writing portion and have found that ExecuteNonQueryAsync() and ExecuteNonQuery() have relatively the same execution time which makes no sense to me. My function is as follows:

        public async Task<int> dumpStorage(string cmdStr)
        {
            int r;
            using (SQLiteCommand cmd = new SQLiteCommand(sqliteConnection))
            {
                    string times = "";
                    Stopwatch tTime = new Stopwatch();
                    tTime.Start();
                cmd.CommandText = cmdStr;
                    times += "tTime1 " + tTime.Elapsed.Seconds + ":" + tTime.Elapsed.Milliseconds + "\n";
                    tTime.Restart();
                r = await cmd.ExecuteNonQueryAsync();
                //cmd.ExecuteNonQuery();
                //cmd.ExecuteScalar();
                //cmd.ExecuteScalarAsync();
                    times += "tTime2 " + tTime.Elapsed.Seconds + ":" + tTime.Elapsed.Milliseconds + "\n";
                    tTime.Restart();
                transaction.Commit();
                    times += "tTime2 " + tTime.Elapsed.Seconds + ":" + tTime.Elapsed.Milliseconds + "\n";
                    tTime.Restart();
                transaction = sqliteConnection.BeginTransaction();
                    tTime.Stop();
                    times += "tTime3 " + tTime.Elapsed.Seconds + ":" + tTime.Elapsed.Milliseconds + "\n";
                    timeStr = times;
                storeToDBWatchDog.Restart();
            }
            return r;
        }

at the moment I am saving 40,000 rows with 20 (plus a primary key) columns which represents 10 seconds of data. I am unable to get the async to have any time advantage over the non-async function. in the future I will need to introduce a scheme that will manage if there was any failures to insert.

I would expect that ExecuteNonQueryAsync() should make tTime2 essentially 0 and it would work in the background however that is not what I am seeing as it appear to wait for the ...Async() function to complete.

the execute non query async and non-async require roughly 2.1 seconds. The end goal is to save the data at least 20 times faster than it is coming in. The only time that this becomes very important is on initial load. By this I mean i am reading an existing file (not in a database) and then saving it into the database. so, at the moment I only have approximately 4 times the performance. by this i mean it would take 5 minutes to save a file that consists of data associated with 20 minutes. this seems unexceptionably slow.

typical time output regardless of which function call is used (both scalar functions take ~50% longer):

Edit: I recently came across this unfortunate post, which essentially states that async doesn't do anything so you should use the WAL setting: https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/async

i have since changed my code to include the WAL journal-mode:

            connectionString = "Data Source=" + filename + "; Version=3" + "; PRAGMA journal_mode=WAL;";
            sqliteConnection = new SQLiteConnection(connectionString);

I did not see any performance increases setting the journal_mode to WAL.

来源:https://stackoverflow.com/questions/61212405/sqlite-sqlitecommand-executenonquery-vs-executenonqueryasync-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!