编译sqlite3启动所有扩展功能(Json text_search R-tree树结构 多线程加速等)

为什么要编译自己的sqlite

默认sqlite包是为了轻量化、快速、低消耗而配置的,很多功能都阉割掉了。

然而,我们有时候使用sqlite并不是为了节省资源,而是sqlite用起来太方便了,单库单文件,清晰而美好。

这时候如果我们需要索取更多的功能,比如json的解析功能(json函数说明见此处),或者希望利用多线程做排序,提高速度,那么就需要手动编译一份sqlite。

可用的扩展功能

  • Json1 - JSON Integration → SQL functions for creating, parsing, and querying JSON content.
  • FTS5 - Full Text Search → A description of the SQLite Full Text Search (FTS5) extension.
  • FTS3 - Full Text Search → A description of the SQLite Full Text Search (FTS3) extension.
  • R-Tree Module → A description of the SQLite R-Tree extension. An R-Tree is a specialized data structure that supports fast multi-dimensional range queries often used in geospatial systems.
  • Sessions → The Sessions extension allows change to an SQLite database to be captured in a compact file which can be reverted on the original database (to implement "undo") or transferred and applied to another similar database.
  • Run-Time Loadable Extensions → A general overview on how run-time loadable extensions work, how they are compiled, and how developers can create their own run-time loadable extensions for SQLite.
  • SQLite Android Bindings → Information on how to deploy your own private copy of SQLite on Android, bypassing the built-in SQLite, but using the same Java interface.
  • Dbstat Virtual Table → The DBSTAT virtual table reports on the sizes and geometries of tables storing content in an SQLite database, and is the basis for the [sqlite3_analyzer] utility program.
  • Csv Virtual Table → The CSV virtual table allows SQLite to directly read and query [https://www.ietf.org/rfc/rfc4180.txt|RFC 4180] formatted files.
  • Carray → CARRAY is a [table-valued function] that allows C-language arrays to be used in SQL queries.
  • generate_series → A description of the generate_series() [table-valued function].
  • Spellfix1 → The spellfix1 extension is an experiment in doing spelling correction for [full-text search].

列表见【官网

这些扩展功能在哪里下载?

不需要额外下载,只需要去官网下载sqlite源代码压缩包:https://www.sqlite.org/download.html

比如我下载的是这一份:

sqlite-amalgamation-3310100.zip
(2.28 MiB)
  C source code as an amalgamation, version 3.31.1.
(sha1: a58e91a39b7b4ab720dbc843c201fb6a18eaf32b)

里面只包含几个c语言源代码和头文件,没有makefile之类的东西。

你会发现这些源代码的体积非常大,这是因为上述所有扩展功能,都早已被塞进了统一的源代码文件里。

只是平时编译的时候默认不带扩展而已。源代码里其实都有。

那么如何启动这些扩展功能呢?

编译并指定开启扩展功能

在linux或mac下,进入源代码所在文件夹,输入以下编译指令:

gcc -O2 -I. \
   -DSQLITE_DEFAULT_WORKER_THREADS=4 \
   -DSQLITE_MAX_WORKER_THREADS=6 \
   -DSQLITE_ENABLE_FTS4 \
   -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 \
   -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
   -DHAVE_USLEEP -DHAVE_READLINE \
   shell.c sqlite3.c -ldl -lreadline -lncurses -lpthread -o sqlite3

注意,这条指令不是野论坛魔改指令,而是官网编译指南所提供的方法:https://www.sqlite.org/howtocompile.html

其中,O2 的速度优化参照【说明】【文档】,pthread优化参照【文档】,编译选项参照【文档】,编译出来1.7M左右的大小。

编译成功后,将sqlite3二进制文件复制到/usr/local/bin/ 下面就可以了。

针对Windows的编译区别

在win下,进入源码文件夹,使用MSVC的 cl 编译器(c-languange):

cl shell.c sqlite3.c -Fesqlite3.exe

 

 


讲技术,说人话。

Aurora 极光城

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章