SQLite3开启事务和关闭事务模式下 性能测试对比

来源:IT165收集  发布日期:2015-04-14 20:55:16
最近学习了下SQLite数据库基本知识,想了解下这款小巧的数据库,性能到底怎样,于是写个性能测试程序,对 SQLite3 最新发布版(3.7.13)在Linux平台进行了测试。最后发现在开启事务模式和关闭事务模式(默认)下,性能测试结果相差近 1000 倍! 

在测试的过程中,得出如下一些结论:

1、对于批量数据操作,建议采用事务模式,批量提交操作。

2、在提交事务之前,若程序发生异常,则所有插入、更新、删除等操作,都不会成功。

3、在操作数据库时,程序发生异常而中断操作,不会对现有的数据库造成任何破坏(SQLite3 的可靠性还不错!)。

一、测试环境
操作系统类型:linux-suse11 2.6.34.10-0.6-desktop, x86-32
操作系统内核:2.6.34.10-0.6-desktop
编译器版本 : gcc (SUSE Linux) 4.5.0 20100604 [gcc-4_5-branch revision 160292]
数据库版本 : SQLite 3.7.13
CPU 配置 :AMD Athlon(tm) II X2 240 Processor, 2812.890 MHZ, 1024 KB cache size,dual core
内存配置 :1017812 kB

二、编译方法:
1、开启事务模式编译: gcc -Wall -lpthread -ldl testsqlite3.c sqlite3.c -o main -DTRANSACTION_ON;
2、关闭事务模式编译: gcc -Wall -lpthread -ldl testsqlite3.c sqlite3.c -o main;

 

三、测试结果:

测试结果单位:微秒(μs), 1秒=1000毫秒=1000000微秒

1、关闭事务模式(默认)

操作类型 Counts TotalTime(us) AverageTime(us)
插入 1000 31307617 31307.000000
查询 1000 944442 944.000000
更新 1000 32264043 32264.000000
删除 1000 30638604 30638.000000

2、开启事务模式

操作类型 Counts TotalTime(us) AverageTime(us)
插入 1000 23326 23.000000
查询 1000 935739 935.000000
更新 1000 39197 39.000000
删除 1000 24394 24.000000

四、源代码:

 

  1. /****************************************************************************** Copyright by Javacode007, All rights reserved!
  2. Filename : testsqlite3.c Author : Javacode007
  3. Date : 2012-8-11 Version : 1.0
  4. Description : SQLite3 基本功能性能测试 ******************************************************************************/
  5. #include "sqlite3.h"
  6. #include <stdlib.h> #include <stdio.h>
  7. #include <string.h> #include <sys/time.h>
  8. #include <time.h>
  9. #define PRINT_TRACE(fmt, args...) do { fprintf(stderr, fmt" File:%s, Line:%04d ",##args, __FILE__, __LINE__);
  10. }while(0)
  11.  
  12. #ifdef TRANSACTION_ON #define START_TRANSACTION() sqlite3_exec(db, "begin transaction;", NULL, NULL, NULL)
  13. #define END_TRANSACTION() sqlite3_exec(db, "commit transaction;", NULL, NULL, NULL) #else
  14. #define START_TRANSACTION() #define END_TRANSACTION()
  15. #endif
  16.  
  17. //性别枚举类型 typedef enum
  18. { UNKNOWN,
  19. MALE, FEMALE,
  20. }SEX_E;
  21. //SQL 操作枚举类型 typedef enum
  22. { INSERT,
  23. SELECT, UPDATE,
  24. DELETE }SQL_OPTYPE;
  25. typedef struct stEmployee
  26. { unsigned int id;
  27. unsigned int age; SEX_E sex;
  28. char registertime[26]; char cellphone[12];
  29. char email[128]; char resume[512];
  30. float salary; }Employee_S;
  31. //全局雇员ID
  32. static unsigned int g_employeeid;
  33. long diff_timeval(struct timeval tv1, struct timeval tv2) {
  34. long timecost = 0;
  35. timecost = 1000000*(tv2.tv_sec - tv1.tv_sec) + (tv2.tv_usec - tv1.tv_usec);
  36. return timecost; }
  37. void init_employee(Employee_S* employee)
  38. { time_t ttime;
  39. if(NULL == employee)
  40. { PRINT_TRACE("Invalid parameter: NULL pointer!");
  41. return ; }
  42. memset(employee, 0, sizeof(*employee));
  43. g_employeeid ++;
  44. employee->id = g_employeeid; employee->age = 0;
  45. employee->sex = UNKNOWN;
  46. time(&ttime); sprintf(employee->registertime, "%s", ctime(&ttime));
  47. sprintf(employee->cellphone, "%0*d", sizeof(employee->cellphone) -1, 0); sprintf(employee->email, "persion_%06d@sun.org", g_employeeid);
  48. sprintf(employee->resume, "Resume:");
  49. employee->salary = 12345.78;
  50. }
  51. void print_employee(Employee_S employee)
  52. { printf("id=%d, age=%d, sex=%d, regtime=%s", employee.id, employee.age, employee.sex, employee.registertime);
  53. printf("cellphone=%s, email=%s, resume=%s, salary=%6.2f ", employee.cellphone, employee.email, employee.resume, employee.salary); }
  54.  
  55. sqlite3* open_db(const char* dbname) {
  56. int ret = -1; sqlite3 *db = NULL;
  57. if(NULL == dbname)
  58. { PRINT_TRACE("Invalid parameter: null database name!");
  59. return NULL; }
  60. ret = sqlite3_open(dbname, &db);
  61. if(SQLITE_OK != ret) {
  62. PRINT_TRACE("Open database "%s" failed: %s.", dbname, sqlite3_errmsg(db)); sqlite3_close(db);
  63. return NULL; }
  64. return db;
  65. }
  66. int close_db(sqlite3* db) {
  67. int ret = -1;
  68. if(NULL != db) {
  69. ret = sqlite3_close(db); }
  70. return ret;
  71. }
  72. int create_table(sqlite3* db, const char* tablename) {
  73. int ret = -1; int cmdlen = 0;
  74. char *errmsg ;
  75. char *sqlfmt = "CREATE TABLE %s (id INTEGER PRIMARY KEY, age INTEGER, sex " "INTEGER, registertime VARCHAR(26), cellphone VARCHAR(12),"
  76. "email TEXT, resume TEXT, salary REAL);"; char *sqlcmd = NULL;
  77.  
  78. if(NULL == db || NULL == tablename) {
  79. PRINT_TRACE("Invalid parameter: &db=%p, &tablename=%p.", db, tablename); return ret;
  80. }
  81. //必须用 strlen 获取字符长度,不能用 sizeof,sizeof 获取的是指针长度,为4. cmdlen = strlen(sqlfmt) + strlen(tablename) + 1;
  82. sqlcmd = (char*)malloc(cmdlen);
  83. if(NULL == sqlcmd) {
  84. PRINT_TRACE("Not enough memory for sql command!"); return ret;
  85. }
  86. memset(sqlcmd, 0, cmdlen); sprintf(sqlcmd, sqlfmt, tablename);
  87. ret = sqlite3_exec(db, sqlcmd, NULL, NULL, &errmsg);
  88. if(SQLITE_OK != ret) {
  89. PRINT_TRACE("Create table "%s" failed: %s.", tablename, errmsg); sqlite3_free(errmsg);
  90. free(sqlcmd); return ret;
  91. }
  92. free(sqlcmd);
  93. return ret; }
  94. char* get_sqlcmd(const char* tablename, SQL_OPTYPE optype, Employee_S employee)
  95. { char* insertfmt = "INSERT INTO %s values(%d, %d, %d, '%s', '%s', '%s', '%s', %10.6f);";
  96. char* selectfmt = "SELECT * FROM %s where id <= %d;"; char* updatefmt = "UPDATE %s set age=%d, sex=%d, registertime='%s',cellphone='%s',"
  97. "email='%s', resume='%s', salary=%10.6f where id=%d;"; char* deletefmt = "DELETE FROM %s where id=%d;";
  98. char* sqlcmd = NULL; int cmdlen = 0;
  99. if(NULL == tablename)
  100. { PRINT_TRACE("Invalid parameter: NULL pointer of tablename!");
  101. return NULL; }
  102. //为了简化,给 sql 语句申请最大空间
  103. cmdlen = strlen(updatefmt) + strlen(tablename) + sizeof(employee); sqlcmd = (char*) malloc(cmdlen);
  104. if(NULL == sqlcmd) {
  105. PRINT_TRACE("Not enough memory for sql command!"); return NULL;
  106. }
  107. switch(optype) {
  108. case INSERT: sprintf(sqlcmd, insertfmt, tablename, employee.id, employee.age,
  109. employee.sex, employee.registertime, employee.cellphone, employee.email, employee.resume, employee.salary);
  110. break;
  111. case SELECT: sprintf(sqlcmd, selectfmt, tablename, employee.id);
  112. break;
  113. case UPDATE: sprintf(sqlcmd, updatefmt, tablename, employee.age, employee.sex,
  114. employee.registertime, employee.cellphone, employee.email, employee.resume, employee.salary, employee.id);
  115. break;
  116. case DELETE: sprintf(sqlcmd, deletefmt, tablename, employee.id);
  117. break;
  118. default: PRINT_TRACE("Unknown operation type:%d ", optype);
  119. free(sqlcmd); return NULL;
  120. }
  121. return sqlcmd;
  122. }
  123. int test(const char* dbname, const char* tablename, SQL_OPTYPE optype, int count, int istableexists)
  124. { int ret = -1;
  125. int i = 0; int failcount = 0;
  126. long costtime = 0;
  127. time_t ttime; struct timeval tvStart;
  128. struct timeval tvEnd;
  129. sqlite3* db = NULL; Employee_S employee;
  130. char* sqlcmd = NULL; char* errmsg = NULL;
  131. time(&ttime);
  132. printf(" Start "%s" at: %s", __FUNCTION__, ctime(&ttime));
  133. if(NULL == dbname || NULL == tablename || 0 >= count) {
  134. PRINT_TRACE("Invalid Parameter:dbname=%p, tablename=%p, count=%d.", dbname, tablename, count); return ret;
  135. }
  136. //打开数据库 db = open_db(dbname);
  137. if(NULL == db) {
  138. return ret; }
  139. //判断是否需要创建表
  140. if( 0 == istableexists) {
  141. ret = create_table(db, tablename); if(SQLITE_OK != ret)
  142. { close_db(db);
  143. return ret; }
  144. }
  145. //开启事务模式 START_TRANSACTION();
  146. for(i = 0; i < count; i++)
  147. { init_employee(&employee);
  148. sqlcmd = get_sqlcmd(tablename, optype, employee);
  149. if(NULL == sqlcmd)
  150. { failcount ++;
  151. continue; }
  152. //开始计时
  153. gettimeofday(&tvStart, NULL);
  154. ret = sqlite3_exec(db, sqlcmd, NULL, NULL, &errmsg);
  155. //结束计时 gettimeofday(&tvEnd, NULL);
  156. costtime += diff_timeval(tvStart, tvEnd);
  157. if(SQLITE_OK != ret)
  158. { failcount ++;
  159. PRINT_TRACE("Excecute sql: %s failed! Error info:%s.", sqlcmd, errmsg); }
  160. if(NULL != sqlcmd)
  161. { free(sqlcmd);
  162. }
  163. if(NULL != errmsg) {
  164. sqlite3_free(errmsg); }
  165. }
  166. //关闭事务模式
  167. END_TRANSACTION();
  168. close_db(db);
  169. //打印结果 printf("Operation Type: %d, Database name: %s, Table name: %s. ", optype, dbname, tablename);
  170. printf("Counts TotalTime(us) AverageTime(us) "); printf("%-8d %-10ld %-10.6f ", count, costtime, (float)(costtime/count));
  171. time(&ttime);
  172. printf("Finish "%s" at: %s ", __FUNCTION__, ctime(&ttime));
  173. if(0 < failcount) {
  174. ret = -1; }
  175. return ret;
  176. }
  177. int main()
  178. { int count = 1000;
  179. int istableexists = 0; int ret = -1;
  180. char* dbname = "employee.db";
  181. char* tablename = "employee";
  182. SQL_OPTYPE optype;
  183. optype = INSERT; ret = test(dbname, tablename, optype, count, istableexists);
  184. if(SQLITE_OK != ret) {
  185. PRINT_TRACE("Test failed!"); return 0;
  186. }
  187. istableexists = 1; g_employeeid = 0;
  188. optype = SELECT; ret = test(dbname, tablename, optype, count, istableexists);
  189. if(SQLITE_OK != ret)
  190. { PRINT_TRACE("Test failed!");
  191. return 0; }
  192. g_employeeid = 0;
  193. optype = UPDATE;
  194. ret = test(dbname, tablename, optype, count, istableexists); if(SQLITE_OK != ret)
  195. { PRINT_TRACE("Test failed!");
  196. return 0; }
  197. g_employeeid = 0;
  198. optype = DELETE;
  199. ret = test(dbname, tablename, optype, count, istableexists); if(SQLITE_OK != ret)
  200. { PRINT_TRACE("Test failed!");
  201. return 0; }
  202. return 0;
  203. }
Tag标签: 事务   性能   模式  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规