对比sqlite3_exec 和sqlite3_bind 插入100万行数据的速度 with BEGIN TRANSACTION using C++ and SQLite

前端之家收集整理的这篇文章主要介绍了对比sqlite3_exec 和sqlite3_bind 插入100万行数据的速度 with BEGIN TRANSACTION using C++ and SQLite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用sqlite3_exec 插入100万行数据需要 27 s,而使用sqlite3_bind_double 插入100万行数据只需要3.7 s。

主要是因为采用sqlite3_exec(),相当于每插入一行数据同时用到sqlite3_prepare_v2(),sqlite3_step() 和sqlite3_finalize(),另外需要把double 强制转换成 string 然后再转换成 const char*,这也需要耗费时间;而如果采用sqlite3_bind_double来加入数据,只要用到sqlite3_prepare_v2(),然后不断地使用sqlite3_step() 和 sqlite3_reset();并且不需要数据类型的转换。

当然,BEGIN TRANSACTION 的功能居功至伟。如果把sqlite3_exec(database,"BEGIN TRANSACTION;",NULL,&errmsg); 和sqlite3_exec(database,"COMMIT TRANSACTION;",NULL); 这两行注释掉,那么上述两种方法将耗费大量的时间;需要几分钟吧?

关于不同插入方法对插入速度的影响,见http://www.sqlite.org/faq.html#q19 中的“(19) INSERT is really slow - I can only do few dozen INSERTs per second



下面是两种类型的代码


使用sqlite3_exec 插入100万行数据

  1. #include<iostream>@H_404_39@
  2. #include<iostream>@H_404_39@
  3. #include"sqlite3.h"@H_404_39@
  4. #include<string.h>@H_404_39@
  5. #include<stdio.h>@H_404_39@
  6. #include<sys/time.h>@H_404_39@
  7. #include<boost/lexical_cast.hpp>@H_404_39@
  8. @H_404_39@
  9. @H_404_39@
  10. usingnamespacestd;@H_404_39@
  11. usingnamespaceboost;@H_404_39@
  12. @H_404_39@
  13. intfirst_row;@H_404_39@
  14. sqlite3*database;@H_404_39@
  15. @H_404_39@
  16. //callbackfunction;@H_404_39@
  17. intselect_callback(void*p_data,intnum_fields,char**p_fields,char**p_col_names)@H_404_39@
  18. {@H_404_39@
  19. inti;@H_404_39@
  20. int*nof_records=(int*)p_data;@H_404_39@
  21. (*nof_records)++;@H_404_39@
  22. @H_404_39@
  23. //first_rowwasdefinedin<select_stmt>function;@H_404_39@
  24. //iffirst_row==1,printthefirstrow@H_404_39@
  25. //andthensetfirst_row=0toavoidthesubsequentexecutionforthefollowingrows.@H_404_39@
  26. if(first_row==1)@H_404_39@
  27. {@H_404_39@
  28. first_row=0;@H_404_39@
  29. for(i=0;i<num_fields;i++)@H_404_39@
  30. {@H_404_39@
  31. //printf("%20s",p_col_names[i]);@H_404_39@
  32. }@H_404_39@
  33. //printf("\n");@H_404_39@
  34. for(i=0;i<num_fields*10;i++)@H_404_39@
  35. {@H_404_39@
  36. //printf("=");@H_404_39@
  37. }@H_404_39@
  38. //printf("\n");@H_404_39@
  39. }@H_404_39@
  40. @H_404_39@
  41. for(i=0;i<num_fields;i++)@H_404_39@
  42. {if(p_fields[i])@H_404_39@
  43. {@H_404_39@
  44. //printf("%20s",p_fields[i]);@H_404_39@
  45. }@H_404_39@
  46. else@H_404_39@
  47. {@H_404_39@
  48. //printf("%20s","");@H_404_39@
  49. }@H_404_39@
  50. }@H_404_39@
  51. @H_404_39@
  52. //printf("\n");@H_404_39@
  53. return0;@H_404_39@
  54. }@H_404_39@
  55. @H_404_39@
  56. @H_404_39@
  57. //Withcallbackfunction;@H_404_39@
  58. voidselect_stmt(constchar*stmt)@H_404_39@
  59. {char*errmsg;@H_404_39@
  60. intret;@H_404_39@
  61. intnrecs=0;@H_404_39@
  62. first_row=1;@H_404_39@
  63. @H_404_39@
  64. ret=sqlite3_exec(database,stmt,select_callback,&nrecs,&errmsg);@H_404_39@
  65. @H_404_39@
  66. if(ret!=sqlITE_OK)@H_404_39@
  67. {printf("Errorinselectstatement%s[%s].\n",errmsg);@H_404_39@
  68. }@H_404_39@
  69. else@H_404_39@
  70. {printf("\n%drecordsreturned.\n",nrecs);@H_404_39@
  71. }@H_404_39@
  72. }@H_404_39@
  73. @H_404_39@
  74. @H_404_39@
  75. //timecaculation@H_404_39@
  76. longtimecacul(){@H_404_39@
  77. structtimevaltv;@H_404_39@
  78. structtimezonetz;@H_404_39@
  79. gettimeofday(&tv,&tz);@H_404_39@
  80. return(tv.tv_sec*1000+tv.tv_usec/1000);@H_404_39@
  81. }@H_404_39@
  82. @H_404_39@
  83. intmain()@H_404_39@
  84. {longstarttime,endtime,resulttime;@H_404_39@
  85. @H_404_39@
  86. @H_404_39@
  87. @H_404_39@
  88. @H_404_39@
  89. char*errmsg;@H_404_39@
  90. sqlite3_open("./Database.db",&database);@H_404_39@
  91. @H_404_39@
  92. //sqlite3_exec(database,"PRAGMAsynchronous=OFF",&errmsg);@H_404_39@
  93. @H_404_39@
  94. @H_404_39@
  95. sqlite3_stmt*stmt;@H_404_39@
  96. strings="createtablewujie(xdecimal(5,2),ydecimal(5,zdecimal(5,2))";@H_404_39@
  97. constchar*creatTable=s.c_str();@H_404_39@
  98. cout<<"creatTable:"<<creatTable<<endl;@H_404_39@
  99. @H_404_39@
  100. //charcreatTable[]="createtablewujie(a,b,c)";@H_404_39@
  101. @H_404_39@
  102. @H_404_39@
  103. intresult=sqlite3_exec(database,@H_404_39@
  104. creatTable,//stmt@H_404_39@
  105. 0,@H_404_39@
  106. 0,@H_404_39@
  107. &errmsg@H_404_39@
  108. );@H_404_39@
  109. if(result!=sqlITE_OK)@H_404_39@
  110. {cout<<"\nCouldnotpreparestatement:creatTable:"<<result<<endl;@H_404_39@
  111. return1;@H_404_39@
  112. }@H_404_39@
  113. @H_404_39@
  114. @H_404_39@
  115. ////////BEGINTRANSACTION@H_404_39@
  116. starttime=timecacul();@H_404_39@
  117. sqlite3_exec(database,"BEGINTRANSACTION;",&errmsg);@H_404_39@
  118. @H_404_39@
  119. @H_404_39@
  120. stringinsertDataStr;@H_404_39@
  121. doublex,y,z;@H_404_39@
  122. doubleyTimes=1.222222222;@H_404_39@
  123. intiNum;@H_404_39@
  124. for(iNum=1;iNum<=1000000;iNum++)@H_404_39@
  125. {x=1*iNum;@H_404_39@
  126. y=yTimes*iNum;@H_404_39@
  127. z=2*iNum;@H_404_39@
  128. insertDataStr="insertintowujieVALUES("@H_404_39@
  129. +lexical_cast<string>(x)+","@H_404_39@
  130. +lexical_cast<string>(y)+","@H_404_39@
  131. +lexical_cast<string>(z)+")";@H_404_39@
  132. //cout<<"insertDataStr:"<<insertDataStr<<endl;@H_404_39@
  133. constchar*insertDataChar=insertDataStr.c_str();@H_404_39@
  134. @H_404_39@
  135. result=sqlite3_exec@H_404_39@
  136. (database,@H_404_39@
  137. insertDataChar,//stmt@H_404_39@
  138. 0,@H_404_39@
  139. 0,@H_404_39@
  140. &errmsg@H_404_39@
  141. );@H_404_39@
  142. @H_404_39@
  143. if(result!=sqlITE_OK)@H_404_39@
  144. {cout<<"\nCouldnotpreparestatement:inserData:"<<result<<endl;@H_404_39@
  145. return1;@H_404_39@
  146. }@H_404_39@
  147. }@H_404_39@
  148. @H_404_39@
  149. sqlite3_exec(database,"COMMITTRANSACTION;",NULL);@H_404_39@
  150. endtime=timecacul();@H_404_39@
  151. resulttime=endtime-starttime;@H_404_39@
  152. @H_404_39@
  153. printf("NOAUTOCOMMITINSERT:%dms.",resulttime);@H_404_39@
  154. cout<<endl;@H_404_39@
  155. @H_404_39@
  156. @H_404_39@
  157. charselectData[]="Selectx,zfromwujie";@H_404_39@
  158. starttime=timecacul();@H_404_39@
  159. select_stmt(selectData);@H_404_39@
  160. endtime=timecacul();@H_404_39@
  161. resulttime=endtime-starttime;@H_404_39@
  162. printf("Selectsqltime:%dms.",resulttime);@H_404_39@
  163. @H_404_39@
  164. sqlite3_close(database);@H_404_39@
  165. @H_404_39@
  166. return0;@H_404_39@
  167. }@H_404_39@


使用sqlite3_bind_double 插入100万行数据

  1. #include<iostream>@H_404_39@
  2. #include<iostream>@H_404_39@
  3. #include"sqlite3.h"@H_404_39@
  4. #include<string.h>@H_404_39@
  5. #include<stdio.h>@H_404_39@
  6. #include<sys/time.h>@H_404_39@
  7. #include<boost/lexical_cast.hpp>@H_404_39@
  8. @H_404_39@
  9. @H_404_39@
  10. usingnamespacestd;@H_404_39@
  11. usingnamespaceboost;@H_404_39@
  12. @H_404_39@
  13. intfirst_row;@H_404_39@
  14. sqlite3*database;@H_404_39@
  15. @H_404_39@
  16. //callbackfunction;@H_404_39@
  17. intselect_callback(void*p_data,intnum_fields,char**p_fields,char**p_col_names)@H_404_39@
  18. {@H_404_39@
  19. inti;@H_404_39@
  20. int*nof_records=(int*)p_data;@H_404_39@
  21. (*nof_records)++;@H_404_39@
  22. @H_404_39@
  23. //first_rowwasdefinedin<select_stmt>function;@H_404_39@
  24. //iffirst_row==1,printthefirstrow@H_404_39@
  25. //andthensetfirst_row=0toavoidthesubsequentexecutionforthefollowingrows.@H_404_39@
  26. if(first_row==1)@H_404_39@
  27. {@H_404_39@
  28. first_row=0;@H_404_39@
  29. for(i=0;i<num_fields;i++)@H_404_39@
  30. {@H_404_39@
  31. //printf("%20s",p_col_names[i]);@H_404_39@
  32. }@H_404_39@
  33. printf("\n");@H_404_39@
  34. for(i=0;i<num_fields*10;i++)@H_404_39@
  35. {@H_404_39@
  36. //printf("=");@H_404_39@
  37. }@H_404_39@
  38. //printf("\n");@H_404_39@
  39. }@H_404_39@
  40. @H_404_39@
  41. for(i=0;i<num_fields;i++)@H_404_39@
  42. {if(p_fields[i])@H_404_39@
  43. {@H_404_39@
  44. //printf("%20s",p_fields[i]);@H_404_39@
  45. }@H_404_39@
  46. else@H_404_39@
  47. {@H_404_39@
  48. //printf("%20s","");@H_404_39@
  49. }@H_404_39@
  50. }@H_404_39@
  51. @H_404_39@
  52. //printf("\n");@H_404_39@
  53. return0;@H_404_39@
  54. }@H_404_39@
  55. @H_404_39@
  56. //Withcallbackfunction;@H_404_39@
  57. voidselect_stmt(constchar*stmt)@H_404_39@
  58. {char*errmsg;@H_404_39@
  59. intret;@H_404_39@
  60. intnrecs=0;@H_404_39@
  61. first_row=1;@H_404_39@
  62. ret=sqlite3_exec(database,&errmsg);@H_404_39@
  63. @H_404_39@
  64. if(ret!=sqlITE_OK)@H_404_39@
  65. {printf("Errorinselectstatement%s[%s].\n",errmsg);@H_404_39@
  66. }@H_404_39@
  67. else@H_404_39@
  68. {printf("\n%drecordsreturned.\n",nrecs);@H_404_39@
  69. }@H_404_39@
  70. }@H_404_39@
  71. @H_404_39@
  72. @H_404_39@
  73. //timecaculation@H_404_39@
  74. longtimecacul(){@H_404_39@
  75. structtimevaltv;@H_404_39@
  76. structtimezonetz;@H_404_39@
  77. gettimeofday(&tv,&tz);@H_404_39@
  78. return(tv.tv_sec*1000+tv.tv_usec/1000);@H_404_39@
  79. }@H_404_39@
  80. @H_404_39@
  81. @H_404_39@
  82. intmain()@H_404_39@
  83. {longstarttime,resulttime;@H_404_39@
  84. @H_404_39@
  85. char*errmsg;@H_404_39@
  86. sqlite3_open("./Database.db",&database);@H_404_39@
  87. @H_404_39@
  88. sqlite3_stmt*stmt;@H_404_39@
  89. @H_404_39@
  90. strings="createtablewujie(x,z)";@H_404_39@
  91. constchar*creatTable=s.c_str();@H_404_39@
  92. //cout<<"creatTable:"<<creatTable<<endl;@H_404_39@
  93. @H_404_39@
  94. intresult=sqlite3_exec(database,@H_404_39@
  95. creatTable,//stmt@H_404_39@
  96. 0,@H_404_39@
  97. &errmsg@H_404_39@
  98. );@H_404_39@
  99. if(result!=sqlITE_OK)@H_404_39@
  100. {cout<<"\nCouldnotpreparestatement:creatTable:"<<result<<endl;@H_404_39@
  101. return1;@H_404_39@
  102. }@H_404_39@
  103. @H_404_39@
  104. @H_404_39@
  105. if(sqlite3_prepare@H_404_39@
  106. (database,@H_404_39@
  107. "insertintowujievalues(:x,:y,:z)",//stmt@H_404_39@
  108. -1,//Ifthanzero,thenstmtisreaduptothefirstnulterminator@H_404_39@
  109. &stmt,@H_404_39@
  110. 0//Pointertounusedportionofstmt@H_404_39@
  111. )@H_404_39@
  112. !=sqlITE_OK)@H_404_39@
  113. {printf("\nCouldnotpreparestatement.");@H_404_39@
  114. return1;@H_404_39@
  115. }@H_404_39@
  116. @H_404_39@
  117. intindex1,index2,index3;@H_404_39@
  118. index1=sqlite3_bind_parameter_index(stmt,":x");@H_404_39@
  119. index2=sqlite3_bind_parameter_index(stmt,":y");@H_404_39@
  120. index3=sqlite3_bind_parameter_index(stmt,":z");@H_404_39@
  121. @H_404_39@
  122. //cout<<index1<<endl;@H_404_39@
  123. //cout<<index2<<endl;@H_404_39@
  124. //cout<<index3<<endl;@H_404_39@
  125. @H_404_39@
  126. printf("\nThestatementhas%dwildcards\n",sqlite3_bind_parameter_count(stmt));@H_404_39@
  127. @H_404_39@
  128. @H_404_39@
  129. starttime=timecacul();@H_404_39@
  130. sqlite3_exec(database,"BEGINTRANSACTION;",&errmsg);@H_404_39@
  131. doublex,z;@H_404_39@
  132. doubleyTimes=1.222222222;@H_404_39@
  133. intiNum;@H_404_39@
  134. for(iNum=1;iNum<=1000000;iNum++)@H_404_39@
  135. {x=1*iNum;@H_404_39@
  136. y=yTimes*iNum;@H_404_39@
  137. z=2*iNum;@H_404_39@
  138. @H_404_39@
  139. if(sqlite3_bind_double(stmt,@H_404_39@
  140. index1,//Indexofwildcard@H_404_39@
  141. x@H_404_39@
  142. )@H_404_39@
  143. !=sqlITE_OK)@H_404_39@
  144. {printf("\nCouldnotbinddouble.\n");@H_404_39@
  145. return1;@H_404_39@
  146. }@H_404_39@
  147. @H_404_39@
  148. if(sqlite3_bind_double(stmt,@H_404_39@
  149. index2,//Indexofwildcard@H_404_39@
  150. y@H_404_39@
  151. )@H_404_39@
  152. !=sqlITE_OK)@H_404_39@
  153. {printf("\nCouldnotbinddouble.\n");@H_404_39@
  154. return1;@H_404_39@
  155. }@H_404_39@
  156. @H_404_39@
  157. if(sqlite3_bind_double(stmt,@H_404_39@
  158. index3,//Indexofwildcard@H_404_39@
  159. z@H_404_39@
  160. )@H_404_39@
  161. !=sqlITE_OK)@H_404_39@
  162. {printf("\nCouldnotbinddouble.\n");@H_404_39@
  163. return1;@H_404_39@
  164. }@H_404_39@
  165. @H_404_39@
  166. if(sqlite3_step(stmt)!=sqlITE_DONE)@H_404_39@
  167. {printf("\nCouldnotstep(execute)stmt.\n");@H_404_39@
  168. return1;@H_404_39@
  169. }@H_404_39@
  170. sqlite3_reset(stmt);@H_404_39@
  171. @H_404_39@
  172. }@H_404_39@
  173. @H_404_39@
  174. sqlite3_exec(database,"COMMITTRANSACTION;",NULL);@H_404_39@
  175. endtime=timecacul();@H_404_39@
  176. resulttime=endtime-starttime;@H_404_39@
  177. printf("NOAUTOCOMMITINSERT:%dms.",resulttime);@H_404_39@
  178. @H_404_39@
  179. ///////////////////////////////////////////////@H_404_39@
  180. starttime=timecacul();@H_404_39@
  181. @H_404_39@
  182. charselectData[]="Select*fromwujie";@H_404_39@
  183. select_stmt(selectData);@H_404_39@
  184. sqlite3_close(database);@H_404_39@
  185. @H_404_39@
  186. endtime=timecacul();@H_404_39@
  187. resulttime=endtime-starttime;@H_404_39@
  188. printf("NOAUTOCOMMITINSERT:%dms.",resulttime);@H_404_39@
  189. @H_404_39@
  190. return0;@H_404_39@
  191. } @H_404_39@

猜你在找的Sqlite相关文章