English 中文(简体)
事务处理速度与查询速度
原标题:Transaction speed vs. Query speed

一个事务有多个查询,或者每个查询都有一个单独的事务,哪个更快?

一个事务包含多个查询:

 db.transaction(function (tx){
      tx.executeSql( INSERT INTO TEST VALUES ("Halo1" ,"Halo2") , [], win, fail);        
      tx.executeSql( INSERT INTO TEST VALUES ("Halo3" ,"Halo4") , [], win, fail);
      tx.executeSql( INSERT INTO TEST VALUES ("Halo5" ,"Halo6") , [], win, fail);
      .
      .
      .
      .
   });

每个查询一个事务:

    db.transaction(function(tx){
        tx.executeSql( INSERT INTO TEST VALUES ("Halo1" ,"Halo2") , [], win, fail);

    });

    db.transaction(function(tx){
        tx.executeSql( INSERT INTO TEST VALUES ("Halo3" ,"Halo4") , [], win, fail);

    });
    and so on.....

我需要这些信息,因为我必须在一个表中插入许多记录。

最佳回答

这是一种权衡。几乎可以肯定的是,用大量语句处理一个事务会更快,因为你可以避免将这些事务提交到最后的开销,但是衡量标准,不要猜测

另一方面,您不希望在单个事务中投入太多,因为DBMS必须维护有关事务的控制信息(回滚或前滚日志等)。

我通常会选择半途而废的立场。在函数中执行插入操作,该函数自动提交每个N插入操作。类似伪代码的东西:

counter = 50
start new transaction                  # always start a transaction
for each insert_cmd in insert_list:
    call do_it (insert_cmd)
commit current transaction             # and always end it

def do_it (insert_cmd):
    if counter < 1:
        commit current transaction
        start new transaction
        counter = 50
    execute insert_cmd
    counter = counter - 1

只要确保你没有规避创建交易的全部原因。它们是ACID属性(原子性)中的A。如果需要将插入作为单个事务来正确维护数据,则可以这样做

问题回答

一个带有多个插入的事务总是会更快——快得多!也就是说,如果某个东西失败了,你将失去以前的所有插入。。。

Fastest way to determine speed of partial sourcecode is to make a loop of about 5.000 iterations around the specific part (maybe more, depends on how fast the code is executed) and measure the time difference between a timestamp before the loop started and a timestamp thereafter. Works in every language that supports any time measuring. Hope this will help.

使用单个事务会更快,但这并不意味着它使用更少的资源。

在第一次插入时,事务将锁定表的一部分或整个表。任何试图使用该表的其他查询都将排队,直到事务结束。您不仅应该考虑插入所需的时间,还应该考虑其他查询无法运行时所浪费的时间。

如果并发连接很少,这通常不是什么大问题。如果数据库负载很大,则应考虑使用较小的事务。通过将表锁定较短的时间,可以避免排队查询。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签