此次调研分两步走吧, 先从测试的角度看(一), 再从源码的角度看(二)(待续).
下面的是测试代码:
SQLiteUtils.java
package com.johnsoft.app.temp;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* @author John Kenrinus Lee
* @version 2016-05-16
*/
public class SQLiteUtils {
private static SQLiteOpenHelper sHelper;
public static void loopInsert(Context context) {
final SQLiteOpenHelper helper = getSQLiteHelper(context);
final SQLiteDatabase db = helper.getWritableDatabase();
db.beginTransaction();
String[] args = new String[2];
for (int i = 0; i < 200000; ++i) {
args[0] = "person" + i;
args[1] = String.valueOf(i);
db.execSQL("insert into person (name,age) values (?,?);", args);
}
db.setTransactionSuccessful();
db.endTransaction();
System.err.println("Insert OOOOOOOOOOOOOOKKKKKKKKKKKKKK");
}
public static void fullQuery(Context context) {
final long start = System.currentTimeMillis();
final SQLiteOpenHelper helper = getSQLiteHelper(context);
final SQLiteDatabase db = helper.getReadableDatabase();
final Cursor cursor = db.rawQuery("select * from person", null);
long count = 0;
int size = 0;
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
count += cursor.getString(1).length();
++size;
}
cursor.close();
System.err.println("Size: " + size);
System.err.println("Query count ccccccccccccccc " + count);
System.err.println("total: " + (System.currentTimeMillis() - start));
}
public static void segQuery(Context context) {
final long start = System.currentTimeMillis();
final SQLiteOpenHelper helper = getSQLiteHelper(context);
final SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from person", null);
final int size;
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
size = cursor.getInt(0);
} else {
System.err.println("Query failed !!!!!!!!!!!!!!!!!!!!!!!");
return;
}
cursor.close();
int offset = 0;
final int length = size / 100;
long count = 0;
boolean isNotLast = true;
int arg0, arg1;
while(isNotLast) {
if (offset + length > size) {
arg0 = size - offset;
if (arg0 <= 0) {
break;
} else {
isNotLast = false;
}
} else {
arg0 = length;
}
arg1 = offset;
offset += length;
System.err.println("args0: " + arg0 + ", args1: " + arg1);
cursor = db.rawQuery("select * from person limit " + arg0 + " offset " + arg1 + ";", null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
count += cursor.getString(1).length();
}
cursor.close();
}
System.err.println("Query count ccccccccccccccc " + count);
System.err.println("total: " + (System.currentTimeMillis() - start));
}
private static SQLiteOpenHelper getSQLiteHelper(Context context) {
if (sHelper == null) {
synchronized(SQLiteUtils.class) {
if (sHelper == null) {
sHelper = new SQLiteOpenHelper(context, "qu.db", null, 1) {
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists person ("
+ "id integer primary key,"
+ "name text not null,"
+ "age text);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
};
}
}
}
return sHelper;
}
}
MainActivity.java
package com.johnsoft.app.temp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View insert = findViewById(R.id.insert);
final View full = findViewById(R.id.full);
final View seg = findViewById(R.id.seg);
if (insert != null) {
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread("insert") {
@Override
public void run() {
SQLiteUtils.loopInsert(getApplicationContext());
}
}.start();
}
});
}
if (full != null) {
full.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread("full") {
@Override
public void run() {
SQLiteUtils.fullQuery(getApplicationContext());
}
}.start();
}
});
}
if (seg != null) {
seg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread("seg") {
@Override
public void run() {
SQLiteUtils.segQuery(getApplicationContext());
}
}.start();
}
});
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/insert" android:text="Insert"
android:layout_alignParentTop="true" android:layout_marginTop="10dp"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/full" android:text="Full Query"
android:layout_below="@id/insert" android:layout_marginTop="20dp"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/seg" android:text="Seg Query"
android:layout_below="@id/full" android:layout_marginTop="20dp"
/>
</RelativeLayout>
测试结果:[fullQuery]
05-16 00:01:35.845 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 404 bytes, free space 114 bytes, window size 2097152 bytes
05-16 00:01:37.933 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:39.519 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:41.150 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 36 bytes, free space 16 bytes, window size 2097152 bytes
05-16 00:01:42.812 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 404 bytes, free space 114 bytes, window size 2097152 bytes
05-16 00:01:44.489 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:46.176 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:47.849 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:49.582 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 404 bytes, free space 114 bytes, window size 2097152 bytes
05-16 00:01:51.345 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 36 bytes, free space 18 bytes, window size 2097152 bytes
05-16 00:01:53.092 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:54.825 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:01:56.633 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 404 bytes, free space 114 bytes, window size 2097152 bytes
05-16 00:01:58.474 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 36 bytes, free space 26 bytes, window size 2097152 bytes
05-16 00:02:00.324 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:02:02.186 11175-11468/com.johnsoft.app.temp W/CursorWindow: Window is full: requested allocation 6 bytes, free space 0 bytes, window size 2097152 bytes
05-16 00:02:03.927 11175-11468/com.johnsoft.app.tempx W/System.err: Size: 400000
05-16 00:02:03.927 11175-11468/com.johnsoft.app.tempx W/System.err: Query count ccccccccccccccc 4355560
05-16 00:02:03.927 11175-11468/com.johnsoft.app.tempx W/System.err: total: 29344
测试结果[segQuery]:
05-16 00:04:52.782 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 0
05-16 00:04:52.849 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 4000
05-16 00:04:52.912 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 8000
05-16 00:04:52.972 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 12000
05-16 00:04:53.032 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 16000
05-16 00:04:53.100 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 20000
05-16 00:04:53.162 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 24000
05-16 00:04:53.217 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 28000
05-16 00:04:53.267 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 32000
05-16 00:04:53.322 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 36000
05-16 00:04:53.378 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 40000
05-16 00:04:53.436 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 44000
05-16 00:04:53.489 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 48000
05-16 00:04:53.544 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 52000
05-16 00:04:53.599 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 56000
05-16 00:04:53.661 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 60000
05-16 00:04:53.715 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 64000
05-16 00:04:53.769 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 68000
05-16 00:04:53.825 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 72000
05-16 00:04:53.881 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 76000
05-16 00:04:53.943 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 80000
05-16 00:04:54.005 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 84000
05-16 00:04:54.063 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 88000
05-16 00:04:54.121 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 92000
05-16 00:04:54.180 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 96000
05-16 00:04:54.247 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 100000
05-16 00:04:54.306 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 104000
05-16 00:04:54.364 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 108000
05-16 00:04:54.428 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 112000
05-16 00:04:54.495 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 116000
05-16 00:04:54.555 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 120000
05-16 00:04:54.616 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 124000
05-16 00:04:54.678 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 128000
05-16 00:04:54.741 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 132000
05-16 00:04:54.810 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 136000
05-16 00:04:54.872 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 140000
05-16 00:04:54.940 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 144000
05-16 00:04:55.015 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 148000
05-16 00:04:55.081 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 152000
05-16 00:04:55.149 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 156000
05-16 00:04:55.219 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 160000
05-16 00:04:55.287 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 164000
05-16 00:04:55.369 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 168000
05-16 00:04:55.449 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 172000
05-16 00:04:55.526 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 176000
05-16 00:04:55.605 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 180000
05-16 00:04:55.681 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 184000
05-16 00:04:55.759 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 188000
05-16 00:04:55.847 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 192000
05-16 00:04:55.935 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 196000
05-16 00:04:56.051 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 200000
05-16 00:04:56.145 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 204000
05-16 00:04:56.229 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 208000
05-16 00:04:56.315 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 212000
05-16 00:04:56.409 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 216000
05-16 00:04:56.498 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 220000
05-16 00:04:56.583 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 224000
05-16 00:04:56.686 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 228000
05-16 00:04:56.773 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 232000
05-16 00:04:56.863 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 236000
05-16 00:04:56.964 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 240000
05-16 00:04:57.056 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 244000
05-16 00:04:57.154 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 248000
05-16 00:04:57.250 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 252000
05-16 00:04:57.338 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 256000
05-16 00:04:57.434 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 260000
05-16 00:04:57.536 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 264000
05-16 00:04:57.636 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 268000
05-16 00:04:57.730 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 272000
05-16 00:04:57.828 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 276000
05-16 00:04:57.933 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 280000
05-16 00:04:58.034 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 284000
05-16 00:04:58.129 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 288000
05-16 00:04:58.234 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 292000
05-16 00:04:58.353 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 296000
05-16 00:04:58.456 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 300000
05-16 00:04:58.559 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 304000
05-16 00:04:58.662 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 308000
05-16 00:04:58.772 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 312000
05-16 00:04:58.888 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 316000
05-16 00:04:58.995 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 320000
05-16 00:04:59.105 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 324000
05-16 00:04:59.212 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 328000
05-16 00:04:59.320 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 332000
05-16 00:04:59.450 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 336000
05-16 00:04:59.562 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 340000
05-16 00:04:59.671 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 344000
05-16 00:04:59.780 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 348000
05-16 00:04:59.889 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 352000
05-16 00:05:00.011 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 356000
05-16 00:05:00.141 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 360000
05-16 00:05:00.256 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 364000
05-16 00:05:00.376 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 368000
05-16 00:05:00.498 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 372000
05-16 00:05:00.617 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 376000
05-16 00:05:00.725 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 380000
05-16 00:05:00.843 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 384000
05-16 00:05:00.965 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 388000
05-16 00:05:01.083 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 392000
05-16 00:05:01.208 11175-13623/com.johnsoft.app.temp W/System.err: args0: 4000, args1: 396000
05-16 00:05:01.331 11175-13623/com.johnsoft.app.temp W/System.err: Query count ccccccccccccccc 4355560
05-16 00:05:01.331 11175-13623/com.johnsoft.app.temp W/System.err: total: 8595
结论: 不要去一次查几十万条, 而是使用分页查询(没有产生window full的情况), 从时间看, 显然分页查快很多;