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


使用sqlite3_bind_double 插入100万行数据

@H_404_1082@
  • #include<iostream>
  • #include<iostream>
  • #include"sqlite3.h"
  • #include<string.h>
  • #include<stdio.h>
  • #include<sys/time.h>
  • #include<boost/lexical_cast.hpp>
  • usingnamespacestd;
  • usingnamespaceboost;
  • intfirst_row;
  • sqlite3*database;
  • //callbackfunction;
  • intselect_callback(void*p_data,intnum_fields,char**p_fields,char**p_col_names)
  • {
  • inti;
  • int*nof_records=(int*)p_data;
  • (*nof_records)++;
  • //first_rowwasdefinedin<select_stmt>function;
  • //iffirst_row==1,printthefirstrow
  • //andthensetfirst_row=0toavoidthesubsequentexecutionforthefollowingrows.
  • if(first_row==1)
  • {
  • first_row=0;
  • for(i=0;i<num_fields;i++)
  • {
  • //printf("%20s",p_col_names[i]);
  • }
  • printf("\n");
  • for(i=0;i<num_fields*10;i++)
  • {
  • //printf("=");
  • }
  • //printf("\n");
  • }
  • for(i=0;i<num_fields;i++)
  • {if(p_fields[i])
  • {
  • //printf("%20s",p_fields[i]);
  • }
  • else
  • {
  • //printf("%20s","");
  • }
  • }
  • //printf("\n");
  • return0;
  • }
  • //Withcallbackfunction;
  • voidselect_stmt(constchar*stmt)
  • {char*errmsg;
  • intret;
  • intnrecs=0;
  • first_row=1;
  • ret=sqlite3_exec(database,&errmsg);
  • if(ret!=sqlITE_OK)
  • {printf("Errorinselectstatement%s[%s].\n",errmsg);
  • }
  • else
  • {printf("\n%drecordsreturned.\n",nrecs);
  • }
  • }
  • //timecaculation
  • longtimecacul(){
  • structtimevaltv;
  • structtimezonetz;
  • gettimeofday(&tv,&tz);
  • return(tv.tv_sec*1000+tv.tv_usec/1000);
  • }
  • intmain()
  • {longstarttime,resulttime;
  • char*errmsg;
  • sqlite3_open("./Database.db",&database);
  • sqlite3_stmt*stmt;
  • strings="createtablewujie(x,z)";
  • constchar*creatTable=s.c_str();
  • //cout<<"creatTable:"<<creatTable<<endl;
  • intresult=sqlite3_exec(database,
  • creatTable,//stmt
  • 0,
  • &errmsg
  • );
  • if(result!=sqlITE_OK)
  • {cout<<"\nCouldnotpreparestatement:creatTable:"<<result<<endl;
  • return1;
  • }
  • if(sqlite3_prepare
  • (database,
  • "insertintowujievalues(:x,:y,:z)",//stmt
  • -1,//Ifthanzero,thenstmtisreaduptothefirstnulterminator
  • &stmt,
  • 0//Pointertounusedportionofstmt
  • )
  • !=sqlITE_OK)
  • {printf("\nCouldnotpreparestatement.");
  • return1;
  • }
  • intindex1,index2,index3;
  • index1=sqlite3_bind_parameter_index(stmt,":x");
  • index2=sqlite3_bind_parameter_index(stmt,":y");
  • index3=sqlite3_bind_parameter_index(stmt,":z");
  • //cout<<index1<<endl;
  • //cout<<index2<<endl;
  • //cout<<index3<<endl;
  • printf("\nThestatementhas%dwildcards\n",sqlite3_bind_parameter_count(stmt));
  • starttime=timecacul();
  • sqlite3_exec(database,"BEGINTRANSACTION;",&errmsg);
  • doublex,z;
  • doubleyTimes=1.222222222;
  • intiNum;
  • for(iNum=1;iNum<=1000000;iNum++)
  • {x=1*iNum;
  • y=yTimes*iNum;
  • z=2*iNum;
  • if(sqlite3_bind_double(stmt,
  • index1,//Indexofwildcard
  • x
  • )
  • !=sqlITE_OK)
  • {printf("\nCouldnotbinddouble.\n");
  • return1;
  • }
  • if(sqlite3_bind_double(stmt,
  • index2,//Indexofwildcard
  • y
  • )
  • !=sqlITE_OK)
  • {printf("\nCouldnotbinddouble.\n");
  • return1;
  • }
  • if(sqlite3_bind_double(stmt,
  • index3,//Indexofwildcard
  • z
  • )
  • !=sqlITE_OK)
  • {printf("\nCouldnotbinddouble.\n");
  • return1;
  • }
  • if(sqlite3_step(stmt)!=sqlITE_DONE)
  • {printf("\nCouldnotstep(execute)stmt.\n");
  • return1;
  • }
  • sqlite3_reset(stmt);
  • }
  • sqlite3_exec(database,"COMMITTRANSACTION;",NULL);
  • endtime=timecacul();
  • resulttime=endtime-starttime;
  • printf("NOAUTOCOMMITINSERT:%dms.",resulttime);
  • ///////////////////////////////////////////////
  • starttime=timecacul();
  • charselectData[]="Select*fromwujie";
  • select_stmt(selectData);
  • sqlite3_close(database);
  • endtime=timecacul();
  • resulttime=endtime-starttime;
  • printf("NOAUTOCOMMITINSERT:%dms.",resulttime);
  • return0;
  • }
  • 猜你在找的Sqlite相关文章