-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: renew table and reindex; explaination sql
- Loading branch information
1 parent
dc7823c
commit 6fdfc03
Showing
2 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# PostgreSQL 代码解析 | ||
|
||
```sql | ||
INSERT INTO auditlogs("BizId","Table","Type","Status","Message","CreateTime","UpdateTime") | ||
VALUES (@BizId,@Table,@Type,@Status,@Message,@CreateTime,@UpdateTime) | ||
ON CONFLICT("Table","BizId","Type") | ||
DO UPDATE SET "Status"=EXCLUDED."Status","Type"=EXCLUDED."Type","Message"=EXCLUDED."Message","UpdateTime"=EXCLUDED."UpdateTime" | ||
``` | ||
|
||
`INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) ON CONFLICT (conflict_target) DO UPDATE SET column1 = value1, column2 = value2, ...` 是一个特定语法结构,用于实现 UPSERT(更新或插入)操作。 | ||
|
||
如果表中已有一条记录使得插入失败(如主键冲突),通常会抛出错误。 | ||
|
||
其中 `EXCLUDED` 表示在冲突发生时的数据。你可以使用 `EXCLUDED.column_name` 来引用这些新行中的某个列的值。 | ||
|
||
```sql | ||
WITH batch AS ( | ||
SELECT id FROM vac | ||
WHERE NOT processed | ||
LIMIT 1000 | ||
FOR UPDATE SKIP LOCKED | ||
) | ||
UPDATE vac | ||
SET processed = true | ||
WHERE id IN (SELECT id FROM batch); | ||
``` | ||
|
||
`WITH batch` 是一个 **CTE(公用表表达式)**,临时选择出最多 1000 条未处理(`processed = false`)的记录。 | ||
|
||
`FOR UPDATE`: 将查询结果中的行加锁,防止其他事务同时修改这些行。 | ||
|
||
`SKIP LOCKED`: 如果某些行已经被其他事务加锁,则跳过这些行。 | ||
|
||
这可以避免事务因为等待锁而阻塞,提高并发性能。 |