提交 93e78662 编写于 作者: M Mars Liu

index

上级 9d06ce74
{
"type": "code_options",
"author": "ccat",
"author": "Mars",
"source": "sum.md",
"notebook_enable": false,
"exercise_id": "6ac9bb99a6684023ad80fde36ea32f6a"
"exercise_id": "1beebe6f7dcb4c8db11cc6d5151c395d"
}
\ No newline at end of file
{
"node_id": "mysql-db7153c698e8442eb5f71e8ec2f2e6cd",
"keywords": ["unique"],
"keywords": [
"unique"
],
"children": [],
"export": [],
"export": [
"unique.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 1
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "unique.md",
"notebook_enable": false,
"exercise_id": "959101e435844130811d359d7abcab04"
}
\ No newline at end of file
# 唯一约束
Goods 表结构如下:
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
Joe 需要确保同一个类型下没有重名的商品,他应该怎么做?
## 答案
```mysql
alter table goods add unique index (category_id, name);
```
## 选项
### A
```mysql
alter table goods add index (category_id, name);
```
### B
```mysql
alter table goods add unique index (category_id + name);
```
### C
```mysql
alter table goods add unique index (concat(category_id, name));
```
{
"node_id": "mysql-391b51e398764531abf5a6e036699cc8",
"keywords": ["equals", "not equals",
"相等", "不等", "全值"],
"keywords": [
"equals",
"not equals",
"相等",
"不等",
"全值"
],
"children": [],
"export": [],
"export": [
"total_index.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 2
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "limit_action.md",
"notebook_enable": false,
"exercise_id": "2c68494e5e9f4f9b835bd5758d61f6b9"
}
\ No newline at end of file
# 全值匹配
Goods 表结构如下:
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
现在有大量根据商品名获取价格的查询`select price from goods where name = '...''`,Joe希望进行优化,那么他应该:
## 答案
```mysql
alter table goods add index (name, price);
```
## 选项
### A
将 name 和 price 合成一个字段。
### B
建立一个计算字段:
```mysql
alter table goods add summary varchar(1024) generated always as (concat(name, '(', 0.5, ')'));
```
### C
```mysql
alter table goods add index (concat(name, price));
```
\ No newline at end of file
......@@ -2,7 +2,9 @@
"node_id": "mysql-bdc593e4b7ea4c8aa248227ff2e1221d",
"keywords": [],
"children": [],
"export": [],
"export": [
"match_fields.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "match_fields.md",
"notebook_enable": false,
"exercise_id": "7d5c09ded4ff44dda7928f0d902a9afe"
}
\ No newline at end of file
# 左匹配
Goods 表结构如下:
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp,
index (category_id, name)
)
```
Joe 发现有大量查询 `select id, category_id, name, price from goods where name=? and category_id=?`
性能很差,应该如何优化?
## 答案
将该查询改写为
```mysql
select id, category_id, name, price from goods where category_id=? and name=?;
```
## 选项
### A
将该查询改写为
```mysql
select id, category_id, name, price from goods where category_id and name= (?, ?);
```
### B
将该查询改写为
```mysql
select id, category_id, name, price from goods where not (category_id !=? or name != ?);
```
### C
将该查询改写为
```mysql
select id, category_id, name, price from goods where not (category_id !=?) and not (name != ?);
```
{
"node_id": "mysql-2b57deb6baa445ca87e66143624164bb",
"keywords": ["match", "range"],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
{
"type": "code_options",
"author": "ccat",
"source": "combinate.md",
"notebook_enable": false,
"exercise_id": "163ad81217b246b5a3e8819ecffa8411"
}
\ No newline at end of file
# 组合索引
Goods 表结构如下:
```mysql
create table goods(
id int primary key auto_increment,
category_id int,
category varchar(64),
name varchar(256),
price decimal(12, 4),
stock int,
upper_time timestamp
)
```
现有大量查询 `select id, category_id, name, price, stock from goods where stock=? and category_id=? and name like ?`
Joe 应该如何优化?
## 答案
```mysql
alter table goods add index (stock, category_id, name);
```
## 选项
### A
```mysql
alter table goods add index (stock and category_id and name);
```
### B
```mysql
alter table goods add index (concat(stock, category_id, name));
```
### C
```mysql
alter table goods add index (stock + category_id + name);
```
{
"node_id": "mysql-ce1f863b4d254d2aadb6ce2331122d16",
"keywords": ["compose"],
"keywords": [
"compose"
],
"children": [],
"export": [],
"export": [
"combinate.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 1
}
\ No newline at end of file
{
"node_id": "mysql-8e4a5a0e19e04932b9680ea24364fdd4",
"keywords": [],
"children": [],
"export": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
\ No newline at end of file
......@@ -10,5 +10,5 @@
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 2
}
\ No newline at end of file
{
"node_id": "mysql-c57e1195b0914cd38798ce029156ec87",
"keywords": [],
"keywords": ["invisible", "visible", "隐藏索引"],
"children": [],
"export": [
"invisible.json"
......
......@@ -1460,7 +1460,7 @@
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 1
}
},
{
......@@ -1476,7 +1476,7 @@
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 2
}
},
{
......@@ -1490,7 +1490,7 @@
}
},
{
"前缀匹配": {
"匹配顺序": {
"node_id": "mysql-bdc593e4b7ea4c8aa248227ff2e1221d",
"keywords": [],
"children": [],
......@@ -1499,29 +1499,6 @@
"group": 0
}
},
{
"精确匹配与范围匹配": {
"node_id": "mysql-2b57deb6baa445ca87e66143624164bb",
"keywords": [
"match",
"range"
],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
"连接查询与索引": {
"node_id": "mysql-8e4a5a0e19e04932b9680ea24364fdd4",
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
}
},
{
"组合索引": {
"node_id": "mysql-ce1f863b4d254d2aadb6ce2331122d16",
......@@ -1531,7 +1508,7 @@
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 1
}
},
{
......@@ -1554,7 +1531,7 @@
"children": [],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
"group": 2
}
},
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册