sqlite的数据库本质上来讲就是一个磁盘上的文件,所以一切的数据库操作其实都会转化为对文件的操作,而频繁的文件操作将会是一个很好时的过程,会极大地影响数据库存取的速度。
这样
sqlite将把全部要执行的
sql语句先缓存在内存当中,然后等到COMMIT的时候一次性的写入数据库,这样
数据库文件只被打开关闭了一次,效率自然大大的提高。有一组数据对比:
测试1: 1000 INSERTs
create table t1(a
IntegeR,b
IntegeR,c VARCHAR(100)
);
INSERT INTO t1 VALUES(1,13153,'thirteen thousand one hundred fifty three'
);
INSERT INTO t1 VALUES(2,75560,'seventy five thousand five hundred sixty'
);
... 995 li
nes omitted
INSERT INTO t1 VALUES(998,66289,'sixty six thousand two hundred eighty nine'
);
INSERT INTO t1 VALUES(999,24322,'twenty four thous
and three hundred twenty two'
);
INSERT INTO t1 VALUES(1000,94142,'ninety four thousand one hundred forty two'
);
sqlite 2.7.6:
13.061
sqlite 2.7.6 (nosyn
C):
0.223
测试2: 使用事务 25000 INSERTs
BEGIN;
create table t2(a
IntegeR,c VARCHAR(100)
);
INSERT INTO t2 VALUES(1,59672,'fifty nine thousand six hundred seventy two'
);
... 24997 li
nes omitted
INSERT INTO t2 VALUES(24999,89569,'eighty nine thousand five hundred sixty nine'
);
INSERT INTO t2 VALUES(25000,94666,'ninety four thousand six hundred sixty six'
);
COMMIT;
sqlite 2.7.6:
0.914
sqlite 2.7.6 (nosyn
C):
0.757
可见使用了事务之后却是极大的提高了数据库的效率。但是我们也要注意,使用事务也是有一定的开销的,所以对于数据量很小的操作可以不必使用,以免造成而外的消耗。
androID中的方法,
public final intbulkInsert(Uriurl,ContentValues[]values)
Inserts multiple rows into a table at the given URl. This function make no guarantees about the atomicity of the insertions.
Parameters
@H_
489_102@
url |
The URL of the table to insert into. |
values |
The initial values for the newly inserted rows. The key is the column name for the fIEld. Passing null will create an empty row. |