How to speed up the process when inserting 1000's of records into sqlite using HTML5

后端 未结 5 1953
[愿得一人]
[愿得一人] 2021-01-13 09:58

Iam new to developing HTML5 applications. In this I want to insert 1000\'s of records into sqlite database using HTML5. This process is very slow. How to use BEGIN/COMMIT be

相关标签:
5条回答
  • 2021-01-13 10:10

    You're starting 1000 transactions. Committing a transaction is slow because it is linked to hard drive speed. See this

    0 讨论(0)
  • 2021-01-13 10:20

    This issue is that you're using a separate transaction for each step, instead of reusing it, hence why it's running poorly.

    db.transaction IS a transaction, so no BEGIN/COMMIT needed.

    Try this:

    <!DOCTYPE HTML>
    <html>
    <head>
    <script type="text/javascript">
    var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
    var msg;
    db.transaction(function (tx) {
        for(var i=0;i<1000;i++)
        {
            txquer(tx, i,"test");
        }
    });
    db.transaction(function (tx) {
      tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
       var len = results.rows.length, i;
       msg = "<p>Found rows: " + len + "</p>";
       document.querySelector('#status').innerHTML +=  msg;  
     }, null);
    });
    function txquer(tx,i,test)
    { 
        tx.executeSql('INSERT INTO LOGS (id, log) VALUES (?, ?)',[i,test]);    
    } 
    </script>
    </head>
    <body>
    <div id="status" name="status">Status Message</div>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-13 10:20

    You already have the code to place your 1000 inserts inside a single transaction. For some reason it is commented out. Remove the comments and you are done!

    db.transaction(function(tx){tx.executeSql("BEGIN",[]);});
    for(var i=0;i<1000;i++)
    {
        txquer(i,"test");
    }
    db.transaction(function(tx){tx.executeSql("COMMIT",[]);});
    
    0 讨论(0)
  • 2021-01-13 10:27

    I have actually wrote a small open source javascript module called html5sql.js which solves this very problem. The good news is that html5sql.js is really fast. On the website http://html5sql.com I have a demo which creates a table and inserts 11000 records into that table, usually in less than a second or two. If anyone is still dealing with this issue I recommend checking html5sql.js out.

    0 讨论(0)
  • 2021-01-13 10:30

    I solved it , may be this works. Use 'insert select union all' instead of creating 1000 insert statements.But this can insert only 500 rows at a time. Here is the code which i worked on, do a test on google chrome i am sure it works.

    <!DOCTYPE HTML>
    <html>
    <head>
    <script type="text/javascript">
    var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
    var msg;
    var j=1;
    var i=1;
    var quer="";
    db.transaction(function(tx){tx.executeSql("CREATE TABLE IF NOT EXISTS LOGS (ID INTEGER PRIMARY KEY ASC, todo TEXT)",[]);});
    db.transaction(function(tx){tx.executeSql("delete from logs",[]);});
    txquer();
    showMsg();
    function txquer()
    {
      quer="insert into logs ";
     for(i=j;i<=j+498;i++)
     {
      quer+=" select "+i+",'test' union all";
      }
      quer+=" select "+i+",'test' ; ";
      j=i+1;
        db.transaction(
        function(tx){
          tx.executeSql(quer,[]);    
        }
        );
    
    }
    
    function showMsg(){
    db.transaction(function (tx) {
      tx.executeSql('SELECT count(*) todo FROM LOGS', [], function (tx, results) {
       var len = results.rows.item(0).todo;
       msg = "<p>Found rows: " + len + "</p>";
       document.querySelector('#status').innerHTML +=  msg;  
     }, null);
    });
    }
    

    Status Message

    0 讨论(0)
提交回复
热议问题