SQlite優化查詢的例子

2021-05-23 10:23:44 字數 3761 閱讀 8360

上次講到了sqlite的查詢優化**中的具體實現,現在來看一下它的幾個例項:

1 #include "stdio.h"

2 #include "sqlite3.h"

3 #include

4 void query(sqlite3 *db,sqlite3_stmt *stmt,char * sql);

5 6 int main(int argc, char **argv)

7 18 

19     //下面是所建的各個表的結構

20     sql="create table t1 (num int,word text not null)";

21     //sql="create table t4 (num integer not null,word text not null)";

22     //sql="create table t3 (num integer not null,word text not null)";

23     rc = sqlite3_exec(db, sql, null, null, &zerr);

24     if(rc != sqlite_ok)

29     }

30 31     //下面是對所以插入進行手動提交,這樣可以加快插入速度

32     //sqlite3_exec(db,"begin",null,null,&zerr);

33     //插入1000000條記錄

34     //for (int i=0;i<1000000;i++)

35     //

39     //sqlite3_exec(db,"commit",null,null,&zerr);

40     

41     sql="create index t1nwindex on t1(num)";

42     rc=sqlite3_exec(db, sql, null, null, &zerr);

43     if(rc != sqlite_ok)

48     }

49 50     //sql="drop index t1nwindex";

51     //sql="drop index t3index";

52     //sql="delete from t2";

53     rc=sqlite3_exec(db, sql, null, null, &zerr);

54     if(rc != sqlite_ok)

59     }

60 61     printf("查詢結果是:n");

62     //sql="select * from t1 where num=3000 or num=2000";//有integer primary key,快

63     //sql="select * from t2 where num=3000 or num=2000";//沒有索引,慢

64     //sql="select * from t3 where num=3000 or num=2000";//有索引,快

65     

66     //這裡交換位置了,但是結果用的時間想差比較大的原因是,t1是用索引儲存的,但是它不是由create index

67     //而建立的,所以系統還不會把它作為索引處理,所以這兩個表就只是無索引的表,在內部優化計算代價只是對它

68     //進行估計,因為源**中沒有捕獲到下面的查詢條件,所以都是系統最大值(源**中有),所以就巢狀順序沒

69     //變,所以出現下面的差異。

70     //sql="select count(*) from t3, t1 where t1.num = t3.num";//比下面的快,由於內層少

71     //sql="select count(*) from t1, t3 where t1.num = t3.num";//比上面的慢,由於內層多

72 73     //下面這個已經內部實現優化,所以所用時間是相同的

74     //sql="select * from t2, t3 where t2.num = t3.num";//有索引,稍快

75     //sql="select * from t3, t2 where t2.num = t3.num";//同上,內部已經優化

76 77     //sql="select * from t3 where num=8000";//有索引,快

78     //sql="select * from t1 where num%2=0";//有索引,但不能用,很慢

79     //sql="select * from t1 where num=8000";//沒有索引,慢

80     

81     //between的轉換優化---內部已經實現優化,如果有索引的話快一點

82     //sql="select count(*) from t2 where word between 'goodl' and 'goodm'";//between

83     //sql="select count(*) from t2 where word >='goodl' and word<'goodm'";//between的轉換

84 85     //like的轉換優化---內部已經實現優化

86     //sql="select count(*) from t2 where word like 'goodl%'";//有索引不起作用

87     //sql="select count(*) from t2 where word >='goodl' and word <'goodm'";//如果有索引會更快

88 89     //in的轉換優化---內部沒有實現優化,但此時如果可以用索引的話就會很好

90     //如果不用索引則在這裡體現不出in比or優,而如果有索引則差別很明顯

91     //sql="select count(*) from t2 where word in('goodllll','goodkkkk','goodaaaa')";

92     //sql="select count(*) from t2 where word ='goodllll' or word ='goodkkkk' or word='goodaaaa'";

93 94     int start=gettickcount();

95     query(db,stmt,sql);

96     printf("the time has pass:%dmsn",gettickcount()-start);

97     sqlite3_close(db);

98     return 0;    

99 }

100 

101 void query(sqlite3 *db,sqlite3_stmt *stmt,char * sql)

108     rc = sqlite3_step(stmt);

109     ncols = sqlite3_column_count(stmt); 

110     while(rc == sqlite_row)

114         fprintf(stderr, "n");

115         rc = sqlite3_step(stmt);

116     }

117 }

Sqlite 的小例子

第一步,搞乙個資料庫幫助類,繼承sqliteopenhelper類 第二步,寫乙個activity,用來對資料庫實現相關操作 第三步,當然不可缺少布局檔案,相當簡單,就不再貼 了 第四步,執行結果 05 10 03 43 50.959 info system.out 15420 資料已經存在!05 ...

SQLite應用之路 SQL查詢優化

temp1 2499條資料 temp6 969596條資料 注意時間單位ms和s 其中temp1和temp2已經給eid加上索引 外表大於子表的時候,使用in 外表小於字表的時候,使用exists select from temp1 where eid in select eid from temp...

Sqlite的索引優化

一直比較喜歡文字型資料庫,簡單 方便,容易儲存。最近將乙個 的mysql 資料庫轉換成了 2.5g 的 sqlite資料庫。悲劇發生了,非常慢,經常超出30秒的執行時間,所以一直用快取扛著,10.1 假期正好有空,決定徹底解決下這個慢的問題。首先是首頁慢,認真的分析了首頁呼叫的函式,發現卡死經常在乙...