zoukankan      html  css  js  c++  java
  • sqlite优化记录:建立索引加快查询速度

    凡是数据库中,索引的存在就是为了提高查询速度的,数据库的索引有点类似于书本上面的目录的概念,因为在英文中都是index,事实上也就是目录。

    其算法应该叫做“倒排索引”,这个其实也类似于搜索引擎里面的基本算法。

    测试:10w条数据,没有索引的情况下,查询一条数据大约需要550ms以上。

    建立索引后,数据库的体积增大了3倍左右,但是同样的查询却减少到8ms的级别,提升了70倍

    有时候关于sqlite数据库出错或者没法用的情况看这里

    下面是在android手机上面的测试代码

    01 //创建数据库
    02 SQLiteDatabase  database = SQLiteDatabase.openOrCreateDatabase(databaseFile,  "test123", null);
    03 database.execSQL("create table if not exists t1(a,b)");
    04 //创建索引
    05 database.execSQL("create index if not exists ia on t1(a,b)");
    06 //插入10w条数据
    07 for (int i = 0; i < 100000; i++) {
    08     database.execSQL("insert into t1(a, b) values(?, ?)",new String[] { "" + i, "name" + Math.random() * i });
    09 }
    10 //查询一条数据
    11 Log.v("test","开始查询");
    12             Cursor cursor = database.rawQuery("select * from t1 where a='88980'", null);
    13             while(cursor.moveToNext()){
    14                 String a = cursor.getString(cursor.getColumnIndex("a"));
    15                 String b = cursor.getString(cursor.getColumnIndex("b"));
    16                 Log.v("test","查找结果---------->" + "a: "+a+"  "+"b: "+b);
    17             }
    18             Log.v("test", "查找结束");

    本文地址http://tweetyf.org/2012/06/sqlite_optimization_using_index.html

  • 相关阅读:
    day_15补充加总结
    Day_15 内置模块er
    sort 函数 与 sorted 函数区别
    python 代码风格------------PEP8规则
    python 返回值
    函数记忆 : startswith() 与 endswith
    RE模块使用
    正则表达式
    collections
    函数datetime
  • 原文地址:https://www.cnblogs.com/daishuguang/p/4015395.html
Copyright © 2011-2022 走看看