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

fixed server and client

上级 a2f71bf2
# MySQL 服务器 # 连接 MySQL 数据库
关于 MySQL 服务器,以下说法错误的是 下列选项中,可以用于连接到 MySQL 数据库的有
## 答案 ## 答案
PostgreSQL 的服务器和客户端必须运行在不同的计算机上。 全部都可以
## 选项 ## 选项
### A ### A
MySQL 允许不同的数据表使用不同的存储引擎。 命令行工具 mysql
### B ### B
MySQL 可以为不同的用户连接设置资源组,限制其使用的资源。 GUI 工具 phpMyAdmin
### C ### C
MySQL 可以设置监听端口、地址和最大连接数 JDBC 库 `mysql:mysql-connector-java`
### D ### D
MySQL 的超级用户默认为 root Python DBAPI 实现 MySQLdb
### E ### E
MySQL 可以运行在 FreeBSD、Linux或Windows系统。 Go 语言驱动库 `github.com/go-sql-driver/mysql`
\ No newline at end of file
# {在此填写标题} # PostgreSQL 服务器
{在此填写题目描述} 关于 PostgreSQL 服务器,以下说法错误的是:
## 答案 ## 答案
{在此填写答案} PostgreSQL 的服务器和客户端必须运行在不同的计算机上。
## 选项 ## 选项
### A ### A
{在此填写选项A} PostgreSQL 的每个活跃连接对应一个 worker 进程。
### B ### B
{在此填写选项B} PostgreSQL 的服务器将数据存储在一个 owner 为 postgres 的目录下。
### C ### C
{在此填写选项C} PostgreSQL 可以设置监听端口、地址和最大连接数
### D
PostgreSQL 的超级用户默认为 postgres
### E
PostgreSQL 可以运行在 FreeBSD、Linux或Windows系统。
{
"node_id": "mysql-e5530d16129047d38934c3ce9a906650",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"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": [],
"children": [],
"export": [
"slack.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ 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": [],
"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": [],
"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
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
"node_id": "mysql-cc8606a40aac43cb8b063a65f51e4df1", "node_id": "mysql-cc8606a40aac43cb8b063a65f51e4df1",
"keywords": [], "keywords": [],
"children": [], "children": [],
"export": [], "export": [
"slave_master.json"
],
"keywords_must": [], "keywords_must": [],
"keywords_forbid": [], "keywords_forbid": [],
"group": 0 "group": 0
......
{ {
"type": "code_options", "type": "code_options",
"author": "ccat", "author": "ccat",
"source": "slack.md", "source": "slave_master.md",
"notebook_enable": false, "notebook_enable": false,
"exercise_id": "d62e33f550494f849f87b62acdcf9d0a" "exercise_id": "117eea79277e4a7199307f48e96e2998"
} }
\ No newline at end of file
...@@ -2236,16 +2236,6 @@ ...@@ -2236,16 +2236,6 @@
"group": 2 "group": 2
} }
}, },
{
"优化数据类型": {
"node_id": "mysql-e5530d16129047d38934c3ce9a906650",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{ {
" 范式设计": { " 范式设计": {
"node_id": "mysql-bee8db3dd5354a42bb50277fe4cb2913", "node_id": "mysql-bee8db3dd5354a42bb50277fe4cb2913",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册