SQLite is a lightweight database, an ACID-compliant relational database management system, contained in a relatively small C library. It is a public domain project established by D.RichardHipp. It is designed to be embedded, and it has been used in many embedded products. It occupies very low resources. In embedded devices, only a few hundred K of memory may be enough. It can support mainstream operating systems such as Windows/Linux/Unix, and can be combined with many programming languages, such as Java, C#, PHP, Tcl, etc., and has an ODBC interface. Compared with Mysql and PostgreSQL, the two open source worlds As far as the famous database management system is concerned, its processing speed is faster than them. The first Alpha version of SQLite was born in May 2000, and it has been 16 years since 2016. SQLite also ushered in a new version SQLite3, which has been released.
SQLite data type
Every value stored in an SQLite database has one of the following storage types:
storage type | describe |
NULL | Value is a NULL value. |
INTEGER | The value is a signed integer stored in 1, 2, 3, 4, 6 or 8 bytes depending on the size of the value |
REAL | Value is a floating-point value stored as an 8-byte IEEE floating-point number |
TEXT | The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE) |
BLOB | Value is a blob of data that flex stores according to its input |
Basic SQL syntax
DDL Data Definition Language
Order | describe |
CREATE | Create a new table , view of a table or other object in the database |
ALTER | Modify an existing database object in the database, such as a table |
DROP | Drop an entire table , or a view of a table, or other object in the database |
DML Database Manipulation Language
Order | describe |
INSERT | Insert a record into the table |
UPDATE | modification record |
DELETE | Delete Record |
DQL data query language
Order | describe |
SELECT | retrieve certain records from one or more tables |
SQLite Optimization (Interview)
index
Simply put, an index is a pointer to data in a table. An index in a database is very similar to an index in a book. Take the catalog page (index) of the Chinese dictionary as an example, we can quickly find the desired word in the catalog (index) sorted by pinyin, strokes, radicals, etc.
Advantages: greatly speed up the database retrieval, including single-table query, linked-table query, group query, and sort query. Often an order of magnitude or two in performance improvement, growing by orders of magnitude with the data.
Disadvantages: The creation and maintenance of the index consumes, and the index takes up physical space, which increases with the increase of the data volume. Indexes need to be maintained when adding, deleting, and modifying the database, which will affect the performance of adding, deleting, and modifying.
index classification
Directly create index: use sql statement to create, in Android, you can directly excuSql create statement in SQLiteOpenHelper's onCreate or onUpgrade.
create index id_index on user(id)
Unique index: ensure that all data in the index column is unique, and can be used for both clustered and non-clustered indexes
create unique index id_index on user(id);
Single index: Only a single field is included in the index creation statement , such as the ordinary index and unique index creation examples above
Composite index: also known as composite index, which includes multiple fields in the index creation statement at the same time
create index cx_index on user(id, name);
scenes to be used
It is recommended to use an index when the data update frequency of a certain field is low, the query frequency is high, and range queries (>, <, =, >=, <=) or order by, group by often occur. And the greater the selectivity, the more advantageous it is to build an index. Here, the selectivity refers to the number of unique values in a field/total number.
Multiple columns are often accessed at the same time, and each column contains repeated values. Consider building a composite index.
Index Usage Rules
For compound indexes, the most frequently used column is used as the leading column (the first field in the index). If the leading column is not in the query condition when querying, the composite index will not be used.
create index complex_index on user(id, name);
select * from user where id > 3; --使用了索引
select * from user where name like 'z*'; --未使用索引
--检验是否使用了索引
sqlite> explain query plan select * from user where id > 3;
QUERY PLAN
--SEARCH TABLE user USING INDEX sqlite_autoindex_user_1 (id>?)
Run Time: real 0.000 user 0.000000 sys 0.000000
sqlite> explain query plan select * from user where name like 'z*'
QUERY PLAN
--SCAN TABLE user
Run Time: real 0.001 user 0.000000 sys 0.000000
Avoid calculating the index column. If any calculation on the where clause column cannot be compiled and optimized, it will cause the index to fail when querying.
select * from user where cast(id as char) = '3';
--检验
explain query plan select * from user where cast(id as char) = '3';
QUERY PLAN
--SCAN TABLE user
Run Time: real 0.001 user 0.000000 sys 0.000000
Compare values to avoid NULL
When querying multiple tables, pay attention to selecting the appropriate table as the inner table. The connection conditions should fully consider tables with indexes, tables with a large number of rows, and the selection of inner and outer tables can be determined by the formula: the number of matching rows in the outer table * the number of times each lookup in the inner table, and the smallest product is the best solution . Before the actual multi-table operation is actually executed, the query optimizer will list several groups of possible connection schemes according to the connection conditions and find the best scheme with the least system overhead.
Query columns are in the same order as index columns
Replace the EXISTS clause with a multi-table join
use transaction