2018.4.2 sqlite优化

一.参数优化。

```
PRAGMA foreign_keys=ON;PRAGMA cache_size=8000;PRAGMA synchronous=OFF;PRAGMA temp_store=MEMORY;PRAGMA auto_vacuum=0;
```

二. 给特定的字段创建索引。
如以下查询语句:
```
select * from files where inode=?
```
就给inode创建索引:

```
create index index_inode on files(inode)
```
三. 批量插入使用事务。

```
sqlite3_exec(pstDb, "BEGIN", NULL, NULL, &errorMsg);
sqlite3_exec(pstDb, "ROLLBACK", NULL, NULL, NULL);
sqlite3_exec(pstDb, "COMMIT", NULL, NULL, NULL);
```

四. 文件数据库和内存数据库结合。(可能会丢数据)

五. 日志模式修改。


```
PRAGMA journal_mode = delete;
PRAGMA journal_mode = wal; //会生成.db-wal .db-shm文件
```
六. 简化字段,封装为结构体,使用blob类型存储。

```
sqlite3_bind_blob(pstStmt, 0, &(pstFileCache->stStat), sizeof(struct stat), 0);
memcpy(&(pstFileCache->stStat), sqlite3_column_blob(pstStmt, 2), sizeof(struct stat));
```

七. 优化sql查询语句,将查询量更小的放在前面。

一边喊着救命,一边享受沉沦。
原文地址:https://www.cnblogs.com/fast-walking/p/8706708.html