非常教程

Sqlite参考手册

其他 | Miscellaneous

The DBSTAT Virtual Table

1.概述

DBSTAT 虚拟表是只读的同名虚拟表,它返回有关架构中哪些表和索引使用数据库文件的哪些页面的信息。DBSTAT 虚表是用来实现 sqlite3_analyzer.exe 应用程序,并帮助计算表大小饼图在化石实现 SQLite 的版本控制系统。

dbstat 当 SQLite 是使用 SQLITE_ENABLE_DBSTAT_VTAB 编译时选择建立的虚拟表可在所有的数据库连接。dbstat 虚拟表提供有关数据库文件中 btree 和溢出页面的底层信息。

dbstat 虚拟表是一个同名的虚拟表,这意味着在使用 CREATE VIRTUAL TABLE 来创建 dbstat 虚拟表的实例之前,不需要使用CREATE VIRTUAL TABLE。可以像使用表名直接查询 dbstat 虚拟表一样使用 “dbstat” 模块名称。例如:

SELECT * FROM dbstat;

如果需要使用 dbstat 模块的命名虚拟表,那么建议创建 dbstat 虚拟表实例的方法如下:

CREATE VIRTUAL TABLE temp.stat USING dbstat(main);

注意“温度”。虚拟表名称(“stat”)之前的限定符。该限定符会导致虚拟表是临时的 - 仅在当前数据库连接期间存在。这是推荐的方法。

The "main" argument to dbstat is default schema for which information is to be provided. The default is "main", and so the use of "main" in the example above is redundant. For any particular query, the schema can be changed by specifying the alternative schema as a function argument to the virtual table name in the FROM clause of the query. (See further discussion of table-valued functions in the FROM clause for more details.)

dbstat 虚拟表的模式是这样的:

CREATE TABLE dbstat(
  name       TEXT,        -- Name of table or index
  path       TEXT,        -- Path to page from root
  pageno     INTEGER,     -- Page number
  pagetype   TEXT,        -- 'internal', 'leaf' or 'overflow'
  ncell      INTEGER,     -- Cells on page (0 for overflow)
  payload    INTEGER,     -- Bytes of payload on this page
  unused     INTEGER,     -- Bytes of unused space on this page
  mx_payload INTEGER,     -- Largest payload size of all cells on this page
  pgoffset   INTEGER,     -- Offset of page in file
  pgsize     INTEGER,     -- Size of the page
  schema     TEXT HIDDEN  -- Database schema being analyzed
);

数据库文件中的每个页面都有一行 dbstat 表。数据库文件的 Freelist 页面,锁页面和指针映射页面不会出现在 dbstat 虚拟表中。

2. dbstat 虚拟表的“路径”列

“路径”列描述了从 btree 结构的根节点到每个页面的路径。根节点本身的“路径”是“/”。btree 页面根目录的最左侧子页面的“路径”是“/ 000 /”。(Btree 存储的内容从左到右排列,因此左侧的页面比右侧的页面小。)根页面的最左边的孩子的下一个是'/ 001',依此类推,每个兄弟页面由3位十六进制值标识。第451个最左边的兄弟姐妹的孩子具有诸如 '/ 1c2 / 000 /,'/ 1c2 / 001 /'等的路径。通过向路径附加“+”字符和六位十六进制值来指定溢出页面到他们链接的单元格。例如,

'/1c2/000+000000'         // First page in overflow chain
'/1c2/000+000001'         // Second page in overflow chain
'/1c2/000+000002'         // Third page in overflow chain

如果路径使用 BINARY 整理序列进行排序,则与单元格关联的溢出页面将按排序顺序比它的子页面更早出现:

'/1c2/000/'               // Left-most child of 451st child of root

3.使用 dbstat 虚拟表的示例

要查找架构 “aux1” 中用于存储表 “xyz” 的页面总数,请使用:

SELECT count(*) FROM dbstat('aux1') WHERE name='xyz';

要查看表的内容在磁盘上的存储效率如何,请计算用于容纳实际内容的空间量除以所用磁盘空间总量。这个数字越接近100%,包装效率越高。(在这个例子中,'xyz' 表被假定为'主'模式。)

SELECT sum(pgsize-unused)/sum(pgsize) FROM dbstat WHERE name='xyz';

要查找表格的平均扇出,请运行:

SELECT avg(ncell) FROM dbstat WHERE name='xyz' AND pagetype='internal';

当磁盘访问是连续的时,现代文件系统运行得更快。因此,如果数据库文件的内容位于顺序页面上,SQLite 将运行得更快。要找出数据库中页面的哪一部分是顺序的(从而获得可能有助于确定何时进行 VACUUM 的测量),请运行如下所示的查询:

CREATE TEMP TABLE s(rowid INTEGER PRIMARY KEY, pageno INT);
INSERT INTO s(pageno) SELECT pageno FROM dbstat ORDER BY path;
SELECT sum(s1.pageno+1==s2.pageno)*1.0/count(*)
  FROM s AS s1, s AS s2
 WHERE s1.rowid+1=s2.rowid;
DROP TABLE s;

其他 | 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.Indexes On Expressions
28.Internal Versus External BLOBs
29.Isolation In SQLite
30.Long Term Support
31.Maintaining Private Branches Of SQLite
32.Many Small Queries Are Efficient In SQLite
33.Measuring and Reducing CPU Usage in SQLite
34.Memory-Mapped I/O
35.NULL Handling in SQLite
36.Partial Indexes
37.Pointer Passing Interfaces
38.Powersafe Overwrite
39.Release History Of SQLite
40.Result and Error Codes
41.Row Values
42.Rowid Tables
43.Run-Time Loadable Extensions
44.SQL Features That SQLite Does Not Implement
45.sqldiff.exe: Database Difference Utility
46.SQLite As An Application File Format
47.SQLite Autoincrement
48.SQLite Backup API
49.SQLite Changes From Version 3.4.2 To 3.5.0
50.SQLite Changes From Version 3.5.9 To 3.6.0
51.SQLite Database Speed Comparison
52.SQLite File IO Specification
53.SQLite Frequently Asked Questions
54.SQLite In 5 Minutes Or Less
55.SQLite is a Self Contained System
56.SQLite Is Serverless
57.SQLite Is Transactional
58.SQLite Library Footprint
59.SQLite Shared-Cache Mode
60.SQLite Unlock-Notify API
61.SQLite Version 3 Overview
62.SQLite: Single File Database
63.Temporary Files Used By SQLite
64.TH3
65.The COMPLETION() Table-Valued Function
66.The CSV Virtual Table
67.The dbhash.exe Utility Program
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