非常教程

Sqlite参考手册

其他 | Miscellaneous

Indexes On Expressions

通常,SQL索引引用表的列。但是也可以在涉及表列的表达式上形成索引。

作为一个例子,请考虑下表追踪各种“账户”上的美元数量变化:

CREATE TABLE account_change(
  chng_id INTEGER PRIMARY KEY,
  acct_no INTEGER REFERENCES account,
  location INTEGER REFERENCES locations,
  amt INTEGER,  -- in cents
  authority TEXT,
  comment TEXT
);
CREATE INDEX acctchng_magnitude ON account_change(acct_no, abs(amt));

account_change表中的每个条目都将记录或提款记录到帐户中。存款有一个积极的“amt”,提款有一个负的“amt”。

acctchng_magnitude索引覆盖帐号(“acct_no”)和金额的绝对值。该索引允许人们对帐户变化的大小进行有效查询。例如,要列出帐号$ xyz超过$ 100.00的所有更改,可以这样说:

SELECT * FROM account_change WHERE acct_no=$xyz AND abs(amt)>=10000;

或者,要按大小递减的顺序列出对某个特定账户($ xyz)的所有更改,可以这样写:

SELECT * FROM account_change WHERE acct_no=$xyz
 ORDER BY abs(amt) DESC;

如果没有acctchng_magnitude索引,上述两个示例查询都可以正常工作。acctchng_magnitude索引索引仅帮助查询运行得更快,特别是对于每个帐户的表中有许多条目的数据库。

1.如何在表达式上使用索引

使用CREATE INDEX语句在一个或多个表达式上创建一个新索引,就像在列上创建索引一样。唯一的区别是表达式被列为要编入索引的元素而不是列名。

当被索引的表达式出现在查询的WHERE子句或ORDER BY子句中时,SQLite查询规划器将考虑在表达式上使用索引,就像在CREATE INDEX语句中写的一样。查询计划者不会做代数。为了将WHERE子句约束和ORDER BY项匹配到索引,SQLite要求表达式相同,除了小的语法差异(如空白变化)之外。所以如果你有:

CREATE TABLE t2(x,y,z);
CREATE INDEX t2xy ON t2(x+y);

然后你运行查询:

SELECT * FROM t2 WHERE y+x=22;

然后,索引将不会被使用,因为CREATE INDEX语句(x + y)上的表达式与它在查询(y + x)中出现的表达式不同。这两个表达式在数学上可能是等价的,但SQLite查询规划器坚持认为它们是相同的,而不仅仅是等价的。考虑重写这个查询:

SELECT * FROM t2 WHERE x+y=22;

第二个查询可能会使用索引,因为现在WHERE子句(x + y)中的表达式与索引中的表达式完全匹配。

2.限制

对CREATE INDEX语句中出现的表达式有一定的合理限制:

  1. CREATE INDEX语句中的表达式只能引用被索引表的列,而不能引用其他表中的列。
  1. CREATE INDEX语句中的表达式可能包含函数调用,但仅限于其输出总是完全由其输入参数(又名:确定性函数)确定的函数。很显然,像random()这样的函数在索引中不能很好地工作。但是,像sqlite_version()这样的函数,尽管它们在任何一个数据库连接中都是不变的,但它们在底层数据库文件的整个生命周期中并不是不变的,因此可能不会用在CREATE INDEX语句中。

请注意,应用程序定义的SQL函数默认情况下被视为非确定性的,并且可能不会在CREATE INDEX语句中使用,除非在函数注册时使用SQLITE_DETERMINISTIC标志。

  1. CREATE INDEX语句中的表达式可能不使用子查询。
  1. 表达式只能在CREATE INDEX语句中使用,而不能在CREATE TABLE语句中的UNIQUE或PRIMARY KEY约束中使用。

3.兼容性

SQLite 版本3.9.0(2015-10-14)添加了对表达式进行索引的功能。对表达式使用索引的数据库不能用于早期版本的SQLite。

 SQLite在公共领域。

其他 | Miscellaneous相关

1.35% Faster Than The Filesystem
2.8+3 Filenames
3.An Asynchronous I/O Module For SQLite
4.Appropriate Uses For SQLite
5.Architecture of SQLite
6.Atomic Commit In SQLite
7.Automatic Undo/Redo With SQLite
8.Benefits of SQLite As A File Format
9.Change in Default Page Size in SQLite Version 3.12.0
10.Clustered Indexes and the WITHOUT ROWID Optimization
11.Compile-time Options
12.Constraint Conflict Resolution in SQLite
13.Custom Builds Of SQLite
14.Deterministic SQL Functions
15.Distinctive Features Of SQLite
16.EXPLAIN QUERY PLAN
17.Features Of SQLite
18.File Format Changes in SQLite
19.Full-Featured SQL
20.High Reliability
21.Hints for Debugging SQLite
22.How SQLite Is Tested
23.How To Compile SQLite
24.How To Download Canonical SQLite Source Code
25.Imposter Tables
26.In-Memory Databases
27.Internal Versus External BLOBs
28.Isolation In SQLite
29.Long Term Support
30.Maintaining Private Branches Of SQLite
31.Many Small Queries Are Efficient In SQLite
32.Measuring and Reducing CPU Usage in SQLite
33.Memory-Mapped I/O
34.NULL Handling in SQLite
35.Partial Indexes
36.Pointer Passing Interfaces
37.Powersafe Overwrite
38.Release History Of SQLite
39.Result and Error Codes
40.Row Values
41.Rowid Tables
42.Run-Time Loadable Extensions
43.SQL Features That SQLite Does Not Implement
44.sqldiff.exe: Database Difference Utility
45.SQLite As An Application File Format
46.SQLite Autoincrement
47.SQLite Backup API
48.SQLite Changes From Version 3.4.2 To 3.5.0
49.SQLite Changes From Version 3.5.9 To 3.6.0
50.SQLite Database Speed Comparison
51.SQLite File IO Specification
52.SQLite Frequently Asked Questions
53.SQLite In 5 Minutes Or Less
54.SQLite is a Self Contained System
55.SQLite Is Serverless
56.SQLite Is Transactional
57.SQLite Library Footprint
58.SQLite Shared-Cache Mode
59.SQLite Unlock-Notify API
60.SQLite Version 3 Overview
61.SQLite: Single File Database
62.Temporary Files Used By SQLite
63.TH3
64.The COMPLETION() Table-Valued Function
65.The CSV Virtual Table
66.The dbhash.exe Utility Program
67.The DBSTAT Virtual Table
68.The Error And Warning Log
69.The generate_series Table-Valued Function
70.The OS Backend (VFS) To SQLite
71.The Spellfix1 Virtual Table
72.The SQLite Amalgamation
73.The SQLite Bytecode Engine
74.The sqlite3_analyzer.exe Utility Program
75.The SQLITE_STMT Virtual Table
76.The UNION Virtual Table
77.The Virtual Database Engine of SQLite
78.Uniform Resource Identifiers
79.Using SQLite In Multi-Threaded Applications
80.Version Numbers in SQLite
81.What If OpenDocument Used SQLite?
82.Why Is SQLite Coded In C
83.Zero-Configuration
Sqlite

SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来

主页 https://sqlite.org/
源码 https://www.sqlite.org/src/
发布版本 3.21.0

Sqlite目录

1.C界面 | C Interface
2.C Interface: Session Module
3.CLI
4.数据库文件表 | Database File Format
5.数据类 | Datatypes
6.动态内存分配 | Dynamic Memory Allocation
7.外键约束 | Foreign Key Constraints
8.全文索引 | Full-Text Search
9.损坏方式 | How To Corrupt
10.JSON
11.语言 | Language
12.局限性 | Limits
13.锁定和并发 | Locking and Concurrency
14.其他 | Miscellaneous
15.PRAGMA Statements
16.查询计划程序 | Query Planner
17.R*Tree Module
18.RBU Extension
19.语法图 | Syntax Diagrams
20.Tcl Interface
21.虚拟表机制 | Virtual Table Mechanism
22.预写日志 | Write-Ahead Logging
23.SQL 教程
24.SQL 简介
25.SQL 语法
26.SQL DELETE 语句
27.SQL UPDATE 语句
28.SQL NOT NULL 约束
29.SQL 约束
30.SQL CREATE TABLE 语句
31.SQL CREATE DATABASE 语句
32.SQL INSERT INTO SELECT 语句
33.SQL SELECT INTO 语句
34.SQL CREATE VIEW、REPLACE VIEW、 DROP VIEW 语句
35.SQL AUTO INCREMENT 字段
36.SQL ALTER TABLE 语句
37.SQL 撤销索引、表以及数据库
38.SQL CREATE INDEX 语句
39.SQL DEFAULT 约束
40.SQL CHECK 约束
41.SQL FOREIGN KEY 约束
42.SQL PRIMARY KEY 约束
43.SQL UNIQUE 约束
44.SQL 通用数据类型
45.SQL ISNULL()、NVL()、IFNULL() 和 COALESCE() 函数
46.SQL NULL 值 – IS NULL 和 IS NOT NULL
47.SQL Server 和 MySQL 中的 Date 函数
48.SQL MS Access、MySQL 和 SQL Server 数据类型
49.SQL 函数
50.SQL 总结
51.SQL 主机
52.SQL 快速参考
53.SQL ROUND() 函数
54.SQL Server GETDATE() 函数
55.MySQL DATE_FORMAT() 函数
56.MySQL DATEDIFF() 函数
57.MySQL DATE_SUB() 函数
58.MySQL DATE_ADD() 函数
59.MySQL EXTRACT() 函数
60.MySQL DATE() 函数
61.MySQL CURTIME() 函数
62.MySQL CURDATE() 函数
63.MySQL NOW() 函数
64.SQL Server CONVERT() 函数
65.SQL Server DATEDIFF() 函数
66.SQL Server DATEADD() 函数
67.SQL Server DATEPART() 函数
68.SQLite 命令
69.SQLite 安装
70.SQLite 简介
71.SQLite 运算符
72.SQLite Select 语句
73.SQLite 删除表
74.SQLite 创建表
75.SQLite Insert 语句
76.SQLite 分离数据库
77.SQLite 附加数据库
78.SQLite 创建数据库
79.SQLite 数据类型
80.SQLite 语法
81.SQLite Order By
82.SQLite Limit 子句
83.SQLite Glob 子句
84.SQLite Like 子句
85.SQLite Delete 语句
86.SQLite Update 语句
87.SQLite AND/OR 运算符
88.SQLite Where 子句
89.SQLite 表达式
90.SQLite Distinct 关键字
91.SQLite Having 子句
92.SQLite Group By
93.SQLite Join
94.SQLite 约束
95.SQLite PRAGMA
96.SQLite 事务
97.SQLite 视图
98.SQLite Truncate Table
99.SQLite Alter 命令
100.SQLite Indexed By