SQLiteadvanced主题tutorial

LearningSQLiteadvancedquerytechniques, performanceoptimization策略 and best practices

advancedquerytechniques

子query

子query is 嵌套 in otherSQL语句in query, 用于 complex dataretrieve:

-- 单行子query
SELECT name, salary 
FROM employees 
WHERE salary > (SELECT AVG(salary) FROM employees);

--  many 行子query
SELECT name 
FROM employees 
WHERE department_id IN (SELECT id FROM departments WHERE location = 'Beijing');

-- 相关子query
SELECT e1.name, e1.salary 
FROM employees e1 
WHERE e1.salary > (SELECT AVG(e2.salary) FROM employees e2 WHERE e2.department_id = e1.department_id);

连接queryadvancedtechniques

除了basic INNER JOIN, SQLite还supportother连接class型:

-- LEFT JOIN ( left 连接) 
SELECT e.name, d.name as department 
FROM employees e 
LEFT JOIN departments d ON e.department_id = d.id;

-- RIGHT JOIN ( right 连接) 
SELECT e.name, d.name as department 
FROM employees e 
RIGHT JOIN departments d ON e.department_id = d.id;

-- FULL OUTER JOIN (全 out 连接) 
-- SQLite不直接supportFULL OUTER JOIN, 但可以usingUNIONimplementation
SELECT e.name, d.name as department 
FROM employees e 
LEFT JOIN departments d ON e.department_id = d.id
UNION
SELECT e.name, d.name as department 
FROM employees e 
RIGHT JOIN departments d ON e.department_id = d.id;

窗口function

SQLite 3.25.0及以 on versionsupport窗口function, 用于计算累积, move平均值etc.:

-- 计算每个部门 工资排名
SELECT name, department_id, salary,
       RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) as rank
FROM employees;

-- 计算累积工资
SELECT name, salary,
       SUM(salary) OVER (ORDER BY salary) as cumulative_salary
FROM employees;

-- 计算move平均值
SELECT date, sales,
       AVG(sales) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as moving_avg
FROM sales_data;

performanceoptimization策略

queryoptimization

-- usingEXPLAINanalysisquery
EXPLAIN QUERY PLAN
SELECT * FROM employees WHERE department_id = 1 AND salary > 5000;

datalibrarystructureoptimization

-- 执行VACUUMoperation
VACUUM;

-- 针 for specific表执行VACUUM
VACUUM employees;

连接 and transactionoptimization

-- 启用WAL模式
PRAGMA journal_mode = WAL;

-- check当 before log模式
PRAGMA journal_mode;

实践case: 电商systemperformanceoptimization

fake设我们 has 一个电商system, 需要optimization以 under query:

-- optimization before : queryuser最近 订单
SELECT o.id, o.order_date, o.total_amount, p.name as product_name
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id
WHERE o.user_id = 1
ORDER BY o.order_date DESC;

-- optimization after : 
-- 1. creationindex
CREATE INDEX idx_orders_user_id ON orders(user_id);
CREATE INDEX idx_orders_order_date ON orders(order_date);
CREATE INDEX idx_order_items_order_id ON order_items(order_id);
CREATE INDEX idx_order_items_product_id ON order_items(product_id);

-- 2. optimizationquery
SELECT o.id, o.order_date, o.total_amount, p.name as product_name
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id
WHERE o.user_id = 1
ORDER BY o.order_date DESC
LIMIT 10;

throughcreation适当 index and optimizationquery语句, 我们可以显著improvingqueryperformance, 特别 is in processing big 量data时.

SQLitebest practices

securitybest practices

parameter化queryexample

-- 不security query
SELECT * FROM users WHERE username = 'admin' AND password = '123456';

-- security parameter化query ( in application程序in) 
-- Pythonexample
cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, password));

Developmentbest practices

deploymentbest practices

-- 设置cache big  small  (以KB for 单位) 
PRAGMA cache_size = 8192;

-- check当 before cache big  small 
PRAGMA cache_size;

SQLiteadvancedfunctions

JSONsupport

SQLite 3.9.0及以 on versionsupportJSONdataclass型 and 相关function:

-- creationpackage含JSONdata 表
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    profile JSON
);

-- 插入JSONdata
INSERT INTO users (name, profile) VALUES (
    '张三',
    '{"age": 30, "email": "zhangsan@example.com", "address": "北京市"}'
);

-- queryJSONdata
SELECT name, json_extract(profile, '$.email') as email
FROM users;

-- updateJSONdata
UPDATE users
SET profile = json_set(profile, '$.age', 31)
WHERE id = 1;

全文搜索

SQLitesupport全文搜索 (FTS) functions:

-- creationFTS表
CREATE VIRTUAL TABLE articles USING FTS5(title, content);

-- 插入data
INSERT INTO articles (title, content) VALUES
('SQLitetutorial', 'SQLite is a 轻量级 嵌入式datalibrary...'),
('Pythonprogramming', 'Python is a广泛using advancedprogramminglanguage...');

-- 全文搜索
SELECT * FROM articles WHERE articles MATCH 'SQLitetutorial';

-- 搜索specific列
SELECT * FROM articles WHERE title MATCH 'tutorial';

自定义function

throughscaleinterface, SQLite允许creation自定义function:

提示: 自定义function通常 in application程序codeinimplementation, 而不 is in SQL语句in. 例such as, in Pythonin可以usingsqlite3module create_functionmethodcreation自定义function.

互动练习

1. 以 under 哪些 is SQLiteperformanceoptimization has 效策略? ( many 选)

A. usingSELECT * 选择所 has 列
B. for 常用query 列creationindex
C. usingEXPLAINanalysisquery执行计划
D. 避免usingtransaction

2. 以 under 哪种连接class型 in SQLitein需要usingUNIONimplementation?

A. INNER JOIN
B. LEFT JOIN
C. RIGHT JOIN
D. FULL OUTER JOIN

3. 实践练习: optimization以 under query

fake设 has 一个订单表orders and 订单project表order_items, queryuserID for 10 所 has 订单及其商品information:

SELECT * FROM orders, order_items WHERE orders.id = order_items.order_id AND orders.user_id = 10;

请写出optimizationsolutions, includingindexcreation and query语句optimization.

summarized and 展望

through本tutorial, 我们Learning了SQLite advanced主题, including:

SQLite is a functions强 big 且flexible 嵌入式datalibrary, 适用于各种application场景. throughMaster这些advanced主题, 你可以充分发挥SQLite 潜力, for 你 application程序providing high 效, reliable datastoresolution.

Learning建议: 继续探索SQLite 官方documentation and communityresource, Understand最 new features and best practices. 实践 is Learningdatalibrary 最佳方式, 尝试 in practicalprojectinapplication所学knowledge.