Veja por que o SQLite Γ© perfeito para o seu trabalho diΓ‘rio. NΓ£o importa se vocΓͺ Γ© um desenvolvedor, analista, testador, administrador ou gerente de produto.
Para comeΓ§ar, alguns fatos conhecidos:
SQLite Γ© o sistema de gerenciamento de banco de dados mais amplamente usado no mundo e estΓ‘ incluΓdo em todos os sistemas operacionais populares.
Funciona sem servidor.
Para desenvolvedores, ele Γ© incorporado diretamente no aplicativo.
Para todos os outros - um console conveniente (REPL) em um arquivo (sqlite3.exe no Windows, sqlite3 no Linux / macOS).
Console, importe e exporte
O console Γ© um recurso matador do SQLite: uma ferramenta de anΓ‘lise de dados mais poderosa do que o Excel e muito mais simples do que qualquer pandas. Os dados do CSV sΓ£o carregados com um comando, a tabela Γ© criada automaticamente:
> .import --csv city.csv city
> select count(*) from city;
1117
Recursos bΓ‘sicos de SQL sΓ£o suportados e o console mostra o resultado em uma boa tabela. Existem tambΓ©m recursos SQL avanΓ§ados, mas mais sobre eles mais tarde.
select
century || '- ' as dates,
count(*) as city_count
from history
group by century
order by century desc;
ββββββββββββ¬βββββββββββββ
β dates β city_count β
ββββββββββββΌβββββββββββββ€
β 21- β 1 β
β 20- β 263 β
β 19- β 189 β
β 18- β 191 β
β 17- β 137 β
β 16- β 79 β
β 15- β 39 β
β 14- β 38 β
β 13- β 27 β
β 12- β 44 β
β 11- β 8 β
β 10- β 6 β
β 9- β 4 β
β 5- β 1 β
β 3- β 1 β
ββββββββββββ΄βββββββββββββ
VΓ‘rios formatos de exportaΓ§Γ£o de dados: sql, csv, json, atΓ© mesmo markdown e html. Tudo Γ© feito com alguns comandos:
.mode json
.output city.json
select city, foundation_year, timezone from city limit 10;
.shell cat city.json
[{"city":"","foundation_year":1969,"timezone":"UTC+3"},
{"city":"","foundation_year":1857,"timezone":"UTC+3"},
{"city":"-","foundation_year":1830,"timezone":"UTC+7"},
{"city":"","foundation_year":1913,"timezone":"UTC+7"},
{"city":"","foundation_year":1730,"timezone":"UTC+7"},
{"city":"","foundation_year":1846,"timezone":"UTC+7"},
{"city":"","foundation_year":1709,"timezone":"UTC+7"},
{"city":"","foundation_year":1942,"timezone":"UTC+7"},
{"city":"","foundation_year":1748,"timezone":"UTC+7"},
{"city":"","foundation_year":1736,"timezone":"UTC+7"}]
Trabalhar com JSON nativamente
NΓ£o hΓ‘ nada mais conveniente do que SQLite para analisar e transformar JSON. VocΓͺ pode selecionar dados diretamente do arquivo, como se fosse uma tabela normal. Ou carregue-o em uma mesa e selecione-o - como preferir.
select
json_extract(value, '$.code') as code,
json_extract(value, '$.name') as name,
json_extract(value, '$.rate') as rate,
json_extract(value, '$.default') as "default"
from
json_each(readfile('currency.sample.json'))
;
ββββββββ¬ββββββββββββββββββββ¬βββββββββββββ¬ββββββββββ β code β name β rate β default β ββββββββΌββββββββββββββββββββΌβββββββββββββΌββββββββββ€ β AZN β β 0.023107 β 0 β β BYR β β 0.034966 β 0 β β EUR β β 0.011138 β 0 β β GEL β β 0.0344 β 0 β β KGS β β 1.131738 β 0 β β KZT β β 5.699857 β 0 β β RUR β β 1.0 β 1 β β UAH β β 0.380539 β 0 β β USD β β 0.013601 β 0 β β UZS β β 142.441417 β 0 β ββββββββ΄ββββββββββββββββββββ΄βββββββββββββ΄ββββββββββ
NΓ£o importa o quΓ£o extenso Γ© o JSON, vocΓͺ pode escolher os atributos de qualquer aninhamento:
select
json_extract(value, '$.id') as id,
json_extract(value, '$.name') as name
from
json_tree(readfile('industry.sample.json'))
where
path like '$[%].industries'
;
ββββββββββ¬βββββββββββββββββββββββ β id β name β ββββββββββΌβββββββββββββββββββββββ€ β 7.538 β - β β 7.539 β - β β 7.540 β β β 9.399 β β β 9.400 β β β 9.401 β β β 43.641 β β β 43.646 β β β 43.647 β β ββββββββββ΄βββββββββββββββββββββββ
CTE e operaΓ§Γ΅es definidas
, Common Table Expressions ( WITH
) , . ( parent_id
) β WITH
. «» .
with recursive tmp(id, name, level) as (
select id, name, 1 as level
from area
where parent_id is null
union all
select
area.id,
tmp.name || ', ' || area.name as name,
tmp.level + 1 as level
from area
join tmp on area.parent_id = tmp.id
)
select * from tmp;
ββββββββ¬ββββββββββββββββββββββββββββββββββββββ¬ββββββββ β id β name β level β ββββββββΌββββββββββββββββββββββββββββββββββββββΌββββββββ€ β 113 β β 1 β β 1 β , β 2 β β 1586 β , β 2 β β 1588 β , , β 3 β β 78 β , , β 3 β β 212 β , , β 3 β β ... β ... β ... β ββββββββ΄ββββββββββββββββββββββββββββββββββββββ΄ββββββββ
? : UNION
, INTERSECT
, EXCEPT
.
select employer_id
from employer_area
where area_id = 1
except
select employer_id
from employer_area
where area_id = 2;
? β :
alter table vacancy
add column salary_net integer as (
case when salary_gross = true then
round(salary_from/1.13)
else
salary_from
end
);
, :
select
substr(name, 1, 40) as name,
salary_net
from vacancy
where
salary_currency = 'RUR'
and salary_net is not null
limit 10;
ββββββββββββββββββββββββββββββββββββββββββββ¬βββββββββββββ
β name β salary_net β
ββββββββββββββββββββββββββββββββββββββββββββΌβββββββββββββ€
β - (Delphi) β 40000 β
β Scala / Java ( Senio β 60000 β
β Java / Kotlin Developer β 150000 β
β 1 β 150000 β
β C# β 53097 β
β 1 β 80000 β
β Java - (Middle, Senior) β 100000 β
β C#/ .NET β 70796 β
β / QA engineer ( β 45000 β
β β 17699 β
ββββββββββββββββββββββββββββββββββββββββββββ΄βββββββββββββ
.
? : , , , . , . , ( ).
.load sqlite3-stats select count(*) as book_count, cast(avg(num_pages) as integer) as mean, cast(median(num_pages) as integer) as median, mode(num_pages) as mode, percentile_90(num_pages) as p90, percentile_95(num_pages) as p95, percentile_99(num_pages) as p99 from books;
ββββββββββββββ¬βββββββ¬βββββββββ¬βββββββ¬ββββββ¬ββββββ¬βββββββ
β book_count β mean β median β mode β p90 β p95 β p99 β
ββββββββββββββΌβββββββΌβββββββββΌβββββββΌββββββΌββββββΌβββββββ€
β 1483 β 349 β 295 β 256 β 640 β 817 β 1199 β
ββββββββββββββ΄βββββββ΄βββββββββ΄βββββββ΄ββββββ΄ββββββ΄βββββββ
. SQLite - . , β . .
, . , :
. . , :
with slots as (
select
num_pages/100 as slot,
count(*) as book_count
from books
group by slot
),
max as (
select max(book_count) as value
from slots
)
select
slot,
book_count,
printf('%.' || (book_count * 30 / max.value) || 'c', '*') as bar
from slots, max
order by slot;
ββββββββ¬βββββββββββββ¬βββββββββββββββββββββββββββββββββ
β slot β book_count β bar β
ββββββββΌβββββββββββββΌβββββββββββββββββββββββββββββββββ€
β 0 β 116 β ********* β
β 1 β 254 β ******************** β
β 2 β 376 β ****************************** β
β 3 β 285 β ********************** β
β 4 β 184 β ************** β
β 5 β 90 β ******* β
β 6 β 54 β **** β
β 7 β 41 β *** β
β 8 β 31 β ** β
β 9 β 15 β * β
β 10 β 11 β * β
β 11 β 12 β * β
β 12 β 2 β * β
β 13 β 5 β * β
β 14 β 3 β * β
β 15 β 1 β * β
β 17 β 1 β * β
β 18 β 2 β * β
ββββββββ΄βββββββββββββ΄βββββββββββββββββββββββββββββββββ
SQLite ( β ). INSERT
240 . CSV ( ) β 2 .
.load sqlite3-vsv create virtual table temp.blocks_csv using vsv( filename="ipblocks.csv", schema="create table x(network text, geoname_id integer, registered_country_geoname_id integer, represented_country_geoname_id integer, is_anonymous_proxy integer, is_satellite_provider integer, postal_code text, latitude real, longitude real, accuracy_radius integer)", columns=10, header=on, nulls=on );
.timer on
insert into blocks
select * from blocks_csv;
Run Time: real 5.176 user 4.716420 sys 0.403866
select count(*) from blocks;
3386629
Run Time: real 0.095 user 0.021972 sys 0.063716
, SQLite , . . write-ahead log ( ) . β , .
SQLite . , sqlite.org SQLite , (~200 ). 700 , 95% .
,
, «» . .
SQLite : json json_extract()
:
create table currency(
body text,
code text as (json_extract(body, '$.code')),
name text as (json_extract(body, '$.name'))
);
create index currency_code_idx on currency(code);
insert into currency
select value
from json_each(readfile('currency.sample.json'));
explain query plan
select name from currency where code = 'RUR';
QUERY PLAN
`--SEARCH TABLE currency USING INDEX currency_code_idx (code=?)
. WITH RECURSIVE
, :
:
create virtual table books_fts
using fts5(title, author, publisher);
insert into books_fts
select title, author, publisher from books;
select
author,
substr(title, 1, 30) as title,
substr(publisher, 1, 10) as publisher
from books_fts
where books_fts match 'ann'
limit 5;
βββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ¬βββββββββββββ β author β title β publisher β βββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββΌβββββββββββββ€ β Ruby Ann Boxcar β Ruby Ann's Down Home Trailer P β Citadel β β Ruby Ann Boxcar β Ruby Ann's Down Home Trailer P β Citadel β β Lynne Ann DeSpelder β The Last Dance: Encountering D β McGraw-Hil β β Daniel Defoe β Robinson Crusoe β Ann Arbor β β Ann Thwaite β Waiting for the Party: The Lif β David R. G β βββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββ΄βββββββββββββ
in-memory ? :
db = sqlite3.connect(":memory:")
:
db = sqlite3.connect("file::memory:?cache=shared")
( PostgreSQL). UPSERT
, UPDATE FROM
generate_series()
. R-Tree . , fuzzy- . SQLite «» .
, SQLite . , !
, SQLite β @sqliter