Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_mysql
提交
d2fc511a
S
skill_tree_mysql
项目概览
CSDN 技术社区
/
skill_tree_mysql
通知
21
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_mysql
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
d2fc511a
编写于
5月 27, 2022
作者:
M
Mars Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cte
上级
4bf40e72
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
203 addition
and
4 deletion
+203
-4
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/config.json
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/config.json
+2
-1
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/to_root.json
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/to_root.json
+8
-0
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/to_root.md
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/to_root.md
+68
-0
data/3.MySQL高阶/3.SQL高级技巧/7.索引和优化/daily_payment.md
data/3.MySQL高阶/3.SQL高级技巧/7.索引和优化/daily_payment.md
+1
-1
data/3.MySQL高阶/5.设计优化/6.增加中间表/config.json
data/3.MySQL高阶/5.设计优化/6.增加中间表/config.json
+7
-2
data/3.MySQL高阶/5.设计优化/6.增加中间表/daily_payment2.json
data/3.MySQL高阶/5.设计优化/6.增加中间表/daily_payment2.json
+8
-0
data/3.MySQL高阶/5.设计优化/6.增加中间表/daily_payment2.md
data/3.MySQL高阶/5.设计优化/6.增加中间表/daily_payment2.md
+109
-0
未找到文件。
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/config.json
浏览文件 @
d2fc511a
...
@@ -7,7 +7,8 @@
...
@@ -7,7 +7,8 @@
],
],
"children"
:
[],
"children"
:
[],
"export"
:
[
"export"
:
[
"continuous.json"
"continuous.json"
,
"to_root.json"
],
],
"keywords_must"
:
[
"keywords_must"
:
[
"cte"
,
"cte"
,
...
...
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/to_root.json
0 → 100644
浏览文件 @
d2fc511a
{
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"source"
:
"to_root.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"f249526bc9294eb886de5570896cdbf3"
}
\ No newline at end of file
data/3.MySQL高阶/3.SQL高级技巧/1.CTE和递归查询/to_root.md
0 → 100644
浏览文件 @
d2fc511a
# 树结构溯根
现有一个表 node
```
mysql
create table node
(
id int primary key auto_increment,
pid integer,
val integer
);
```
其 pid 列引用 id 列,形成一个树结构,根节点的 pid 为 0。
现在我们希望写一个查询,找到某一个给定id的记录,其父节点、父节点的父节点,直至根节点的路径。那么这个查询应该是:
## 答案
```
mysql
with recursive t(id, pid, val) as (
select id, pid, val
from node
where id = $1
union all
select node.id, node.pid, node.val
from node
join t on node.id = t.pid)
select node.id, node.pid, node.val
from node
join t on node.id = t.id;
```
## 选项
### 没有递归定义
```
mysql
with t as (
select id, pid, val
from node
where id = $1
union all
select node.id, node.pid, node.level
from node
join t on node.id = t.pid)
select node.id, node.pid, node.val
from node
join t on node.id = t.id;
```
### 平凡的连接查询无法处理递归问题
```
mysql
select node.id, node.pid, node.val
from node
join node as p on node.pid = p.id
where id = $1;
```
### 子查询无法处理递归问题
```
mysql
select node.id, node.pid, node val
from node as t
where t.pid = (select id from t where id = t.pid)
```
\ No newline at end of file
data/3.MySQL高阶/3.SQL高级技巧/7.索引和优化/daily_payment.md
浏览文件 @
d2fc511a
...
@@ -20,7 +20,7 @@ mysql> desc payment;
...
@@ -20,7 +20,7 @@ mysql> desc payment;
| customer_id | int | YES | | NULL | |
| customer_id | int | YES | | NULL | |
| staff_id | int | YES | | NULL | |
| staff_id | int | YES | | NULL | |
| rental_id | int | YES | | NULL | |
| rental_id | int | YES | | NULL | |
| amount | decimal(12,
8
) | YES | | NULL | |
| amount | decimal(12,
4
) | YES | | NULL | |
| pathment_date | timestamp | YES | | NULL | |
| pathment_date | timestamp | YES | | NULL | |
+---------------+---------------+------+-----+---------+----------------+
+---------------+---------------+------+-----+---------+----------------+
6 rows in set (0.12 sec)
6 rows in set (0.12 sec)
...
...
data/3.MySQL高阶/5.设计优化/6.增加中间表/config.json
浏览文件 @
d2fc511a
{
{
"node_id"
:
"mysql-a2ddae1b044149ecbb74db3b6eb32721"
,
"node_id"
:
"mysql-a2ddae1b044149ecbb74db3b6eb32721"
,
"keywords"
:
[
"middle"
,
"中间表"
],
"keywords"
:
[
"middle"
,
"中间表"
],
"children"
:
[],
"children"
:
[],
"export"
:
[],
"export"
:
[
"daily_payment2.json"
],
"keywords_must"
:
[],
"keywords_must"
:
[],
"keywords_forbid"
:
[],
"keywords_forbid"
:
[],
"group"
:
0
"group"
:
0
...
...
data/3.MySQL高阶/5.设计优化/6.增加中间表/daily_payment2.json
0 → 100644
浏览文件 @
d2fc511a
{
"type"
:
"code_options"
,
"author"
:
"ccat"
,
"source"
:
"daily_payment2.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"71343fd41e5d4af5a5876849e77a3be2"
}
\ No newline at end of file
data/3.MySQL高阶/5.设计优化/6.增加中间表/daily_payment2.md
0 → 100644
浏览文件 @
d2fc511a
# 每日报表优化
# 每日报表
分析过去一段时间的查询数据,Joe 发现 payment 表
```
mysql
create table payment(
payment_id int primary key auto_increment,
customer_id int,
staff_id int,
rental_id int,
amount decimal(12, 4),
payment_date timestamp
)
```
每天的订单量很大,下面这个查询的统计过程占用了大多数数据库资源,查询不会
早于当天,总是在历史上某一个日期段内查询
```
mysql
select date(payment_date) as day, sum(amount)
from payment
where date(payment_date) between $1 and $2
group by date(payment_date);
```
怎样优化最有效?
## 答案
建立中间表并建立索引。
```
postgresql
create table daily_payment(day date primary key , amount decimal(12, 4));
insert into daily_payment(day, amount)
select payment_date::date as day, sum(amount) as amount from payment group by day;
```
使用
```
mysql
select day, amount from view_daily_payment where day between $1 and $2;
```
进行查询。并且每天定时执行一次刷新命令
```
mysql
insert into daily_payment(day, amount)
select date(payment_date) as day, sum(amount) as amount
from payment
where date(payment_date) between DATE_SUB(CURDATE(), INTERVAL 2 DAY) and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
group by day;
```
## 选项
### 不会优化 sum 和 group by
在 payment_date 列上建立索引
```
mysql
create index idx_payment_date on payment(payment_date);
```
### 不会优化 sum
建立计算列
```
mysql
alter table payment add day date generated always as ( payment_date::date ) stored
```
然后使用它改写查询
```
mysql
select day as day, sum(amount)
from payment
where day between $1 and DATE_SUB(CURDATE(), INTERVAL 1 DAY)
group by day;
```
### 优化了日期查询,但是不会优化统计过程
建立表达式索引
```
mysql
create index idx_payment_day on payment((date(payment_date)));
```
### 不做物化,对查询速度不会有显著改善
建立视图
```
mysql
create view view_daily_payment as select date(payment_date) as day, amount from payment;
```
然后在视图 view_daily_payment 上执行
```
mysql
select day, sum(amount)
from view_daily_payment
where day between $1 and date('yesterday')
group by day
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录