关于事务开启与否对数据库插入数据所需时间的影响的讨论
根据sqlite3的api,在后面测试的时候发现对于sqlite3来说事务的开启与否仅插入1w条数据,时间的差距就已经非常非常明显了,先说下
最近在做sqlite3的二次开发,看到API里面关于事务的时候,萌生了测试一下事务的开启与否对插入数据所需要的时间影响的想法,根据sqlite3的api,,在后面测试的时候发现对于sqlite3来说事务的开启与否仅插入1w条数据,时间的差距就已经非常非常明显了,先说下测试环境:Ubuntu 12.04, sqlite3 3.7.14.1,测试插入1w条数据。
不开启事务时的测试代码如下:
int insert_no_trans()
{
printf(" \n"
"go into function insert_no_trans()\n");
int j = 0;
for (j = 0; j {
sprintf(sql, "INSERT INTO [dev] ([id], [name], [age])\
values (%d, '%s', %d)", j, "JGood", j);
if(SQLITE_OK != sqlite3_exec
(conn, sql, 0, 0, &err_msg))
{
fprintf(stderr, "INSERT ERROR: %s\n", err_msg);
exit(EXIT_FAILURE);
}
}
printf("INSERT all succussfully!\n");
printf("function insert_no_trans() end. \n"
" \n");
return EXIT_SUCCESS;
}
开启事务的测试代码如下:
int insert_with_trans()
{
printf(" \n"
"go into function insert_with_trans()\n");
sqlite3_exec(conn, "begin;", 0, 0, 0); //开启事务
int j = 0;
for (j = 0; j {
sprintf(sql, "INSERT INTO [dev] ([id], [name], [age])\
values (%d, '%s', %d)", j, "JGood", j);
if(SQLITE_OK != sqlite3_exec(conn, sql, 0, 0, &err_msg))
{
is_succeed = false; //失败之后把标识设为false
fprintf(stderr, "INSERT ERROR: %s\n", err_msg);
break;
}
}
if(is_succeed)
sqlite3_exec(conn, "commit;", 0, 0, 0); //提交事务
else
{
sqlite3_exec(conn, "rollback;", 0, 0, 0); //回滚事务
exit(EXIT_FAILURE);
}
printf("INSERT all succussfully!\n");
printf("function insert_with_trans() end. \n"
" \n");
return EXIT_SUCCESS;
}
测试结果大跌眼镜,时间测试我用的是linux的time命令,在开启了事务的情况下插入1w条数据的时间仅为0.4s,而在不开启事务的情况下,由于时间太长,没有等其执行完,根据.db文件的大小和已经使用的时间推测,假设单位时间插入同样多条数据,推算出来的时间高达18分钟。这已经不在一个数量级了,google了一下这个问题,看到网上有人对此是这么解释的,如果未启用事务,sqlite会每插入一条数据,就往磁盘上面写一次,在整个执行过程中我也观察到未开启事务时程序执行期间硬盘灯一直是亮的,这也映证了这一点。而在开启事务的情况下,其应该是在对数据全部处理完之后才需要执行一次IO操作,时间自然非常快。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use database transactions (Transactions) in the Phalcon framework Introduction: Database transactions are an important mechanism that can ensure the atomicity and consistency of database operations. When developing using the Phalcon framework, we often need to use database transactions to handle a series of related database operations. This article will introduce how to use database transactions in the Phalcon framework and provide relevant code examples. 1. What are database transactions (Transactions)? data

With the rapid development of the Internet, the importance of databases has become increasingly prominent. As a Java developer, we often involve database operations. The efficiency of database transaction processing is directly related to the performance and stability of the entire system. This article will introduce some techniques commonly used in Java development to optimize database transaction processing efficiency to help developers improve system performance and response speed. Batch insert/update operations Normally, the efficiency of inserting or updating a single record into the database at one time is much lower than that of batch operations. Therefore, when performing batch insert/update

In web development, database transaction processing is an important issue. When a program needs to operate multiple database tables, ensuring data consistency and integrity becomes particularly important. Transaction processing provides a way to ensure that these operations either all succeed or all fail. As a popular web development language, PHP also provides transaction processing functions. This article will introduce the best practices for database transaction processing using PHP. What is a database transaction? In a database, a transaction refers to a series of operations performed as a whole.

How to solve database transaction problems in Java back-end function development? In the development of Java back-end functions, functions involving database operations are very common. In database operations, transactions are a very important concept. A transaction is a logical unit consisting of a sequence of database operations that is either fully executed or not executed at all. In practical applications, we often need to ensure that a set of related database operations are either all successfully executed or all rolled back to maintain data consistency and reliability. So, how to develop in Java backend

How to handle database transactions in C# development requires specific code examples. Introduction: In C# development, database transaction processing is a very important technology. Through transaction processing, we can ensure the consistency and integrity of database operations and improve the stability and security of the system. This article will introduce how to handle database transactions in C# and give specific code examples. 1. Introduction to database transactions A database transaction is a logical unit of database operations, which can be composed of one or more operations. Transactions have four basic attributes,

Java development: How to use JPA for database transaction management In Java development, database transaction management is a very important and common requirement. JPA (JavaPersistenceAPI) is part of JavaEE and provides a convenient way to perform database operations. This article will introduce how to use JPA for database transaction management and provide specific code examples. First, we need to introduce JPA-related dependencies into the project. Common JPA implementations include Hibern

In PHP programming, database transaction isolation level is an important concept. Transactions are the basic unit of database management and operation, allowing the database to operate effectively and safely based on consistency and integrity. The transaction isolation level refers to the degree of mutual influence between multiple transactions. In PHP programming, it is essential to understand the concept of database transaction isolation level and its corresponding applications. In the database, there are four transaction isolation levels: uncommitted read (Readuncommitted), committed read (Read

Application of Redis in Golang development: How to handle database transactions Introduction: In Golang development, Redis, as a high-performance cache and data storage database, is widely used in various scenarios, especially in processing database transactions. This article will introduce how Redis handles database transactions in Golang development and provide relevant code examples. 1. What is a transaction? A transaction refers to a series of database operations (read or write). These operations are either all executed successfully or all fail and are rolled back.
