From b5d8d6fdd68414bbf0ca2d7db6759a2bf56cb645 Mon Sep 17 00:00:00 2001 From: syneffort Date: Fri, 9 Dec 2022 09:56:27 +0900 Subject: [PATCH] transaction qry --- SQLite_Console/PsqLiteWrapper/PSqLite.cs | 55 ++++++++++++ SQLite_Console/SQLite_Console/Program.cs | 104 ++++++++++++++--------- 2 files changed, 121 insertions(+), 38 deletions(-) diff --git a/SQLite_Console/PsqLiteWrapper/PSqLite.cs b/SQLite_Console/PsqLiteWrapper/PSqLite.cs index 37c2512..28b402d 100644 --- a/SQLite_Console/PsqLiteWrapper/PSqLite.cs +++ b/SQLite_Console/PsqLiteWrapper/PSqLite.cs @@ -73,12 +73,67 @@ namespace PSqLiteWrapper } } + public static void ExecuteQuery(string[] queries) + { + if (string.IsNullOrEmpty(DBPath)) + throw new ArgumentException("Empty DBPath"); + + try + { + using (SQLiteConnection conn = Connection) + { + conn.Open(); + PSqLite.ExecuteQuery(conn, "BEGIN TRANSACTION;"); + + for (int i = 0; i < queries.Length; i++) + { + SQLiteCommand command = new SQLiteCommand(queries[i], conn); + command.ExecuteNonQuery(); + } + + PSqLite.ExecuteQuery(conn, "END TRANSACTION;"); + Connection.Close(); + } + } + catch (Exception ex) + { + throw ex; + } + } + + public static void ExecuteQueryAsync(string query) + { + if (string.IsNullOrEmpty(DBPath)) + throw new ArgumentException("Empty DBPath"); + + try + { + using (SQLiteConnection conn = Connection) + { + conn.Open(); + SQLiteCommand command = new SQLiteCommand(query, conn); + command.ExecuteNonQueryAsync(); + Connection.Close(); + } + } + catch (Exception ex) + { + throw ex; + } + } + public static void ExecuteQuery(SQLiteConnection openedConn, string query) { SQLiteCommand command = new SQLiteCommand(query, openedConn); command.ExecuteNonQuery(); } + public static void ExecuteQueryAsync(SQLiteConnection openedConn, string query) + { + SQLiteCommand command = new SQLiteCommand(query, openedConn); + command.ExecuteNonQueryAsync(); + } + public static DataSet ExecuteSelectQuery(string query) { if (string.IsNullOrEmpty(DBPath)) diff --git a/SQLite_Console/SQLite_Console/Program.cs b/SQLite_Console/SQLite_Console/Program.cs index e45e7c2..8788afb 100644 --- a/SQLite_Console/SQLite_Console/Program.cs +++ b/SQLite_Console/SQLite_Console/Program.cs @@ -47,52 +47,77 @@ namespace SQLite_Console Console.WriteLine(); watch.Restart(); - List threads = new List(); - for (int i = 0; i < 20; i++) + // Cach size + PSqLite.ExecuteQuery("PRAGMA cache_size=10000;"); + + watch.Stop(); + Console.WriteLine($"set cach size to 10000: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + watch.Restart(); + + //List threads = new List(); + //for (int i = 0; i < 20; i++) + //{ + // threads.Add(new Thread(() => InsertRandomRows($"t{i + 1}", 1000))); + //} + + //foreach (Thread thread in threads) + //{ + // thread.Start(); + //} + + Random rnd = new Random(); + for (int i = 0; i < 2000; i++) { - threads.Add(new Thread(() => InsertRandomRows($"t{i + 1}", 1000))); + string guid = Guid.NewGuid().ToString(); + int age = rnd.Next(1, 100); + string name = $"Robo#{i + 1}"; + string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + PSqLite.ExecuteQuery(insertQry); } - foreach (Thread thread in threads) + watch.Stop(); + Console.WriteLine($"insert 2000 row case1: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + watch.Restart(); + + rnd = new Random(); + using (SQLiteConnection conn = PSqLite.Connection) { - thread.Start(); + conn.Open(); + for (int i = 0; i < 2000; i++) + { + string guid = Guid.NewGuid().ToString(); + int age = rnd.Next(1, 100); + string name = $"Robo#{i + 1}"; + string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + PSqLite.ExecuteQuery(conn, insertQry); + } + + conn.Close(); } - //Random rnd = new Random(); - //for (int i = 0; i < 2000; i++) - //{ - // string guid = Guid.NewGuid().ToString(); - // int age = rnd.Next(1, 100); - // string name = $"Robo#{i + 1}"; - // string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; - // PSqLite.ExecuteQuery(insertQry); - //} + watch.Stop(); + Console.WriteLine($"insert 2000 row case2: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + watch.Restart(); - //watch.Stop(); - //Console.WriteLine($"insert 2000 row case1: {watch.ElapsedMilliseconds}ms"); - //Console.WriteLine(); - //watch.Restart(); + rnd = new Random(); + string[] queries = new string[2000]; + for (int i = 0; i < 2000; i++) + { + string guid = Guid.NewGuid().ToString(); + int age = rnd.Next(1, 100); + string name = $"Robo#{i + 1}"; + queries[i] = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + } - //rnd = new Random(); - //using (SQLiteConnection conn = PSqLite.Connection) - //{ - // conn.Open(); - // for (int i = 0; i < 2000; i++) - // { - // string guid = Guid.NewGuid().ToString(); - // int age = rnd.Next(1, 100); - // string name = $"Robo#{i + 1}"; - // string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; - // PSqLite.ExecuteQuery(conn, insertQry); - // } - - // conn.Close(); - //} + PSqLite.ExecuteQuery(queries); - //watch.Stop(); - //Console.WriteLine($"insert 2000 row case2: {watch.ElapsedMilliseconds}ms"); - //Console.WriteLine(); - //watch.Restart(); + watch.Stop(); + Console.WriteLine($"insert 2000 row case3: {watch.ElapsedMilliseconds}ms"); + Console.WriteLine(); + watch.Restart(); //DataSet select = PSqLite.ExecuteSelectQuery("SELECT * FROM member"); //DataRowCollection rows = select.Tables[0].Rows; @@ -146,15 +171,18 @@ namespace SQLite_Console using (SQLiteConnection conn = PSqLite.Connection) { conn.Open(); + PSqLite.ExecuteQuery("BEGIN TRANSACTION;"); + for (int i = 0; i < count; i++) { string guid = Guid.NewGuid().ToString(); int age = rnd.Next(1, 100); string name = $"{identifier} Robo#{i + 1}"; - string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')"; + string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}');"; PSqLite.ExecuteQuery(conn, insertQry); } + PSqLite.ExecuteQuery("BEGIN TRANSACTION;"); conn.Close(); }