@gordon.gan/specflow 1.4.0-beta → 1.4.2-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/prompts/approval/database-guidance.md +79 -0
- package/prompts/approval/generate.md +600 -290
- package/skills/database/LICENSE +405 -0
- package/skills/database/ORIGIN.md +6 -0
- package/skills/database/README.md +30 -0
- package/skills/database/elasticsearch/LICENSE.txt +202 -0
- package/skills/database/elasticsearch/SKILL.md +199 -0
- package/skills/database/elasticsearch/examples/01-fulltext-search.md +215 -0
- package/skills/database/elasticsearch/examples/02-aggregation-report.md +206 -0
- package/skills/database/elasticsearch/examples/03-reindex-zero-downtime.md +200 -0
- package/skills/database/elasticsearch/examples/04-cluster-monitoring.md +204 -0
- package/skills/database/elasticsearch/references/01-query-dsl-fulltext.md +162 -0
- package/skills/database/elasticsearch/references/02-query-dsl-term.md +210 -0
- package/skills/database/elasticsearch/references/03-aggregations-metric.md +161 -0
- package/skills/database/elasticsearch/references/04-aggregations-bucket.md +236 -0
- package/skills/database/elasticsearch/references/05-mapping-types.md +134 -0
- package/skills/database/elasticsearch/references/06-analyzers.md +187 -0
- package/skills/database/elasticsearch/references/07-cluster-ops.md +225 -0
- package/skills/database/elasticsearch/references/08-elk-integration.md +170 -0
- package/skills/database/mysql/SKILL.md +195 -0
- package/skills/database/mysql/examples/01-connection-pool.md +75 -0
- package/skills/database/mysql/examples/02-slow-query-optimization.md +98 -0
- package/skills/database/mysql/examples/03-master-slave-setup.md +144 -0
- package/skills/database/mysql/examples/04-backup-strategy.md +212 -0
- package/skills/database/mysql/references/01-functions-string.md +103 -0
- package/skills/database/mysql/references/02-functions-date.md +152 -0
- package/skills/database/mysql/references/03-functions-aggregate-window.md +167 -0
- package/skills/database/mysql/references/04-functions-json.md +129 -0
- package/skills/database/mysql/references/05-sql-ddl-types.md +235 -0
- package/skills/database/mysql/references/06-index-optimization.md +232 -0
- package/skills/database/mysql/references/07-replication-ha.md +213 -0
- package/skills/database/mysql/references/08-backup-restore.md +207 -0
- package/skills/database/mysql/references/09-advanced-features.md +345 -0
- package/skills/database/oracle/LICENSE.txt +202 -0
- package/skills/database/oracle/SKILL.md +238 -0
- package/skills/database/oracle/examples/01-plsql-procedure.md +90 -0
- package/skills/database/oracle/examples/02-awr-analysis.md +99 -0
- package/skills/database/oracle/examples/03-rman-backup.md +108 -0
- package/skills/database/oracle/examples/04-dataguard-setup.md +146 -0
- package/skills/database/oracle/references/01-functions-string.md +91 -0
- package/skills/database/oracle/references/02-functions-date.md +71 -0
- package/skills/database/oracle/references/03-analytic-functions.md +103 -0
- package/skills/database/oracle/references/04-plsql-guide.md +303 -0
- package/skills/database/oracle/references/05-performance-tuning.md +164 -0
- package/skills/database/oracle/references/06-backup-recovery.md +115 -0
- package/skills/database/oracle/references/07-dataguard-rac.md +76 -0
- package/skills/database/oracle/references/08-security.md +170 -0
- package/skills/database/oracle/references/09-sql-syntax.md +152 -0
- package/skills/database/oracle/references/10-features.md +174 -0
- package/skills/database/postgresql/LICENSE.txt +202 -0
- package/skills/database/postgresql/SKILL.md +182 -0
- package/skills/database/postgresql/examples/.gitkeep +0 -0
- package/skills/database/postgresql/examples/01-jsonb-query.md +72 -0
- package/skills/database/postgresql/examples/02-cte-recursive.md +110 -0
- package/skills/database/postgresql/examples/03-performance-tuning.md +114 -0
- package/skills/database/postgresql/examples/04-streaming-replication.md +113 -0
- package/skills/database/postgresql/references/.gitkeep +0 -0
- package/skills/database/postgresql/references/01-functions-string.md +174 -0
- package/skills/database/postgresql/references/02-functions-datetime.md +54 -0
- package/skills/database/postgresql/references/03-functions-aggregate-window.md +142 -0
- package/skills/database/postgresql/references/04-functions-jsonb.md +117 -0
- package/skills/database/postgresql/references/05-fulltext-search.md +109 -0
- package/skills/database/postgresql/references/06-index-types.md +95 -0
- package/skills/database/postgresql/references/07-partition-fdw.md +133 -0
- package/skills/database/postgresql/references/08-replication-backup.md +215 -0
- package/skills/database/redis/LICENSE.txt +202 -0
- package/skills/database/redis/SKILL.md +922 -0
- package/skills/database/redis/examples/01-cache-usage.md +104 -0
- package/skills/database/redis/examples/02-session-storage.md +72 -0
- package/skills/database/redis/examples/03-leaderboard.md +63 -0
- package/skills/database/redis/examples/04-redis-cluster-setup.md +70 -0
- package/skills/database/redis/examples/05-stream-queue.md +65 -0
- package/skills/database/redis/references/command-quick-ref.md +180 -0
- package/skills/database/redis/references/commands-admin-key.md +413 -0
- package/skills/database/redis/references/commands-set-sorted-advanced.md +539 -0
- package/skills/database/redis/references/commands-string-hash-list.md +458 -0
- package/skills/database/redis/references/memory-optimization.md +150 -0
- package/skills/database/redis/references/redis-conf-production.md +139 -0
- package/skills/specflow-approval/SKILL.md +102 -181
- package/templates/approval.md +321 -221
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
# 高级特性 (Advanced Features)
|
|
2
|
+
|
|
3
|
+
## 简介
|
|
4
|
+
|
|
5
|
+
MySQL 提供视图、CTE、存储过程/函数、触发器、事务与锁、分区表等高级特性,用于满足复杂业务需求和性能优化。
|
|
6
|
+
|
|
7
|
+
## 视图 (View)
|
|
8
|
+
|
|
9
|
+
视图是存储的查询定义,不存储数据,使用时会展开为底层查询执行。
|
|
10
|
+
|
|
11
|
+
### 创建与使用
|
|
12
|
+
|
|
13
|
+
```sql
|
|
14
|
+
-- 创建视图
|
|
15
|
+
CREATE VIEW user_order_summary AS
|
|
16
|
+
SELECT u.id, u.name, COUNT(o.id) AS order_count, SUM(o.amount) AS total_amount
|
|
17
|
+
FROM user u
|
|
18
|
+
LEFT JOIN `order` o ON u.id = o.user_id
|
|
19
|
+
GROUP BY u.id;
|
|
20
|
+
|
|
21
|
+
-- 使用视图(像普通表一样查询)
|
|
22
|
+
SELECT * FROM user_order_summary WHERE order_count > 5;
|
|
23
|
+
|
|
24
|
+
-- 可更新视图(需满足条件)
|
|
25
|
+
UPDATE active_user SET email = 'new@example.com' WHERE id = 1;
|
|
26
|
+
|
|
27
|
+
-- 查看视图定义
|
|
28
|
+
SHOW CREATE VIEW user_order_summary;
|
|
29
|
+
|
|
30
|
+
-- 删除视图
|
|
31
|
+
DROP VIEW IF EXISTS user_order_summary;
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 视图限制
|
|
35
|
+
|
|
36
|
+
| 限制 | 说明 |
|
|
37
|
+
|------|------|
|
|
38
|
+
| 不可更新条件 | 含 DISTINCT、聚合、GROUP BY、HAVING、UNION 的视图不可更新 |
|
|
39
|
+
| 性能 | 与直接查询无异(视图不存储数据) |
|
|
40
|
+
| 算法 | `ALGORITHM = MERGE | TEMPTABLE | UNDEFINED` |
|
|
41
|
+
|
|
42
|
+
**业务场景**:封装复杂查询逻辑、简化报表查询、提供权限控制层。
|
|
43
|
+
|
|
44
|
+
## CTE (Common Table Expression, MySQL 8.0+)
|
|
45
|
+
|
|
46
|
+
CTE 提供临时结果集的命名引用,可读性更高,支持递归。
|
|
47
|
+
|
|
48
|
+
### 基础 CTE
|
|
49
|
+
|
|
50
|
+
```sql
|
|
51
|
+
WITH dept_avg AS (
|
|
52
|
+
SELECT dept_id, AVG(salary) AS avg_salary
|
|
53
|
+
FROM employee
|
|
54
|
+
GROUP BY dept_id
|
|
55
|
+
)
|
|
56
|
+
SELECT e.name, e.salary, da.avg_salary
|
|
57
|
+
FROM employee e
|
|
58
|
+
JOIN dept_avg da ON e.dept_id = da.dept_id
|
|
59
|
+
WHERE e.salary > da.avg_salary;
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 递归 CTE — 树形结构查询
|
|
63
|
+
|
|
64
|
+
```sql
|
|
65
|
+
WITH RECURSIVE sub_depts AS (
|
|
66
|
+
-- 基础节点(根部门)
|
|
67
|
+
SELECT id, name, parent_id, 1 AS level
|
|
68
|
+
FROM department
|
|
69
|
+
WHERE parent_id IS NULL
|
|
70
|
+
|
|
71
|
+
UNION ALL
|
|
72
|
+
|
|
73
|
+
-- 递归子节点
|
|
74
|
+
SELECT d.id, d.name, d.parent_id, sd.level + 1
|
|
75
|
+
FROM department d
|
|
76
|
+
JOIN sub_depts sd ON d.parent_id = sd.id
|
|
77
|
+
)
|
|
78
|
+
SELECT * FROM sub_depts ORDER BY level, id;
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**业务场景**:
|
|
82
|
+
- 递归 CTE 查询组织树、分类树、评论树
|
|
83
|
+
- CTE 替代派生表,提高可读性并支持多次引用
|
|
84
|
+
|
|
85
|
+
## 存储过程与存储函数
|
|
86
|
+
|
|
87
|
+
### 存储过程
|
|
88
|
+
|
|
89
|
+
封装多条 SQL,支持事务控制、IN/OUT 参数、错误处理。
|
|
90
|
+
|
|
91
|
+
```sql
|
|
92
|
+
DELIMITER //
|
|
93
|
+
CREATE PROCEDURE transfer_funds(
|
|
94
|
+
IN from_account INT,
|
|
95
|
+
IN to_account INT,
|
|
96
|
+
IN amount DECIMAL(10, 2),
|
|
97
|
+
OUT result_code INT,
|
|
98
|
+
OUT result_msg VARCHAR(200)
|
|
99
|
+
)
|
|
100
|
+
BEGIN
|
|
101
|
+
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
102
|
+
BEGIN
|
|
103
|
+
ROLLBACK;
|
|
104
|
+
SET result_code = -1;
|
|
105
|
+
SET result_msg = '转账失败,事务回滚';
|
|
106
|
+
END;
|
|
107
|
+
|
|
108
|
+
START TRANSACTION;
|
|
109
|
+
UPDATE account SET balance = balance - amount
|
|
110
|
+
WHERE id = from_account AND balance >= amount;
|
|
111
|
+
|
|
112
|
+
IF ROW_COUNT() = 0 THEN
|
|
113
|
+
SET result_code = -2;
|
|
114
|
+
SET result_msg = '余额不足';
|
|
115
|
+
ROLLBACK;
|
|
116
|
+
ELSE
|
|
117
|
+
UPDATE account SET balance = balance + amount WHERE id = to_account;
|
|
118
|
+
SET result_code = 0;
|
|
119
|
+
SET result_msg = '转账成功';
|
|
120
|
+
COMMIT;
|
|
121
|
+
END IF;
|
|
122
|
+
END //
|
|
123
|
+
DELIMITER ;
|
|
124
|
+
|
|
125
|
+
-- 调用
|
|
126
|
+
CALL transfer_funds(1, 2, 100.00, @code, @msg);
|
|
127
|
+
SELECT @code, @msg;
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 存储函数
|
|
131
|
+
|
|
132
|
+
返回单值的函数,可在 SQL 中直接使用。
|
|
133
|
+
|
|
134
|
+
```sql
|
|
135
|
+
DELIMITER //
|
|
136
|
+
CREATE FUNCTION get_order_count(user_id INT) RETURNS INT
|
|
137
|
+
READS SQL DATA
|
|
138
|
+
BEGIN
|
|
139
|
+
DECLARE cnt INT;
|
|
140
|
+
SELECT COUNT(*) INTO cnt FROM `order` WHERE user_id = user_id;
|
|
141
|
+
RETURN cnt;
|
|
142
|
+
END //
|
|
143
|
+
DELIMITER ;
|
|
144
|
+
|
|
145
|
+
-- 使用
|
|
146
|
+
SELECT name, get_order_count(id) AS order_count FROM user;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### 存储过程 vs 函数
|
|
150
|
+
|
|
151
|
+
| 特性 | 存储过程 | 存储函数 |
|
|
152
|
+
|------|---------|---------|
|
|
153
|
+
| 返回值 | 多个 OUT 参数 | 单个返回值 |
|
|
154
|
+
| 调用方式 | `CALL proc()` | `SELECT func()` 或 SQL 中直接使用 |
|
|
155
|
+
| 事务控制 | ✅ 支持 | ❌ 不支持 |
|
|
156
|
+
| SQL 中使用 | ❌ | ✅ |
|
|
157
|
+
|
|
158
|
+
**业务场景**:存储过程适合转账、库存核减等事务性操作。但现代应用主要在应用层(如 Spring/Go)实现业务逻辑。
|
|
159
|
+
|
|
160
|
+
## 触发器 (Trigger)
|
|
161
|
+
|
|
162
|
+
### 基本语法
|
|
163
|
+
|
|
164
|
+
```sql
|
|
165
|
+
-- CREATE TRIGGER trigger_name
|
|
166
|
+
-- {BEFORE | AFTER} {INSERT | UPDATE | DELETE}
|
|
167
|
+
-- ON table_name FOR EACH ROW
|
|
168
|
+
-- trigger_body
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 常见用例
|
|
172
|
+
|
|
173
|
+
```sql
|
|
174
|
+
-- 1. 自动更新时间
|
|
175
|
+
CREATE TRIGGER before_user_update
|
|
176
|
+
BEFORE UPDATE ON user
|
|
177
|
+
FOR EACH ROW
|
|
178
|
+
SET NEW.updated_at = NOW();
|
|
179
|
+
|
|
180
|
+
-- 2. 审计日志
|
|
181
|
+
CREATE TRIGGER after_order_update
|
|
182
|
+
AFTER UPDATE ON `order`
|
|
183
|
+
FOR EACH ROW
|
|
184
|
+
INSERT INTO audit_log (table_name, action, old_data, new_data)
|
|
185
|
+
VALUES ('order', 'UPDATE',
|
|
186
|
+
JSON_OBJECT('status', OLD.status, 'amount', OLD.amount),
|
|
187
|
+
JSON_OBJECT('status', NEW.status, 'amount', NEW.amount));
|
|
188
|
+
|
|
189
|
+
-- 3. 防止重复签到
|
|
190
|
+
CREATE TRIGGER before_signin_insert
|
|
191
|
+
BEFORE INSERT ON signin
|
|
192
|
+
FOR EACH ROW
|
|
193
|
+
BEGIN
|
|
194
|
+
DECLARE cnt INT;
|
|
195
|
+
SELECT COUNT(*) INTO cnt FROM signin
|
|
196
|
+
WHERE user_id = NEW.user_id AND DATE(created_at) = CURDATE();
|
|
197
|
+
IF cnt > 0 THEN
|
|
198
|
+
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '今日已签到';
|
|
199
|
+
END IF;
|
|
200
|
+
END;
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### 触发器注意事项
|
|
204
|
+
|
|
205
|
+
| 问题 | 说明 |
|
|
206
|
+
|------|------|
|
|
207
|
+
| 隐性执行 | 排查问题困难("魔法"行为) |
|
|
208
|
+
| 性能影响 | 过多触发器影响 DML 性能 |
|
|
209
|
+
| 错误回滚 | 触发器中的错误会回滚外层事务 |
|
|
210
|
+
| 嵌套复杂度 | 不建议在触发器中调用存储过程 |
|
|
211
|
+
|
|
212
|
+
**优先在应用层实现业务逻辑**,触发器仅用于审计、自动更新时间等必要场景。
|
|
213
|
+
|
|
214
|
+
## 事务与锁
|
|
215
|
+
|
|
216
|
+
### ACID 特性
|
|
217
|
+
|
|
218
|
+
| 特性 | 含义 | MySQL 实现 |
|
|
219
|
+
|------|------|-----------|
|
|
220
|
+
| 原子性 (A) | 事务全部成功或全部回滚 | undo log |
|
|
221
|
+
| 一致性 (C) | 事务前后数据一致 | 约束 + 事务 |
|
|
222
|
+
| 隔离性 (I) | 事务间互相隔离 | MVCC + 锁 |
|
|
223
|
+
| 持久性 (D) | 提交后数据持久保存 | redo log |
|
|
224
|
+
|
|
225
|
+
### 事务隔离级别
|
|
226
|
+
|
|
227
|
+
| 隔离级别 | 脏读 | 不可重复读 | 幻读 | 默认? |
|
|
228
|
+
|---------|------|-----------|------|-------|
|
|
229
|
+
| READ UNCOMMITTED | ✅ 可能 | ✅ 可能 | ✅ 可能 | ❌ |
|
|
230
|
+
| READ COMMITTED | ❌ | ✅ 可能 | ✅ 可能 | ❌(多数公司用) |
|
|
231
|
+
| REPEATABLE READ | ❌ | ❌ | ✅ 可能 | ✅ MySQL 默认 |
|
|
232
|
+
| SERIALIZABLE | ❌ | ❌ | ❌ | ❌(性能差) |
|
|
233
|
+
|
|
234
|
+
### 事务使用
|
|
235
|
+
|
|
236
|
+
```sql
|
|
237
|
+
START TRANSACTION;
|
|
238
|
+
UPDATE account SET balance = balance - 100 WHERE id = 1;
|
|
239
|
+
UPDATE account SET balance = balance + 100 WHERE id = 2;
|
|
240
|
+
-- 成功
|
|
241
|
+
COMMIT;
|
|
242
|
+
-- 或失败
|
|
243
|
+
ROLLBACK;
|
|
244
|
+
|
|
245
|
+
-- 保存点
|
|
246
|
+
START TRANSACTION;
|
|
247
|
+
INSERT INTO log VALUES ('step1');
|
|
248
|
+
SAVEPOINT sp1;
|
|
249
|
+
INSERT INTO log VALUES ('step2'); -- 出错
|
|
250
|
+
ROLLBACK TO SAVEPOINT sp1; -- 回退到 sp1
|
|
251
|
+
INSERT INTO log VALUES ('step3');
|
|
252
|
+
COMMIT;
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### 锁类型
|
|
256
|
+
|
|
257
|
+
| 锁类型 | 说明 | SQL |
|
|
258
|
+
|--------|------|-----|
|
|
259
|
+
| 共享锁 (S) | 允许其他事务读,禁止写 | `SELECT ... LOCK IN SHARE MODE` |
|
|
260
|
+
| 排他锁 (X) | 禁止其他事务读写 | `SELECT ... FOR UPDATE` |
|
|
261
|
+
| 表锁 (READ) | 其他会话可读不可写 | `LOCK TABLES user READ;` |
|
|
262
|
+
| 表锁 (WRITE) | 其他会话不可读写 | `LOCK TABLES user WRITE;` |
|
|
263
|
+
| 乐观锁 | 应用层版本号控制 | `UPDATE SET version+1 WHERE version=:old` |
|
|
264
|
+
|
|
265
|
+
### 死锁避免
|
|
266
|
+
|
|
267
|
+
1. **固定访问顺序**:所有事务按相同顺序访问表
|
|
268
|
+
2. **缩短事务时间**:不要在一个事务内执行大量无关操作
|
|
269
|
+
3. **降低隔离级别**:SERIALIZABLE → REPEATABLE READ → READ COMMITTED
|
|
270
|
+
4. **查看死锁**:`SHOW ENGINE INNODB STATUS;`
|
|
271
|
+
|
|
272
|
+
## 分区表 (Partitioning)
|
|
273
|
+
|
|
274
|
+
### 分区类型
|
|
275
|
+
|
|
276
|
+
| 分区类型 | 说明 | 典型场景 |
|
|
277
|
+
|---------|------|---------|
|
|
278
|
+
| RANGE | 按范围分区(最常用) | 时间范围:订单按年/月分区 |
|
|
279
|
+
| LIST | 按值列表分区 | 区域:按 region_id 分区 |
|
|
280
|
+
| HASH | 按哈希函数分区 | 均匀分布:按 id MOD N |
|
|
281
|
+
| KEY | 类似 HASH,MySQL 内部哈希 | 类似 HASH |
|
|
282
|
+
|
|
283
|
+
### RANGE 分区
|
|
284
|
+
|
|
285
|
+
```sql
|
|
286
|
+
CREATE TABLE orders_partitioned (
|
|
287
|
+
id BIGINT NOT NULL,
|
|
288
|
+
user_id INT NOT NULL,
|
|
289
|
+
amount DECIMAL(10, 2),
|
|
290
|
+
created_at DATETIME NOT NULL
|
|
291
|
+
) PARTITION BY RANGE (YEAR(created_at)) (
|
|
292
|
+
PARTITION p2022 VALUES LESS THAN (2023),
|
|
293
|
+
PARTITION p2023 VALUES LESS THAN (2024),
|
|
294
|
+
PARTITION p2024 VALUES LESS THAN (2025),
|
|
295
|
+
PARTITION p_future VALUES LESS THAN MAXVALUE
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
-- 添加分区
|
|
299
|
+
ALTER TABLE orders_partitioned ADD PARTITION (PARTITION p2025 VALUES LESS THAN (2026));
|
|
300
|
+
|
|
301
|
+
-- 删除分区(极快)
|
|
302
|
+
ALTER TABLE orders_partitioned DROP PARTITION p2022;
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### LIST / HASH / KEY 分区
|
|
306
|
+
|
|
307
|
+
```sql
|
|
308
|
+
-- LIST 分区(按区域)
|
|
309
|
+
CREATE TABLE user_region (
|
|
310
|
+
id INT NOT NULL, name VARCHAR(50), region_id INT NOT NULL
|
|
311
|
+
) PARTITION BY LIST (region_id) (
|
|
312
|
+
PARTITION p_north VALUES IN (1, 2, 3),
|
|
313
|
+
PARTITION p_south VALUES IN (4, 5, 6)
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
-- HASH 分区(按哈希)
|
|
317
|
+
CREATE TABLE logs (
|
|
318
|
+
id INT NOT NULL, log_data TEXT, created_at DATETIME
|
|
319
|
+
) PARTITION BY HASH (id) PARTITIONS 8;
|
|
320
|
+
|
|
321
|
+
-- KEY 分区
|
|
322
|
+
CREATE TABLE sessions (
|
|
323
|
+
id INT NOT NULL, session_data TEXT
|
|
324
|
+
) PARTITION BY KEY (id) PARTITIONS 4;
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### 分区修剪 (Partition Pruning)
|
|
328
|
+
|
|
329
|
+
查询自动只扫描相关分区:
|
|
330
|
+
|
|
331
|
+
```sql
|
|
332
|
+
EXPLAIN SELECT * FROM orders_partitioned WHERE created_at >= '2024-01-01';
|
|
333
|
+
-- 只扫描 p2024, p_future
|
|
334
|
+
|
|
335
|
+
ALTER TABLE orders_partitioned TRUNCATE PARTITION p2022; -- 快速清理历史数据
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
### 分区注意事项
|
|
339
|
+
|
|
340
|
+
| 注意点 | 说明 |
|
|
341
|
+
|--------|------|
|
|
342
|
+
| 分区列必须包含在主键中 | MySQL 硬限制 |
|
|
343
|
+
| 分区数建议 | ≤ 1024,单个分区 ≥ 10GB 时效果明显 |
|
|
344
|
+
| 不是越多越好 | 过多分区增加元数据开销 |
|
|
345
|
+
| 最大适用 | 数据归档场景(按时间删除旧分区极快) |
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|