|
SQLite与大多数的SQL数据库都不同,因为SQLite的核心设计目标是简洁易用:
SQLite is different from most other SQL database engines in that its
primary design goal is to be simple:
很多人喜欢SQLite的原因都是它小巧而快速。但是这些特性不是最主要的。用户会发现SQLite非常的可靠,这是SQLite推崇简约的结果。减少复杂度就减少了出错的概率。所以,SQLite是小巧的,快速的,可靠的,不过最最重要的还是SQLite追求的简约。
Many people like SQLite because it is small and fast. But those
qualities are just happy accidents.
Users also find that SQLite is very reliable. Reliability is
a consequence of simplicity. With less complication, there is
less to go wrong. So, yes, SQLite is small, fast, and reliable,
but first and foremost, SQLite strives to be simple.
一个简约的数据库引擎既能展现出强大,也能表现出无力,这主要取决于让其做什么。为了让SQLite更加的简约,SQLite牺牲了很多一些人认为很有用的特性,比如高并发,细粒度访问控制,丰富的内置函数,存储过程,很少使用的SQL语言特性,XML与Java扩展,T或者P级别的容量扩展等等。如果你需要这些特性而不在意因此而增加的复杂性,那么SQLite可能就不合适于你了。SQLite不准备成为一个企业数据库,也不是为何Oracle或者PostgreSQL竞争而设计的。
Simplicity in a database engine can be either a strength or a
weakness, depending on what you are trying to do. In order to
achieve simplicity, SQLite has had to sacrifice other characteristics
that some people find useful, such as high concurrency, fine-grained
access control, a rich set of built-in functions, stored procedures,
esoteric SQL language features, XML and/or Java extensions,
tera- or peta-byte scalability, and so forth. If you need some of these
features and do not mind the added complexity that they
bring, then SQLite is probably not the database for you.
SQLite is not intended to be an enterprise database engine. It is
not designed to compete with Oracle or PostgreSQL.
判断是否适合使用SQLite的级别原则就是:当易于管理,易于实现,易于维护比那些企业数据库提供的大量复杂特性重要时,SQLite就是一个好的选择。事实证明,简约优于复杂的情况要远比很多人想象的要多的多。
The basic rule of thumb for when it is appropriate to use SQLite is
this: Use SQLite in situations where simplicity of administration,
implementation, and maintenance are more important than the countless
complex features that enterprise database engines provide.
As it turns out, situations where simplicity is the better choice
are more common than many people realize.
换一种方式来看待SQLite:SQLite不是用来替代Oracle的,而是用来替代fopen()的。
Another way to look at SQLite is this: SQLite is not designed
to replace Oracle.
It is designed to replace fopen().
应用文件格式
Application File Format
(更多详细内容)
(Further details here.)
将SQLite作为桌面应用的磁盘文件格式是一个非常好的选择,例如版本控制系统,金融分析工具,媒体目录与编辑套件,CAD包,记录程序信息等等。传统的文件打开操作通过调用sqlite3_open()方法来完成数据库文件的数据追加,当应用内容发生改变时自动更新数据库,这样文件/保存功能就变得多余了。而文件/另存为功能则可以使用备份 API来实现。
SQLite has been used with great success as the on-disk file format
for desktop applications such as version control systems,
financial analysis tools, media cataloging and editing suites, CAD
packages, record keeping programs, and so forth. The traditional
File/Open operation calls sqlite3_open() to attach to the database
file. Updates happen atomically as application content is revised
so the File/Save menu option become superfluous. The File/Save_As
menu option can be implemented using the backup API.
使用SQLite作为应用的文件格式有很多的优点,包括:
There are many advantages to using SQLite as an application file format,
including:
SQLite的数据文件支持任意的扩展名,所以应用程序如果需要的话可以自定义文件扩展名。application_id pragma可以用来在数据库文件中设置一个 "Application ID",这样,类似file(1)的工具就可以判断出该文件是一个应用专属的文件,而不是一个通用SQL数据库。
SQLite allows database files to have any desired filename extension, so
an application can choose a custom filename extension for its own use, if
desired. The application_id pragma can be used to set an "Application ID"
integer in the database file so that tools like
file(1) can determine that the file
is associated with your application and is not just a generic
SQL database.
嵌入式设备和应用
Embedded devices and applications
由于SQLite数据库的管理需求非常少,甚至完全不需要,因此SQLite非常适合无人值守的设备或服务。SQLite也非常适合在手机,PDA,机顶盒,家电上使用。还适合作为用户下载应用的嵌入式数据库。
Because an SQLite database requires little or no administration,
SQLite is a good choice for devices or services that must work
unattended and without human support. SQLite is a good fit for
use in cellphones, PDAs, set-top boxes, and/or appliances. It also
works well as an embedded database in downloadable consumer applications.
网站
Websites
SQLite适合做中小型网站(可以说是99.9%的网站)的数据库引擎。当然,SQLite可以处理多少的网页请求依赖于网站使用数据库的压力。通常来说,每天少于10万次点击的网站都可以放心的使用SQLite。每天10万次只是一个保守的估计,而不是准确的上限。SQLite的示例可以在10倍的流量下正常工作。
SQLite usually will work great as the database engine for low to
medium traffic websites (which is to say, 99.9% of all websites).
The amount of web traffic that SQLite can handle depends, of course,
on how heavily the website uses its database. Generally
speaking, any site that gets fewer than 100K hits/day should work
fine with SQLite.
The 100K hits/day figure is a conservative estimate, not a
hard upper bound.
SQLite has been demonstrated to work with 10 times that amount
of traffic.
替换临时磁盘文件
Replacement for ad hoc disk files
许多程序使用fopen()、
fread() 和
fwrite()来创建和管理自有格式的数据文件。SQLite可以很好的替代这些临时数据文件。
Many programs use
fopen(),
fread(), and
fwrite() to create and
manage files of data in home-grown formats. SQLite works
particularly well as a
replacement for these ad hoc data files.
内部数据库或临时数据库
Internal or temporary databases
如果程序需要对大量数据做筛选和排序操作,通常可以简单的将数据加载到SQLite的内存数据库中,然后使用查询语句、join和ORDIER BY子句来提取和排序数据,而不是尝试人工实现同样操作的代码。在程序内部这样使用SQL数据库可以给程序带来巨大的灵活性,例如增加一个新列或者索引不需要修改一行查询。
For programs that have a lot of data that must be sifted and sorted
in diverse ways, it is often easier and quicker to load the data into
an in-memory SQLite database and use queries with joins and ORDER BY
clauses to extract the data in the form and order needed rather than
to try to code the same operations manually.
Using an SQL database internally in this way also gives the program
greater flexibility since new columns and indices can be added without
having to recode every query.
命令行数据集分析工具
Command-line dataset analysis tool
熟练的SQL用户会使用sqlite3命令行工具来分析各种数据集。CSV文件中的原始数据可以导入数据库中,然后对数据进行细分来生产各种各样的概要报表。可以应用到网站日志分析,动态统计分析,处理程序指标,以及分析实验数据。
Experienced SQL users can equivmploy
the command-line sqlite3 program to analyze miscellaneous
datasets. Raw data can be imported from CSV files, then that
data can be sliced and diced to generate a myriad of summary
reports. Possible uses include website log analysis, sports
statistics analysis, compilation of programming metrics, and
analysis of experimental results.
当然在企业数据库中也可以做同样的事儿。这种情况下,SQLite的优势在于其非常易于配置,并且只产生一个数据库文件,因此你可以存储到软盘或者闪存盘中,或者通过邮件发给同事。
You can also do the same thing with an enterprise client/server
database, of course. The advantages to using SQLite in this situation
are that SQLite is much easier to set up and the resulting database
is a single file that you can store on a floppy disk or flash-memory stick
or email to a colleague.
在做演示或者测试时替代企业数据库
Stand-in for an enterprise database during demos or testing
如果你在为一个企业数据库编写客户端应用,使用一个可以连接不同类型SQL数据库的通用数据库客户端是非常有必要的。可以在开始开发时将SQLite连入客户端程序中,通过这种方法,客户端程序就可以和SQLite一起独立运行了来完成测试或者演示。
If you are writing a client application for an enterprise database engine,
it makes sense to use a generic database backend that allows you to connect
to many different kinds of SQL database engines. It makes even better sense to go ahead and include SQLite in the mix of supported databases and to statically link the SQLite engine in with the client. That way the client program
can be used standalone with an SQLite data file for testing or for
demonstrations.
数据库教学
Database Pedagogy
由于SQLite非常容易安装和使用(安装几乎可以忽略:只需要将sqlite3 或者 sqlite3.exe可执行文件复制到目标机器上并运行即可),所以它非常适合于SQL教学。学生可以简单的创建多个数据库并可以通过将数据库发送给导师评分。对于想学习RDBMS的实现的学生来说SQLite可以充当一个非常好的起点,因为SQLite代码是模块化的并且有优良的注释和文档。这并不是说SQLite是其它数据库实现的精确模型,但是但是如果一个学生已经理解了SQLite是如何工作的,那么就可以轻易的理解其它系统是的运行原理了。
Because it is simple to setup and use (installation is trivial: just
copy the sqlite3 or sqlite3.exe executable to the target machine
and run it) SQLite makes a good database engine for use in teaching SQL.
Students can easily create as many databases as they like and can
email databases to the instructor for comments or grading. For more
advanced students who are interested in studying how an RDBMS is
implemented, the modular and well-commented and documented SQLite code
can serve as a good basis. This is not to say that SQLite is an accurate
model of how other database engines are implemented, but rather a student who
understands how SQLite works can more quickly comprehend the operational
principles of other systems.
SQL语言扩展试验
Experimental SQL language extensions
SQLite简洁和模块化的设计使其成为了新原型设计和数据库语言特性与创意的理想测试平台。
The simple, modular design of SQLite makes it a good platform for
prototyping new, experimental database language features or ideas.
客户端/服务端 应用
Client/Server Applications
当你有很多客户端程序都需要通过网络访问同一个数据库时你就需要考虑使用 客户端/服务端型的数据库来替换SQLite了。虽然SQLite可以运行在一个网络文件系统上,但是由于和大多数网络文件系统都有潜在的联系,所以性能往往不会很好。并且许多网络文件系统实现的文件锁逻辑都存在bug(包括Unix和Windows)。如果文件锁出现问题,那么就可能出现两个以上的客户端程序同时修改同一个数据库上的同一份数据,结果就是导致数据库被破坏。由于这些问题都是源于文件系统的实现,所以SQLite无法提供任何的保证。
If you have many client programs accessing a common database
over a network, you should consider using a client/server database
engine instead of SQLite. SQLite will work over a network filesystem,
but because of the latency associated with most network filesystems,
performance will not be great. Also, the file locking logic of
many network filesystems implementation contains bugs (on both Unix
and Windows). If file locking does not work like it should,
it might be possible for two or more client programs to modify the
same part of the same database at the same time, resulting in
database corruption. Because this problem results from bugs in
the underlying filesystem implementation, there is nothing SQLite
can do to prevent it.
一个原则就是,避免多个计算机通过网络文件系统使用SQLite。
A good rule of thumb is that you should avoid using SQLite
in situations where the same database will be accessed simultaneously
from many computers over a network filesystem.
大容量网站
High-volume Websites
SQLite通常可以很好的支撑起一个网站。但是如果你的网站业务繁忙,以至于你需要将数据库拆分到不同的机器上时,你就需要考虑使用一个企业级的客户端/服务的数据库引擎来替换SQLite了。
SQLite will normally work fine as the database backend to a website.
But if you website is so busy that you are thinking of splitting the
database component off onto a separate machine, then you should
definitely consider using an enterprise-class client/server database
engine instead of SQLite.
巨大的数据集
Very large datasets
SQLite数据库的大小被限制在140TB(247 字节, 128 TB)以下。即使能够处理这么大的数据库,大多数文件系统也会将单个数据文件的大小限制在这以下。所以,当你的数据库达到这个数量级是就需要注意了,需要考虑使用一个客户端/服务的数据库引擎将数据分散到多个磁盘文件中,甚至分散到不同的磁盘中。
An SQLite database is limited in size to 140 terabytes
(247 bytes, 128 tibibytes).
And even if it could handle larger databases, SQLite stores the entire
database in a single disk file and many filesystems limit the maximum
size of files to something less than this. So if you are contemplating
databases of this magnitude, you would do well to consider using a
client/server database engine that spreads its content across multiple
disk files, and perhaps across multiple volumes.
高并发
High Concurrency
SQLite没有限制同时读取的客户端数,但是同一时刻只能有一个客户端写数据。多数情况下,这都不是问题。每个应用都可以在数据库上快速的执行,最多等待十多毫秒。但是有些应用需要很高的并发,这时就需要寻找其它的解决方案了。
SQLite supports an unlimited number of simultaneous readers, but it
will only allow one writer at any instant in time.
For many situations, this is not a problem. Each application
does its database work quickly and moves on, and no lock lasts for more
than a few dozen milliseconds. But there are some applications that require
more concurrency, and those applications may need to seek a different
solution.