Skip to content

node_sqlite3 queries blocking the renderer process #2542

Closed

Description

@bengotow

I'm using Electron 0.30.4 on Mac OS X 10.10.3. The project I'm working on uses node-sqlite3 in the renderer process and makes queries for data to display.

The SQLite3 module uses uv_queue_work(uv_default_loop()... to run queries on the libuv thread pool. If I understand correctly, this means that the heavy lifting of performing the query shouldn't block the renderer process. However, this doesn't seem to be the case.

Using the content-tracing module, I generated profiles of our Electron app just before a particularly expensive query is executed. You can see in the profile that the renderer process is blocked in MessageLoop::RunTask for a very long time. This causes the entire UI to "stick" - things don't render, animate, etc. during that time. The Chromium Developer Tools profiler shows no activity in that window, and nothing is really visible in the content-tracing output.

I'm fairly sure that this long call to MessageLoop::RunTask corresponds to the execution of an expensive query in SQLite. To test, I kept the data and application exactly the same and added/removed the indexes from the table being queried.

With the indexes, the renderer process was blocked for 232msec.
Trace: https://dl.dropboxusercontent.com/u/4803975/ElectronQueryIndexed.org.chromium.Chromium.chRNEA

Without indexes, it was blocked for 800msec.
Trace: https://dl.dropboxusercontent.com/u/4803975/ElectronQuery.org.chromium.Chromium.xZlVkG

screen shot 2015-08-20 at 2 24 18 pm

There are a few other queries visible in the trace which weren't affected by adding/removing the index on this particular table.

This is a big issue for us—would love any insight!

Activity

  1. zcbenz commented on Aug 28, 2015

    @zcbenz
    Contributor

    Can you give an example that can show this problem? I tested with this:

    var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database(':memory:');
    
    console.time('sqlite3');
    db.serialize(function() {
      db.run("CREATE TABLE lorem (info TEXT)");
    
      var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
      for (var i = 0; i < 10; i++) {
          stmt.run("Ipsum " + i);
      }
      stmt.finalize();
    
      db.run("SELECT * FROM lorem", function(err) {
          console.log(err);
      });
    });
    
    db.close();
    console.timeEnd('sqlite3');

    The result is only 2~3ms.

  2. zcbenz commented on Sep 17, 2015

    @zcbenz
    Contributor

    Closing until more information is provided.

  3. mikey0000 commented on Jul 6, 2017

    @mikey0000

    Try doing this one

    var sqlite3 = require('sqlite3').verbose();
      var db = new sqlite3.Database(':memory:');
    
      db.serialize(function() {
        db.run("CREATE TABLE test (id INT, test TEXT)");
    
        var stmt = db.prepare("INSERT INTO test (id, test) VALUES (?, ?)");
        for (var i = 0; i < 100000; i++) {
            stmt.run(Math.floor(Math.random() * 100000), "Ipsum " + i);
        }
        stmt.finalize();
        console.log('done');
        // db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {
        //     console.log(row.id + ": " + row.info);
        // });
      });
    
      db.close();
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions