in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

High CPU time!

Last post 04-28-2008 10:47 AM by Robert Simpson. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 04-24-2008 8:16 AM

    • mce
    • Top 500 Contributor
    • Joined on 04-18-2008
    • Posts 4

    High CPU time!

     

    Hi,

     I have testing sqlite with a table of 300 hundreds records only, at the moment.

    All the data in the records are renewed at about 50ms interval.

    Reading and updating the database, both at approximately 50ms interval.

    I am getting about 40% CPU time. Is this normal for sqlite given the scenario above? This high CPU time doesn;t seem to be acceptable to ne at the moment. How to reduce it?

     

    //this is the updating part 

    public static void WorkerThreadMethod()

    {

     

    SQLiteConnection MyConnection = new SQLiteConnection("Data Source=D:\\SQLite\\Testbed\\test.s3db");

    MyConnection.Open();

    DbCommand MyCmd;

    MyCmd = MyConnection.CreateCommand();

    Random rand = new Random();

    while (true)

    {

    try

    {

     

     

     using (DbDataReader reader = MyCmd .ExecuteReader())
        {
            while (reader.Read())
            {
                string str =  reader.GetString(2);

                  int x = rand.Next(100);

                   MyCmd.CommandText = String.Format("UPDATE Table1 SET Reading = {0:d} where Type = {1}", x, str);

                  MyCmd.ExecuteScalar();

                  Thread.Sleep(100);


            }
        }

    }

    catch(Exception ex)

    {

    string excep = ex.Message;

    }

    }

    }

  • 04-24-2008 8:25 AM In reply to

    • petrv
    • Top 25 Contributor
    • Joined on 04-16-2008
    • Posts 24

    Re: High CPU time!

    From my little experience, I would make all the updates in one transaction. Once I used this approach, speed of multiple inserts raised several times.

  • 04-24-2008 11:12 AM In reply to

    Re: High CPU time!

    Exactly.  SQLite's ACID compliance is biting you here.  Every time you issue a singular update or insert outside a transaction, an implicit transaction has to be constructed and destructed around your statement.  Furthermore, SQLite must make sure the HD has completely flushed the data to disk before finishing the transaction.

    Bulk up your updates/inserts in a transaction as best you can to increase performance.

     

  • 04-28-2008 6:02 AM In reply to

    Re: High CPU time!

    An other thing to do to raise performance would be to use a parameterized query.  SQLite is having to recompile your sql statement for every update.

  • 04-28-2008 7:24 AM In reply to

    • mce
    • Top 500 Contributor
    • Joined on 04-18-2008
    • Posts 4

    Re: High CPU time!

    I am still facing the same issue of high CPU, the same codes, but under different scenario this time. I would appreciate if anyone can also help to do something similar to see if they are getting the same issue.

    The following loop is running at 30ms interval and my CPU usage for the process stands at 1 to 2%.

    However, when certain application is also running, such as Windows media player, the CPU usage for my process shoot up to a constant 10+%.  What has happened? Even the windows media doesn't play anything, it stands at such level until i close the media player.

    I use the same code and test on a laptop, but at slower cpu speed @ 1.7GH, AMD turion64, i am getting CPU usage of 10++% even without windows media player running. Does a slower CPU make the usage higher for my process?

    g_sqltransaction = g_sqlConn.BeginTransaction();

    while (y != 0)

    {

    int x = m_random.Next(100);

    g_sqlCmd.CommandText = String.Format("UPDATE MyTable SET Reading = {0:d} WHERE ID = '{1:d}'", x, y--);

    //g_sqlCmd.ExecuteScalar();

    g_sqlCmd.ExecuteNonQuery();

    }

    g_sqltransaction.Commit();

    }

  • 04-28-2008 7:38 AM In reply to

    Re: High CPU time!

    1.  As I said before, SQLite has to create an implicit transaction, perform your update, wait for the harddrive platters to fully commit the data via flushing the disk buffers, and tear down the transaction every 30ms.  This is a lot of work.  As a matter of fact, in 30ms using a single transaction, SQLite could probably write 10,000 updates to the same database.  Building the transaction and flushing the disk buffers consumes the vast majority of the effort.

    2.  How about revisiting this 30ms update thing?  Do you really have to update that much?  Can't you spin off a separate thread and build the updates up into a cache and when the cache exceeds a certain threshold, flush the cache to the database via separate thread?  Doing this in a single transaction would massively increase performance.

    3.  Use parameterized queries.  This won't solve the problem alone, but it will help.

     

  • 04-28-2008 8:28 AM In reply to

    • mce
    • Top 500 Contributor
    • Joined on 04-18-2008
    • Posts 4

    Re: High CPU time!

    I need to have the database updated every approx 30ms, as some other process is also reading this database at roughly 30 to 50ms interval in order to have the updated readings as real time as possible.

    Since this involves across processes access of DB, and every 30ms, i am getting new readings for all my database records; Building up the updates in cache before flushing the data to the DB  would mean i need to skip a few rounds of updates...

    I have also tried parameterized queries..

    Putting my DB on RAM disk, will it help? What other methods i can use to get around the massive time spent on building the transaction and flushing buffer?

  • 04-28-2008 10:47 AM In reply to

    Re: High CPU time!

    It sounds like you need to post some more information about your architecture then.  Anytime I deal in realtime communications like this, I tend not to use a file-based communications method.

     

Page 1 of 1 (8 items)
Powered by Community Server (Commercial Edition), by Telligent Systems