<i id='N0O9V'><tr id='N0O9V'><dt id='N0O9V'><q id='N0O9V'><span id='N0O9V'><b id='N0O9V'><form id='N0O9V'><ins id='N0O9V'></ins><ul id='N0O9V'></ul><sub id='N0O9V'></sub></form><legend id='N0O9V'></legend><bdo id='N0O9V'><pre id='N0O9V'><center id='N0O9V'></center></pre></bdo></b><th id='N0O9V'></th></span></q></dt></tr></i><div id='N0O9V'><tfoot id='N0O9V'></tfoot><dl id='N0O9V'><fieldset id='N0O9V'></fieldset></dl></div>

      <small id='N0O9V'></small><noframes id='N0O9V'>

      <tfoot id='N0O9V'></tfoot>

      <legend id='N0O9V'><style id='N0O9V'><dir id='N0O9V'><q id='N0O9V'></q></dir></style></legend>
      • <bdo id='N0O9V'></bdo><ul id='N0O9V'></ul>

        在 SQLite 中,准备好的语句真的能提高性能吗?

        时间:2023-10-10

          <bdo id='lC4mK'></bdo><ul id='lC4mK'></ul>

              <tbody id='lC4mK'></tbody>
              <tfoot id='lC4mK'></tfoot>
              1. <legend id='lC4mK'><style id='lC4mK'><dir id='lC4mK'><q id='lC4mK'></q></dir></style></legend>
              2. <i id='lC4mK'><tr id='lC4mK'><dt id='lC4mK'><q id='lC4mK'><span id='lC4mK'><b id='lC4mK'><form id='lC4mK'><ins id='lC4mK'></ins><ul id='lC4mK'></ul><sub id='lC4mK'></sub></form><legend id='lC4mK'></legend><bdo id='lC4mK'><pre id='lC4mK'><center id='lC4mK'></center></pre></bdo></b><th id='lC4mK'></th></span></q></dt></tr></i><div id='lC4mK'><tfoot id='lC4mK'></tfoot><dl id='lC4mK'><fieldset id='lC4mK'></fieldset></dl></div>

              3. <small id='lC4mK'></small><noframes id='lC4mK'>

                  本文介绍了在 SQLite 中,准备好的语句真的能提高性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我听说使用 SQLite 准备好的语句应该可以提高性能.我写了一些代码来测试它,并没有发现使用它们的性能有任何差异.所以,我想也许我的代码不正确.如果您发现我的操作有任何错误,请告诉我...

                  I have heard that prepared statements with SQLite should improve performance. I wrote some code to test that, and did not see any difference in performance with using them. So, I thought maybe my code was incorrect. Please let me know if you see any errors in how I'm doing this...

                  [self testPrep:NO dbConn:dbConn];
                  [self testPrep:YES dbConn:dbConn];
                  
                  reuse=0
                  recs=2000
                  2009-11-09 10:39:18 -0800
                  processing...
                  2009-11-09 10:39:32 -0800
                  
                  reuse=1
                  recs=2000
                  2009-11-09 10:39:32 -0800
                  processing...
                  2009-11-09 10:39:46 -0800
                  
                  -(void)testPrep:(BOOL)reuse dbConn:(sqlite3*)dbConn{
                      int recs = 2000;
                      NSString *sql;
                      sqlite3_stmt *stmt;
                  
                      sql = @"DROP TABLE test";
                      sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
                  
                      sql = @"CREATE TABLE test (id INT,field1 INT, field2 INT,field3 INT,field4 INT,field5 INT,field6 INT,field7 INT,field8 INT,field9 INT,field10 INT)";
                      sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
                  
                      for(int i=0;i<recs;i++){
                          sql = @"INSERT INTO test (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10) VALUES (%d,1,2,3,4,5,6,7,8,9,10)";
                          sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
                      }
                  
                      sql = @"BEGIN";
                      sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
                  
                      if (reuse){
                          sql = @"select * from test where field1=?1 and field2=?2 and field3=?3 and field4=?4 and field5=?5 and field6=?6 and field6=?6 and field8=?8 and field9=?9 and field10=?10 and id=?11";
                          sqlite3_prepare_v2(dbConn, [sql UTF8String], -1, &stmt, NULL);
                      }
                  
                      NSLog(@"reuse=%d",reuse);
                      NSLog(@"recs=%d",recs);
                      NSDate *before = [NSDate date];
                      NSLog([before description]);
                      NSLog(@"processing...");
                      for(int i=0;i<recs;i++){
                          if (!reuse){
                              sql = @"select * from test where field1=?1 and field2=?2 and field3=?3 and field4=?4 and field5=?5 and field6=?6 and field6=?6 and field8=?8 and field9=?9 and field10=?10 and id=?11";
                              sqlite3_prepare_v2(dbConn, [sql UTF8String], -1, &stmt, NULL);
                          }
                          sqlite3_bind_int(stmt, 1, 1);
                          sqlite3_bind_int(stmt, 2, 2);
                          sqlite3_bind_int(stmt, 3, 3);
                          sqlite3_bind_int(stmt, 4, 4);
                          sqlite3_bind_int(stmt, 5, 5);
                          sqlite3_bind_int(stmt, 6, 6);
                          sqlite3_bind_int(stmt, 7, 7);
                          sqlite3_bind_int(stmt, 8, 8);
                          sqlite3_bind_int(stmt, 9, 9);
                          sqlite3_bind_int(stmt, 10, 10);
                          sqlite3_bind_int(stmt, 11, i);
                  
                          while(sqlite3_step(stmt) == SQLITE_ROW) {
                          }
                          sqlite3_reset(stmt);
                      }
                  
                      sql = @"BEGIN";
                      sqlite3_exec(dbConn, [sql UTF8String],NULL,NULL,NULL);
                  
                      NSDate *after = [NSDate date];
                      NSLog([after description]);
                  }
                  

                  推荐答案

                  准备好的语句通过缓存 执行来提高性能在查询优化器找到最佳计划后,为查询计划.

                  Prepared statements improve performance by caching the execution plan for a query after the query optimizer has found the best plan.

                  如果您使用的查询没有复杂的计划(例如没有连接的简单选择/插入),那么准备好的语句不会给您带来很大的改进,因为优化器会很快找到最佳计划.

                  If the query you're using doesn't have a complicated plan (such as simple selects/inserts with no joins), then prepared statements won't give you a big improvement since the optimizer will quickly find the best plan.

                  但是,如果您对具有几个连接并使用一些索引的查询运行相同的测试,您会看到性能差异,因为优化器不会在每次查询时都运行.

                  However, if you ran the same test with a query that had a few joins and used some indexes, you would see the performance difference since the optimizer wouldn't be run every time the query is.

                  这篇关于在 SQLite 中,准备好的语句真的能提高性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:regexp python 和 sqlite 的问题 下一篇:字符串与 Python + Sqlite 的相似度(Levenshtein 距离/编辑距离)

                  相关文章

                  <i id='gTDYl'><tr id='gTDYl'><dt id='gTDYl'><q id='gTDYl'><span id='gTDYl'><b id='gTDYl'><form id='gTDYl'><ins id='gTDYl'></ins><ul id='gTDYl'></ul><sub id='gTDYl'></sub></form><legend id='gTDYl'></legend><bdo id='gTDYl'><pre id='gTDYl'><center id='gTDYl'></center></pre></bdo></b><th id='gTDYl'></th></span></q></dt></tr></i><div id='gTDYl'><tfoot id='gTDYl'></tfoot><dl id='gTDYl'><fieldset id='gTDYl'></fieldset></dl></div>
                  1. <small id='gTDYl'></small><noframes id='gTDYl'>

                    <legend id='gTDYl'><style id='gTDYl'><dir id='gTDYl'><q id='gTDYl'></q></dir></style></legend>

                      • <bdo id='gTDYl'></bdo><ul id='gTDYl'></ul>
                    1. <tfoot id='gTDYl'></tfoot>