当前位置:网站首页>Why does wechat use SQLite to save chat records?
Why does wechat use SQLite to save chat records?
2022-07-06 18:13:00 【Program ape DD_】
SQLite It is a database underestimated by everyone , But some people think it is a toy database that is not suitable for production environment . in fact ,SQLite Is a very reliable database , It can handle TB Level of data , But it has no network layer . Next , This article will discuss with you SQLite The latest in the past year SQL function .
SQLite “ It's just ” A library , It is not a traditional server . therefore , In some cases , It's really inappropriate . however , On quite a few other occasions , It is the most suitable choice .SQLite It claims to be the most widely deployed and used database engine . I think it's very possible , because SQLite There are no copyright restrictions . No matter when , As long as developers want to use SQL Store structured data in files ,SQLite Should be the preferred solution .
SQLite Of SQL Dialects are also very powerful . It is better than MySQL Four years ago, I began to support with sentence . lately , It also implements support for window functions , It's just better than MySQL Five months late .
Next , This article will introduce SQLite stay 2018 Newly added in SQL function , That is to say SQLite From version 3.22.0 To 3.26.0 Newly added SQL function .
Specific contents include :
Boolean literals and judgments
Window function
Filter Clause
Insert … on conflict (“Upsert”)
To be ranked high
stay Modern-SQL.com Go up next
Boolean variables and judgments
SQLite Support “ false ” Boolean value : It accepts Boolean As the name of the type , But it treats it as an integer ( This is very similar to MySQL). Truth value true and false By the numerical value 1 and 0 Express ( This and C The language is the same ).
From version 3.23.0 Start ,SQLite Put keywords true and false Separate numbers 1 and 0 Express , And support is [not] true | false Judgment statement of . Now? , It no longer supports keywords unknown. Developers can use null values null Instead of , because unknown and null The Boolean value of is the same .
stay INSERT and UPDATE In the sentence , Literal true and false Can be greatly improved values and set Readability of clauses .
is [not] true | false This judgment statement is very useful , It is different from the meaning of comparison :
Let's compare
WHERE c <> FALSE
and
WHERE c IS NOT FALSE
In the example above , If c yes null, that c <> false The result is unknown.
This is because WHERE Clause only accepts the result as true Value , It will filter out the result as false or unknown Value . such , It will remove the corresponding row from the result .
Corresponding to this , If c yes null, that ,c is not false It turns out that true. therefore , the second WHERE The clause will also contain c yes null The line of .
To achieve the same effect , Another method you can use is to add separate processing null Value clause . That is, use statements :
WHERE c <> FALSE
OR c IS NULL
This form of statement is longer and has some redundant statements (c Used twice ). Cut a long story short , have access to is not false Judgment replaces this or…is-null The sentence of . More details , Please refer to “Binary Decisions Based on Three-Valued Results”.
SQLite Support for Boolean literals and Boolean judgments in is now close to other open source databases , The only difference is SQLite I won't support it is[not] unknown( You can use is [not] null Instead of ). Interestingly , These functions are not yet available in the commercial products mentioned below .
0: Only support true,false. I won't support it notknown, if necessary , use null Instead of
1: I won't support it is [not] unknown, if necessary , use is [not] null Instead of
Window function
SQLite 3.25.0 Introduced window function . If you know the window function , Then I know this is a big thing . If you don't understand window functions , Please learn how to use by yourself . This article will not specifically explain window functions , But please believe that : It's the most important “ modern ”SQL characteristic .
SQLite Yes over Clause support is very close to other databases . The only notable limitation is range Statement does not support numbers or spacing ( Support only current row and unbounded preceding|following). In the release sqlite 3.25.0 when ,SQL Server and PostgreSQL With the same restrictions .PostgreSQL 11 Eliminate this limitation .
0: There is no change
1:Range Range definitions do not support datetime type
2:Range The scope does not accept keywords ( Only support unbounded and current row)
SQLite The support for window functions is leading in the industry . Functions that it does not support are also not supported in some other major products ( In an aggregate statement distinct,width_bucket, respect|ignore nulls and from first|last Such statements ).
0: Again, there is no ORDER BY sentence
1: Negative offset is not allowed ,nulls Specific processing of :lead(, 'IGNORE NULLS'), Here is the string parameter
2: There is no default value ( The third parameter ), I won't support it respect|ignore nulls sentence
3: Negative offset is not allowed , I won't support it ignore nulls sentence
4: Negative offset is not allowed
5: I won't support it respect|ignore nulls sentence
6: Negative offset is not allowed , I won't support it respect|ignore nulls sentence
7:nulls Specific processing of :first_value(, 1, null, 'IGNORE NULLS') , Here is the string parameter .
8: I won't support it ignore nulls sentence
9: I won't support it ignore nulls Statement and from last sentence
Filter statements
although filter Statements are just grammatical sugar —— You can also easily use expressions to get the same result —— I think it is also an essential grammar sugar , Because it makes it easier for people to learn and understand SQL sentence .
Look at the following select Clause , Which one do you think is easier to understand ?
SELECT SUM(revenue) total_revenue
, SUM(CASE WHEN product = 1
THEN revenue
END
) prod1_revenue
...
and
SELECT SUM(revenue) total_revenue
, SUM(revenue) FILTER(WHERE product = 1) prod1_revenue
...
This example summarizes filter The function of clause : It is the suffix of aggregate function , It can be based on specific conditions before aggregation , Filter out the corresponding lines .pivot The technology is filter Clause is the most common use case . This includes adding entity attribute values (EAV) Attributes in the model are converted into columns of the table , If you want to know more , You can refer to the link “filter-Selective Aggregates”(https://modern-sql.com/feature/filter).
SQLite From version 3.25.0 Start , In the use of over Clause supports filter Clause , But in use group by Clause is not yet supported in aggregate functions . Unfortunately , This means that you still cannot SQLite Use in filter Statement to handle the above situation . You must use it as before case expression . I really hope SQLite We can do this as soon as possible .
Insert … on conflict (“Upsert”)
SQLite From version 3.24.0 Start , Introduced “upsert” Concept : It's a insert sentence , You can handle the conflict between primary key and unique constraint gracefully . You can choose to ignore these conflicts ( stay on conflict Do nothing in the sentence ) Or update the current line ( stay on conflict Statement ).
This is a unique SQL Expand , That is, it is not a standard SQL Part of , Therefore, it is gray in the following matrix . however ,SQLite Compliance and PostgreSQL The same syntax to achieve this function 0. This standard provides a reference to merge Statement support .
And PostgreSQL Different ,SQLite There is a problem in the following statement .
INSERT INTO target
SELECT *
FROM source
ON CONFLICT (id)
DO UPDATE SET val = excluded.val
According to the documentation , This is because the parser cannot judge keywords ON yes SELECT The connection constraint of the statement is still upsert The beginning of a clause . You can solve this problem by adding clauses to the query , for example where true.
INSERT INTO target
SELECT *
FROM source
WHERE true
ON CONFLICT (id)
DO UPDATE SET val = excluded.val
0: Also record insert、update、delete and merge Error message of operation (“DML error logging”)
1:On conflict The statement cannot be next to the query from sentence , if necessary , You can add where true Statement to separate .
To be ranked high
SQLite Another unique feature introduced is renaming columns in the benchmark database table 1. The standard SQL This kind of function is not supported 2.
SQLite Follow the syntax commonly used by other products to rename the list :
ALTER TABLE … RENAME COLUMN … TO
0: Please refer to sp_rename.
Other messages
stay 2018 year ,SQLite In addition to the SQL Grammatical changes , There are also some application program interfaces (API) The change of . You can refer to sqlite.com(https://www.sqlite.org/news.html) See the news section on the for more detailed information .
Footmark :
0:SQLite Generally follow PostgreSQL grammar ,Richard Hipp Call this PostgreSQL What will be done (WWPD).
1: The benchmark database table refers to Create table Statement to create a database table . Derived database tables ( Such as Select Statement returns the query result set ) The column names in can be passed through SELECT sentence 、FROM Sentence or WITH Statement to change
2: as far as I am concerned , Perhaps this function can be simulated by updatable views or derived columns .
original text :https://modern-sql.com/blog/2019-01/sqlite-in-2018
Author's brief introduction :Markus Winand Provide efficient SQL train . His published works 《SQL Performance Explained》 It has become a standard reading for developers .
We have created a high-quality technical exchange group , With good people , I will be excellent myself , hurriedly Click Add group , Enjoy growing up together . in addition , If you want to change jobs recently , Years ago, I spent 2 A wave of large factory classics were collected in a week , Those who are ready to change jobs after the festival can Click here to get !
Recommended reading
··································
Hello , I'm a procedural ape DD,10 Old driver developed in 、 Alibaba cloud MVP、 Tencent cloud TVP、 I have published books and started a business 、 State-owned enterprises 4 In the Internet 6 year . From ordinary developers to architects 、 Then to the partner . Come all the way , My deepest feeling is that I must keep learning and pay attention to the frontier . As long as you can hold on , Think more 、 Don't complain 、 Do it frequently , It's easy to overtake on a curve ! therefore , Don't ask me what I'm doing now, whether it's in time . If you are optimistic about one thing , It must be persistence to see hope , Instead of sticking to it when you see hope . believe me , Just stick to it , You must be better than now ! If you don't have any direction , You can pay attention to me first , Some cutting-edge information is often shared here , Help you accumulate the capital to overtake on the curve .
边栏推荐
- VR panoramic wedding helps couples record romantic and beautiful scenes
- OpenEuler 会长久吗
- Insert dial file of Jerry's watch [chapter]
- C语言高校实验室预约登记系统
- 面试突击62:group by 有哪些注意事项?
- I want to say more about this communication failure
- Alibaba brand data bank: introduction to the most complete data bank
- Grafana 9.0 正式发布!堪称最强!
- Excel usage record
- Interview assault 63: how to remove duplication in MySQL?
猜你喜欢
Interview assault 63: how to remove duplication in MySQL?
Jerry's access to additional information on the dial [article]
Getting started with pytest ----- test case rules
Prophet模型的简介以及案例分析
OliveTin能在网页上安全运行shell命令(上)
[Android] kotlin code writing standardization document
30 minutes to understand PCA principal component analysis
MS-TCT:Inria&SBU提出用于动作检测的多尺度时间Transformer,效果SOTA!已开源!(CVPR2022)...
Olivetin can safely run shell commands on Web pages (Part 1)
Distinguish between basic disk and dynamic disk RAID disk redundant array
随机推荐
传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
The difference between parallelism and concurrency
【Android】Kotlin代码编写规范化文档
带你穿越古罗马,元宇宙巴士来啦 #Invisible Cities
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
RB157-ASEMI整流桥RB157
QT中Model-View-Delegate委托代理机制用法介绍
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
Jerry's watch reads the file through the file name [chapter]
容器里用systemctl运行服务报错:Failed to get D-Bus connection: Operation not permitted(解决方法)
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
node の SQLite
Recommend easy-to-use backstage management scaffolding, everyone open source
Interesting - questions about undefined
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
Interview assault 63: how to remove duplication in MySQL?
d绑定函数
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Virtual machine VirtualBox and vagrant installation