Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATE faster in SQLite + BEGIN TRANSACTION

This one is related to spatilite also (not only SQLite)

I have a file database (xyz.db) which I am using by SQLiteconnection (SQLiteconnection is extends to spatialite).

I have so many records needs to update into database.

                for (int y = 0; y < castarraylist.Count; y++)
                {
                    string s = Convert.ToString(castarraylist[y]);

                    string[] h = s.Split(':');

                    SQLiteCommand sqlqctSQL4 = new SQLiteCommand("UPDATE temp2 SET GEOM = " + h[0] + "WHERE " + dtsqlquery2.Columns[0] + "=" + h[1] + "", con);
                    sqlqctSQL4.ExecuteNonQuery();

                    x = x + 1;
                }

At above logic castarraylist is Arraylist which contains value which need to process into database.

When I checked above code updating around 400 records in 1 minute.

Is there any way by which I can able to improve performance ?

NOTE :: (File database is not thread-safe)

2. BEGIN TRANSACTION

Let's suppose I like to run two (or millions) update statement with single transaction in Spatialite.. is it possible ?

I read online and prepare below statement for me (but not get success)

BEGIN TRANSACTION;
UPDATE builtuparea_luxbel SET ADMIN_LEVEL = 6 where PK_UID = 2;
UPDATE builtuparea_luxbel SET ADMIN_LEVEL = 6 where PK_UID = 3;
COMMIT TRANSACTION; 

Above statement not updating records in my database. is SQLite not support BEGIN TRANSACTION ? is there anything which I missing ?

And If I need to run individual statement then it's taking too much time to update as said above...

like image 392
Hardik Avatar asked Jun 27 '14 05:06

Hardik


People also ask

How can I make SQLite database faster?

Instead of writing changes directly to the db file, write to a write-ahead-log instead and regularily commit the changes. This allows multiple concurrent readers even during an open write transaction, and can significantly improve performance.

What is begin transaction in SQLite?

Transactions can be started manually using the BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command. But a transaction will also ROLLBACK if the database is closed or if an error occurs and the ROLLBACK conflict resolution algorithm is specified.

What is faster than SQLite?

With Actian Zen, developers and product managers get all the advantages of SQLite but in a powerful, secure, and scalable engine that can run serverless or as a client-server. Actian Zen is orders of magnitude faster than SQLite.

Is SQLite good for caching?

SQLite is capable of creating databases that are held entirely in memory. This is extremely useful for creating small, temporary databases that require no permanent storage. In-memory databases are often used to cache results pulled from a more traditional RDBMS server.


2 Answers

SQLite support Transaction, you can try below code.

using (var cmd = new SQLiteCommand(conn))
using (var transaction = conn.BeginTransaction())
{
    for (int y = 0; y < castarraylist.Count; y++)
    {
        //Add your query here.
        cmd.CommandText = "INSERT INTO TABLE (Field1,Field2) VALUES ('A', 'B');";
        cmd.ExecuteNonQuery();
    }
    transaction.Commit();
}
like image 185
Suresh Avatar answered Oct 04 '22 11:10

Suresh


  • The primary goal of a database transaction to get everything done, or nothing if something fails inside;

  • Reusing the same SQLiteCommand object by changing its CommandText property and execute it again and again might be faster, but leads to a memory overhead: If you have an important amount of queries to perform, the best is to dispose the object after use and create a new one;

A common pattern for an ADO.NET transaction is:

using (var tra = cn.BeginTransaction())
{
    try
    { 
        foreach(var myQuery in myQueries)
        { 
            using (var cd = new SQLiteCommand(myQuery, cn, tra))
            {
                cd.ExecuteNonQuery();
            }
        }

        tra.Commit();
    }
    catch(Exception ex)
    {
        tra.Rollback();
        Console.Error.Writeline("I did nothing, because something wrong happened: {0}", ex);
        throw;
    }
}
like image 23
Larry Avatar answered Oct 04 '22 11:10

Larry