A friend tested SQLite today and then concluded that SQLite is too inefficient, and it took 2 minutes to insert 1,000 records in batches!
Below is the test code he sent me. I'm dizzy~~~~~~
using ;
using ;
using ;
// Create database file
("test1.db3");
("test1.db3");
DbProviderFactory factory = ;
using (DbConnection conn = ())
{
// Connect to the database
= "Data Source=test1.db3";
();
// Create a data table
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";
DbCommand cmd = ();
= conn;
= sql;
();
// Add parameters
(());
// Start timing
Stopwatch watch = new Stopwatch();
();
// Insert 1000 records in succession
for (int i = 0; i < 1000; i++)
{
= "insert into [test1] ([s]) values (?)";
[0].Value = ();
();
}
// Stop timing
();
();
}
Alas~~~~~ A common sense error, I will add a few lines of code (add the new code mark "// <-------------------------------------------------------------------------------------------------
using ;
using ;
using ;
// Create database file
("test1.db3");
("test1.db3");
DbProviderFactory factory = ;
using (DbConnection conn = ())
{
// Connect to the database
= "Data Source=test1.db3";
();
// Create a data table
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE)";
DbCommand cmd = ();
= conn;
= sql;
();
// Add parameters
(());
// Start timing
Stopwatch watch = new Stopwatch();
();
DbTransaction trans = (); // <-------------------
try
{
// Insert 1000 records in succession
for (int i = 0; i < 1000; i++)
{
= "insert into [test1] ([s]) values (?)";
[0].Value = ();
();
}
(); // <-------------------
}
catch
{
(); // <-------------------
throw; // <-------------------
}
// Stop timing
();
();
}
It takes 0.2 seconds to execute. Is this a little too big?
Why is there such a big gap if you simply enable a transaction? It's very simple. SQLite starts one transaction for each operation by default. Then the original code 1,000 inserts start at least 1,000 transactions. "Transaction on + SQL execution + transaction closing" naturally takes a lot of time, which is also the reason why the transaction is started later is displayed so fast. In fact, this is the basic common sense of database operations. Everyone should remember that bad code efficiency is not a little bit bad.