提交 ca4b724b 编写于 作者: M Mars Liu

重构章节顺序

上级 ce51c641
{
"node_id": "mysql-bee8db3dd5354a42bb50277fe4cb2913",
"keywords": [],
"children": [],
"export": [
"customer_order.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 2
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "customer_order.md",
"notebook_enable": false,
"exercise_id": "37f64868b412458c8c584a7ab7941792"
}
\ No newline at end of file
# 客户和订单
Goods 中有客户/订单系统如下:
```mysql
create table customers
(
id serial primary key,
company_name varchar(256),
address varchar(1024),
city varchar(256),
state varchar(256)
);
create table products
(
id serial primary key,
description varchar(1024),
unit_price decimal(12, 4)
);
create table orders
(
id serial primary key,
product_id integer references products (id),
order_date timestamp,
quantity integer,
customer_id integer references customers(id)
);
```
我们希望这个数据库能够允许每个订单包含多种商品,那么应该如何改造?
## 答案
* 添加一个 order_detail 表,引用 order id、product id,增加 quantity 列
* order 表中删除 product id 和 quantity 列
## 选项
### A
将 order 表的修改为以 product id 和 customer id 作为联合主键
### B
删除 orders 表中的 product id 和 quantity 列
### C
在 order 表的主键上加唯一约束
\ No newline at end of file
{
"node_id": "mysql-3e4c837b471c454c90bfbc32445f2780",
"keywords": ["反范式", "anti pattern"],
"children": [],
"export": [
"slack.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "slack.md",
"notebook_enable": false,
"exercise_id": "d62e33f550494f849f87b62acdcf9d0a"
}
\ No newline at end of file
# 反范式设计
反范式优化的主要动机和思路是:
1. 通过适当增加冗余字段,减少连接查询的次数和复杂度
2. 对于经常发生的聚合计算,如果对实时正确性要求不高,可以缓存中间结果,减少实时的聚合计算压力
3. 用适度的写冗余,换取读操作的大幅优化
4. 适当的放宽索引,可以优化局限于少数字段的查询性能
## 答案
全部都是
## 选项
### A
全部都不对
### B
```
1, 2, 3
```
### C
```
2, 3, 4
```
### D
```
2, 3
```
### E
```
3, 4
```
\ No newline at end of file
{
"node_id": "mysql-a2ddae1b044149ecbb74db3b6eb32721",
"keywords": [
"middle",
"中间表"
],
"children": [],
"export": [
"daily_payment2.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "daily_payment2.md",
"notebook_enable": false,
"exercise_id": "71343fd41e5d4af5a5876849e77a3be2"
}
\ No newline at end of file
# 每日报表优化
# 每日报表
分析过去一段时间的查询数据,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
{
"node_id": "mysql-cde6eae01784468fa0171ff3042f221f",
"keywords": ["analyze table", "分析数据表"],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-fbcc654d6a86452aa349d2ed6003adb6",
"keywords": ["show profile"],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-f5775462ae784d44a4b2bce5f456311a",
"keywords": ["performance table", "优化数据表"],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"node_id": "mysql-ccb6fcf2983b4b3e8b37abbcbc776f21",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
......@@ -19,13 +19,13 @@
],
"children": [
{
"数据库基本概念": {
"node_id": "mysql-8e971f3c569a4e56bb83b5d75a2a425f",
"MySQL 基本概念": {
"node_id": "mysql-925f9d57c7794e5391fb790bcdbf43c6",
"keywords": [],
"children": [],
"keywords_must": [
[
"数据库",
"mysql",
"概念"
]
],
......@@ -34,13 +34,13 @@
}
},
{
"MySQL 基本概念": {
"node_id": "mysql-925f9d57c7794e5391fb790bcdbf43c6",
"数据库基本概念": {
"node_id": "mysql-8e971f3c569a4e56bb83b5d75a2a425f",
"keywords": [],
"children": [],
"keywords_must": [
[
"mysql",
"数据库",
"概念"
]
],
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册