设为首页 收藏本站
查看: 957|回复: 0

[经验分享] SQLite源码剖析(1)

[复制链接]

尚未签到

发表于 2016-11-29 08:16:30 | 显示全部楼层 |阅读模式
  声明:本SQLite源码剖析系列为刘兴(http://deepfuture.iyunv.com/)原创,未经笔者授权,任何人和机构不能转载
  什么是 sqlite ?

sqlite 是一款轻量级的、基于文件的嵌入式数据库,2000年就已经诞生,经过7年多的发展,直到今天已经成为最流行的嵌入式数据库,包括google在内的公司在其桌面软件中亦使用 sqlite 存储用户数据。由此可以看出,已经没有任何理由去怀疑sqlite的稳定性了。


sqlite的优势?

1. 免配置,和access一样,只要把数据库文件通过ftp上传到服务器上就可以使用,不需要服务器的额外支持

2. 备份方便,因为只是一个文件,只要复制一份该文件,就能备份整个数据库

3. 虽然是轻量级数据库,但他支持最大 2tb 的单个库文件。

4. 快,无与伦比的快。经过实际测试,在几百万记录的情况下,sqlite的插入和查询速度和 mysql 不分上下,快于 sql server,10倍于 access (但这并不意味着它可以替代 sql server )
SQLite源码下载 

    在SQLITE主页上下载源码,选择右边出现以下字样的进行下载


DSC0000.bmp
 

 

 

  源码分析
  我们先分析主程序sqlite3.c
  /******************************************************************************
  ** This file is an amalgamation of many separate C source files from SQLite
  ** version 3.6.23.1.  By combining all the individual C code files into this
  ** single large file, the entire code can be compiled as a one translation
  ** unit.  This allows many compilers to do optimizations that would not be
  ** possible if the files were compiled separately.  Performance improvements
  ** of 5% are more are commonly seen when SQLite is compiled as a single
  ** translation unit.
  **
  ** This file is all you need to compile SQLite.  To use SQLite in other
  ** programs, you need this file and the "sqlite3.h" header file that defines
  ** the programming interface to the SQLite library.  (If you do not have
  ** the "sqlite3.h" header file at hand, you will find a copy embedded within
  ** the text of this file.  Search for "Begin file sqlite3.h" to find the start
  ** of the embedded sqlite3.h header file.) Additional code files may be needed
  ** if you want a wrapper to interface SQLite with your choice of programming
  ** language. The code for the "sqlite3" command-line shell is also in a
  ** separate file. This file contains only code for the core SQLite library.
  */
  //主程序定义SQLITE核心:define SQLITE_CORE 1
  //主程序定义SQLITE_API。
  //主程序序版本为合并后的源代码:define SQLITE_AMALGAMATION 1
  #define SQLITE_CORE 1
  #define SQLITE_AMALGAMATION 1
  #ifndef SQLITE_PRIVATE
  # define SQLITE_PRIVATE static
  #endif
  #ifndef SQLITE_API
  # define SQLITE_API
  #endif
  /************** Begin file sqliteInt.h ***************************************/
  /*
  ** 2001 September 15
  **
  ** The author disclaims copyright to this source code.  In place of
  ** a legal notice, here is a blessing:
  **
  **    May you do good and not evil.
  **    May you find forgiveness for yourself and forgive others.
  **    May you share freely, never taking more than you give.
  **
  *************************************************************************
  ** Internal interface definitions for SQLite.
  **
  */
  //SQLITE内部接口定义
  #ifndef _SQLITEINT_H_
  #define _SQLITEINT_H_
  //如果基础操作系统支持,可用#define开启大于2G的单个文件支持。
  /*
  ** These #defines should enable >2GB file support on POSIX if the
  ** underlying operating system supports it.  If the OS lacks
  ** large file support, or if the OS is windows, these should be no-ops.
  **_LARGEFILE_SOURCE宏必须在任何#include前使用
  ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
  ** system #includes.  Hence, this block of code must be the very first
  ** code in all source files.
  **在编译命令行使用-DSQLITE_DISABLE_LFS可禁止大文件支持,
  ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
  ** on the compiler command line.  This is necessary if you are compiling
  ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
  ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
  ** without this option, LFS is enable.  But LFS does not exist in the kernel
  ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
  ** portability you should omit LFS.
  **
  ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
  */
  //如果没禁止大文件支持,则定义相关常量
  #ifndef SQLITE_DISABLE_LFS
  # define _LARGE_FILE       1
  # ifndef _FILE_OFFSET_BITS
  #   define _FILE_OFFSET_BITS 64
  # endif
  # define _LARGEFILE_SOURCE 1
  #endif
   
  /*
  ** Include the configuration header output by 'configure' if we're using the
  ** autoconf-based build
  */
  //如果使用autoconf-based构建,则include "config.h"
  #ifdef _HAVE_SQLITE_CONFIG_H
  #include "config.h"
  #endif
   
  /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
  /************** Begin file sqliteLimit.h *************************************/
  /*
  ** 2007 May 7
  **
  ** The author disclaims copyright to this source code.  In place of
  ** a legal notice, here is a blessing:
  **
  **    May you do good and not evil.
  **    May you find forgiveness for yourself and forgive others.
  **    May you share freely, never taking more than you give.
  **
  *************************************************************************
  **
  ** This file defines various limits of what SQLite can process.
  */
   
  /*
  ** The maximum length of a TEXT or BLOB in bytes.   This also
  ** limits the size of a row in a table or index.
  **
  ** The hard limit is the ability of a 32-bit signed integer
  ** to count the size: 2^31-1 or 2147483647.
  */
  //最大32位有符号整数
  #ifndef SQLITE_MAX_LENGTH
  # define SQLITE_MAX_LENGTH 1000000000
  #endif
   
  /*
  ** This is the maximum number of
  **以下这些项的最大值:
  **表中的列、索引中的列、视图的列、update的set从句的数量、select
  **返回的字段数、GROUP BY 和ORDER BY的字段数、INSERT的values从句
  **    * Columns in a table
  **    * Columns in an index
  **    * Columns in a view
  **    * Terms in the SET clause of an UPDATE statement
  **    * Terms in the result set of a SELECT statement
  **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
  **    * Terms in the VALUES clause of an INSERT statement
  **
  ** The hard upper limit here is 32676.  Most database people will
  ** tell you that in a well-normalized database, you usually should
  ** not have more than a dozen or so columns in any table.  And if
  ** that is the case, there is no point in having more than a few
  ** dozen values in any of the other situations described above.
  */
  /*
  **以下这些项的最大值:
  **表中的列、索引中的列、视图的列、update的set从句的数量、select
  **返回的字段数、GROUP BY 和ORDER BY的字段数、INSERT的values从句
  */
  #ifndef SQLITE_MAX_COLUMN
  # define SQLITE_MAX_COLUMN 2000
  #endif
   
  /*
  ** The maximum length of a single SQL statement in bytes.
  **
  ** It used to be the case that setting this value to zero would
  ** turn the limit off.  That is no longer true.  It is not possible
  ** to turn this limit off.
  */
  //SQL语句的最大长度
  #ifndef SQLITE_MAX_SQL_LENGTH
  # define SQLITE_MAX_SQL_LENGTH 1000000000
  #endif
   
  /*
  ** The maximum depth of an expression tree. This is limited to
  ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
  ** want to place more severe limits on the complexity of an
  ** expression.
  **
  ** A value of 0 used to mean that the limit was not enforced.
  ** But that is no longer true.  The limit is now strictly enforced
  ** at all times.
  */
  //解释树的最大深度
  #ifndef SQLITE_MAX_EXPR_DEPTH
  # define SQLITE_MAX_EXPR_DEPTH 1000
  #endif
   
  /*
  ** The maximum number of terms in a compound SELECT statement.
  ** The code generator for compound SELECT statements does one
  ** level of recursion for each term.  A stack overflow can result
  ** if the number of terms is too large.  In practice, most SQL
  ** never has more than 3 or 4 terms.  Use a value of 0 to disable
  ** any limit on the number of terms in a compount SELECT.
  */
  //复合SQL语句的最大项数
  #ifndef SQLITE_MAX_COMPOUND_SELECT
  # define SQLITE_MAX_COMPOUND_SELECT 500
  #endif

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306890-1-1.html 上篇帖子: difference between VARCHAR NVARCHAR in SQLite 下篇帖子: SQLite 时间函数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表