下面是在Android4.0上,利用Sqlite数据库的insert,query,update,delete函数以及execSql,rawQuery函数执行插入,查询,更新,删除操作花费时间的对比结果。
是在执行相同的动作,记录条数也一样的情况下的对比,多次验证的结果是:
(1)如果批量执行的记录数在1000条,则Android SqliteDatabase提供的insert,query,update,delete函数和直接写SQL文的execSql,rawQuery的效率差不多,几乎一样。所以使用哪种放到都可以,不会影响到执行效率。
(2)如果批量执行的记录数在10万条,则会存在差别。在某台手机上SqliteDatabase提供的insert执行插入操作耗时45秒,要比execSql插入35秒慢10秒左右。
可见在数据库大的情况下,还是有差别的。execSql省去了拼接sql语句的步骤,要比SqliteDatabase提供的insert,query,update,delete等函数效率高。当数据库越大,差别也越大。
下面是验证代码:
[java] view plaincopy
- public class MainActivity extends Activity {
- private static final String TAG = "MainActivity";
- private DBHelper mDbHelper = null;
- private static TextView mTvInfo;
- private static ProgressDialog mDialog = null;
- private static Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- // TODO Auto-generated method stub
- if(mDialog != null) {
- mDialog.dismiss();
- mDialog = null;
- }
- if (msg.what == 0) {
- Bundle b = msg.getData();
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("insert插入1000条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 1) {
- Bundle b = msg.getData();
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("execSql插入1000条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 10) {
- Bundle b = msg.getData();
- int count = b.getInt("count");
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("query查询");
- builder.append(count).append("条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 11) {
- Bundle b = msg.getData();
- int count = b.getInt("count");
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("rawQuery查询");
- builder.append(count).append("条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 20) {
- Bundle b = msg.getData();
- int count = b.getInt("count");
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("update更新");
- builder.append(count).append("条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 21) {
- Bundle b = msg.getData();
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("execSql更新1000条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 30) {
- Bundle b = msg.getData();
- int count = b.getInt("count");
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("delete删除");
- builder.append(count).append("条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- } else if (msg.what == 31) {
- Bundle b = msg.getData();
- int seconds = b.getInt("seconds");
- int ms = b.getInt("ms");
- StringBuilder builder = new StringBuilder("execSql删除1000条记录花费时间:");
- if (seconds > 0) {
- builder.append(seconds).append("s ");
- }
- builder.append(ms).append("ms");
- String strLines[] = mTvInfo.getText().toString().split("\n");
- // 最多显示8条信息
- for (int i = 0; i < strLines.length; i++) {
- builder.append(‘\n‘).append(strLines[i]);
- if (i == 6) {
- break; // 加上原来最多7条信息,共显示8条信息
- }
- }
- mTvInfo.setText(builder.toString());
- }
- }
- };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button btnCreateDb = (Button)findViewById(R.id.btnCreateDb);
- btnCreateDb.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- mDbHelper = new DBHelper(MainActivity.this, "test", null, 1);
- }
- });
- // insert插入
- Button btnInsert = (Button)findViewById(R.id.btnInsert);
- btnInsert.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "insert插入", "正在插入1000条记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- // 利用事物批量插入数据以提高效率
- ContentValues values = new ContentValues();
- long startms = System.currentTimeMillis();
- db.beginTransaction();
- for (int i = 0; i < 1000; i++) {
- values.put("name", new String("张三") + i);
- values.put("birthday", "2012-12-08");
- values.put("gender", i%2);
- db.insert("student", null, values);
- }
- db.setTransactionSuccessful();
- db.endTransaction();
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- Message msg = new Message();
- msg.what = 0;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- } else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // execSQL插入
- Button btnExecSqlInsert = (Button)findViewById(R.id.btnExecSqlInsert);
- btnExecSqlInsert.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "execSQL插入", "正在插入1000条记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- // 利用事物批量插入数据以提高效率
- long startms = System.currentTimeMillis();
- db.beginTransaction();
- for (int i = 0; i < 1000; i++) {
- db.execSQL("INSERT INTO student(name, birthday, gender) values(?, ?, ?)" ,
- new Object[]{new String("李四" + i), "2012-12-08", i%2});
- }
- db.setTransactionSuccessful();
- db.endTransaction();
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- Message msg = new Message();
- msg.what = 1;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- }else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // query查询
- Button btnQuery = (Button)findViewById(R.id.btnQuery);
- btnQuery.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "query查询", "正在查询记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- Log.d(TAG, "query Start:" + getDate());
- long startms = System.currentTimeMillis();
- // 查询所有记录
- Cursor c = db.query("student", null, null, null, null, null, new String("id ASC LIMIT 0,1000"));
- Log.d(TAG, "query End:" + getDate());
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- if (c != null) {
- Message msg = new Message();
- msg.what = 10;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- b.putInt("count", c.getCount());
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- c.close();
- }
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- }else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // rawQuery查询
- Button btnRawQuery = (Button)findViewById(R.id.btnRawQuery);
- btnRawQuery.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "rawQuery查询", "正在查询记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- Log.d(TAG, "rawQuery Start:" + getDate());
- long startms = System.currentTimeMillis();
- // 查询所有记录
- Cursor c = db.rawQuery("SELECT * FROM student ORDER BY id ASC LIMIT 0,1000", null);
- Log.d(TAG, "rawQuery End:" + getDate());
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- if (c != null) {
- Message msg = new Message();
- msg.what = 11;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- b.putInt("count", c.getCount());
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- c.close();
- }
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- } else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // update更新
- Button btnUpdate = (Button)findViewById(R.id.btnUpdate);
- btnUpdate.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "update更新", "正在更新1000条记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- ContentValues values = new ContentValues();
- long startms = System.currentTimeMillis();
- values.put("name", new String("王五"));
- values.put("birthday", "2012-12-09");
- values.put("gender", 0);
- int count = db.update("student", values, new String("id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)"), null);
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- Message msg = new Message();
- msg.what = 20;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- b.putInt("count", count);
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- } else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // execSQL更新
- Button btnExecSqlUpdate = (Button)findViewById(R.id.btnExecSqlUpdate);
- btnExecSqlUpdate.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "execSQL更新", "正在更新1000条记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- long startms = System.currentTimeMillis();
- db.execSQL("UPDATE student SET name = ?, birthday = ?, gender = ? WHERE id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)" ,
- new Object[]{new String("马六"), "2012-12-10", 1});
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- Message msg = new Message();
- msg.what = 21;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- }else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // delete删除
- Button btnDelete = (Button)findViewById(R.id.btnDelete);
- btnDelete.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "delete删除", "正在删除1000条记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- long startms = System.currentTimeMillis();
- int count = db.delete("student", new String("id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)"), null);
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- Message msg = new Message();
- msg.what = 30;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- b.putInt("count", count);
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- } else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- // execSQL删除
- Button btnExecSqlDelete = (Button)findViewById(R.id.btnExecSqlDelete);
- btnExecSqlDelete.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- if (mDbHelper != null) {
- mDialog = ProgressDialog.show(MainActivity.this, "execSQL删除", "正在删除1000条记录,请稍等……", true);
- new Thread() { // 开启线程执行防止阻塞
- @Override
- public void run() {
- SQLiteDatabase db = mDbHelper.getWritableDatabase();
- try {
- long startms = System.currentTimeMillis();
- db.execSQL("DELETE FROM student WHERE id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)");
- long endms = System.currentTimeMillis();
- int seconds = (int)((endms - startms)/1000);
- int ms = (int)((endms - startms)%1000);
- Message msg = new Message();
- msg.what = 31;
- // 使用bundle对象来传递数据
- Bundle b = new Bundle();
- if(seconds > 0) {
- b.putInt("seconds", seconds);
- }
- b.putInt("ms", ms);
- msg.setData(b);
- mHandler.sendMessage(msg);
- } catch(Exception e) {
- e.printStackTrace();
- } finally {
- db.close(); // 关闭数据库
- }
- }
- }.start();
- }else {
- Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();
- }
- }
- });
- mTvInfo = (TextView)findViewById(R.id.tvInfo);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- /**
- * 获取系统时间
- * @return
- */
- public static String getDate(){
- Calendar ca = Calendar.getInstance();
- int year = ca.get(Calendar.YEAR); // 获取年份
- int month = ca.get(Calendar.MONTH); // 获取月份
- int day = ca.get(Calendar.DATE); // 获取日
- int minute = ca.get(Calendar.MINUTE); // 分
- int hour = ca.get(Calendar.HOUR); // 小时
- int second = ca.get(Calendar.SECOND); // 秒
- int millisecond = ca.get(Calendar.MILLISECOND); // 毫秒
- String date = year + "/" + (month + 1 )+ "/" + day + " "+ hour + ":" + minute + ":" + second + ":" + millisecond;
- return date;
- }
- }
执行效果图:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-06 11:04:45