Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
檀越@新空间
Coding Tree
提交
34ec7ab9
C
Coding Tree
项目概览
檀越@新空间
/
Coding Tree
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coding Tree
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
34ec7ab9
编写于
5月 08, 2022
作者:
彭世瑜
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix
上级
517683aa
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
468 addition
and
3 deletion
+468
-3
blog/mysq-advance/mysql-index.md
blog/mysq-advance/mysql-index.md
+420
-0
blog/php-mysql/index.md
blog/php-mysql/index.md
+1
-1
blog/php-mysql/sql-function.md
blog/php-mysql/sql-function.md
+11
-2
index.html
index.html
+35
-0
static/js/prism-python.min.js
static/js/prism-python.min.js
+1
-0
未找到文件。
blog/mysq-advance/mysql-index.md
浏览文件 @
34ec7ab9
...
...
@@ -867,3 +867,423 @@ mysql> explain select count(*) from tb_user;
1
row
in
set
,
1
warning
(
0
.
00
sec
)
```
索引使用
创建测试表
```
sql
create
table
tb_sku
(
id
int
primary
key
auto_increment
,
uuid
char
(
36
)
);
```
## 6、索引使用
### 6.1、验证索引效率
生成测试数据(1000万条记录)
方式一:使用MySQL的自定义函数
速度较慢,适合较少的数据
```
sql
-- 1、定义个生成测试数据的函数
-- 修改语句结束符
delimiter
$$
-- 创建函数
create
function
init_data
(
total
int
)
returns
int
begin
-- 声明局部变量
declare
i
int
default
0
;
-- 循环处理
while
i
<
total
do
-- 生成测试数据
insert
into
tb_sku
(
`uuid`
)
values
(
uuid
());
set
i
=
i
+
1
;
end
while
;
-- 返回值
return
i
;
end
-- 结束
$$
-- 修改语句结束符
delimiter
;
-- 2、调用函数生成数据(100W)
mysql
>
select
init_data
(
1000000
);
+
--------------------+
|
init_data
(
1000000
)
|
+
--------------------+
|
1000000
|
+
--------------------+
1
row
in
set
(
1
min
16
.
08
sec
)
-- 3、删除函数
drop
function
init_data
;
```
方式二:
使用Python脚本生成,适合生成大量数据
```
python
# pip install records mysqlclient
import
records
import
uuid
db
=
records
.
Database
(
'mysql://root:123456@localhost/data?charset=utf8'
)
# 100 * 100000 = 1000万条数据
for
i
in
range
(
100
):
data
=
[{
'uuid'
:
str
(
uuid
.
uuid4
())}
for
i
in
range
(
100000
)]
db
.
bulk_query
(
"insert into tb_sku(uuid) values(:uuid)"
,
data
)
```
```
sql
mysql
>
select
count
(
*
)
from
tb_sku
;
+
----------+
|
count
(
*
)
|
+
----------+
|
10000000
|
+
----------+
1
row
in
set
(
0
.
38
sec
)
mysql
>
select
*
from
tb_sku
limit
1
;
+
----+--------------------------------------+
|
id
|
uuid
|
+
----+--------------------------------------+
|
1
|
166
fa508
-
2911
-
494
d
-
aed2
-
2
a2ee81a2a64
|
+
----+--------------------------------------+
1
row
in
set
(
0
.
00
sec
)
```
验证索引效率
```
sql
-- 为建立索引之前,执行如下SQL,查看SQL的耗时
mysql
>
select
*
from
tb_sku
where
uuid
=
'166fa508-2911-494d-aed2-2a2ee81a2a64'
;
+
----+--------------------------------------+
|
id
|
uuid
|
+
----+--------------------------------------+
|
1
|
166
fa508
-
2911
-
494
d
-
aed2
-
2
a2ee81a2a64
|
+
----+--------------------------------------+
1
row
in
set
(
2
.
15
sec
)
-- 创建索引
mysql
>
create
index
idx_sku_uuid
on
tb_sku
(
uuid
);
Query
OK
,
0
rows
affected
(
21
.
85
sec
)
Records
:
0
Duplicates
:
0
Warnings
:
0
-- 再次执行相同的SQL语句,查看SQL耗时
mysql
>
select
*
from
tb_sku
where
uuid
=
'166fa508-2911-494d-aed2-2a2ee81a2a64'
;
+
----+--------------------------------------+
|
id
|
uuid
|
+
----+--------------------------------------+
|
1
|
166
fa508
-
2911
-
494
d
-
aed2
-
2
a2ee81a2a64
|
+
----+--------------------------------------+
1
row
in
set
(
0
.
01
sec
)
-- 通过主键查询
mysql
>
select
*
from
tb_sku
where
id
=
1
;
+
----+--------------------------------------+
|
id
|
uuid
|
+
----+--------------------------------------+
|
1
|
166
fa508
-
2911
-
494
d
-
aed2
-
2
a2ee81a2a64
|
+
----+--------------------------------------+
1
row
in
set
(
0
.
00
sec
)
-- 查看执行计划
mysql
>
explain
select
*
from
tb_sku
where
uuid
=
'166fa508-2911-494d-aed2-2a2ee81a2a64'
;
+
----+-------------+--------+------------+------+---------------+--------------+---------+-------+------+----------+-------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+--------+------------+------+---------------+--------------+---------+-------+------+----------+-------------+
|
1
|
SIMPLE
|
tb_sku
|
NULL
|
ref
|
idx_sku_uuid
|
idx_sku_uuid
|
145
|
const
|
1
|
100
.
00
|
Using
index
|
+
----+-------------+--------+------------+------+---------------+--------------+---------+-------+------+----------+-------------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
```
### 6.2、最左前缀法则
如果索引了多列(联合索引),要遵守最左前缀法则。
最左前缀法则指的是查询从索引的最左侧列开始,并且不跳过索引中的列。
如果跳过某一列,索引将部分失效(后面的字段索引失效)
```
sql
-- 查看表数据
select
*
from
tb_user
;
+
----+--------+-------------+------------+------+--------+-------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+-------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
|
2
|
关羽
|
15210231297
|
物理
|
24
|
1
|
3339
@
qq
.
com
|
|
3
|
刘备
|
15214231297
|
数学
|
25
|
0
|
666
@
qq
.
com
|
|
4
|
孙权
|
15215231297
|
语文
|
20
|
1
|
111
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+-------------+
4
rows
in
set
(
0
.
01
sec
)
-- 查看表中的索引
show
index
from
tb_user
;
+
---------+------------+--------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
|
Table
|
Non_unique
|
Key_name
|
Seq_in_index
|
Column_name
|
Collation
|
Cardinality
|
Sub_part
|
Packed
|
Null
|
Index_type
|
Comment
|
Index_comment
|
Visible
|
Expression
|
+
---------+------------+--------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
|
tb_user
|
0
|
PRIMARY
|
1
|
id
|
A
|
4
|
NULL
|
NULL
|
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
0
|
idx_user_phone
|
1
|
phone
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_name
|
1
|
name
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_profession_age_status
|
1
|
profession
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_profession_age_status
|
2
|
age
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_profession_age_status
|
3
|
status
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_email
|
1
|
email
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
+
---------+------------+--------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
7
rows
in
set
(
0
.
06
sec
)
-- 查询数据
select
*
from
tb_user
where
profession
=
'美术'
and
age
=
23
and
status
=
1
;
+
----+--------+-------------+------------+------+--------+------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+------------+
1
row
in
set
(
0
.
00
sec
)
-- 查看用到的索引:profession、age、status
explain
select
*
from
tb_user
where
profession
=
'美术'
and
age
=
23
and
status
=
1
;
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ref
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
53
|
const
,
const
,
const
|
1
|
100
.
00
|
NULL
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
-- 查询profession和age,使用到了索引
explain
select
*
from
tb_user
where
profession
=
'美术'
and
age
=
23
;
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------+------+----------+-------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------+------+----------+-------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ref
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
48
|
const
,
const
|
1
|
100
.
00
|
NULL
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------+------+----------+-------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
-- 单独查询profession,使用到了索引
explain
select
*
from
tb_user
where
profession
=
'美术'
;
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------+------+----------+-------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------+------+----------+-------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ref
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
43
|
const
|
1
|
100
.
00
|
NULL
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------+------+----------+-------+
1
row
in
set
,
1
warning
(
0
.
01
sec
)
-- 查询age和status,没有使用到索引
explain
select
*
from
tb_user
where
age
=
23
and
status
=
1
;
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ALL
|
NULL
|
NULL
|
NULL
|
NULL
|
4
|
25
.
00
|
Using
where
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
1
row
in
set
,
1
warning
(
0
.
01
sec
)
-- 单独查询status,没有使用到索引
explain
select
*
from
tb_user
where
status
=
1
;
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ALL
|
NULL
|
NULL
|
NULL
|
NULL
|
4
|
25
.
00
|
Using
where
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
-- 跳过中间的age字段,只用到了profession,而status没有用到索引
explain
select
*
from
tb_user
where
profession
=
'美术'
and
status
=
1
;
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------+------+----------+-----------------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------+------+----------+-----------------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ref
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
43
|
const
|
1
|
25
.
00
|
Using
index
condition
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------+------+----------+-----------------------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
-- 索引字段可以交换顺序,也能使用到索引
explain
select
*
from
tb_user
where
age
=
23
and
status
=
1
and
profession
=
'美术'
;
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ref
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
53
|
const
,
const
,
const
|
1
|
100
.
00
|
NULL
|
+
----+-------------+---------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
```
### 6.3、范围查询
联合索引中,出现范围查询(>、<),范围查询右侧的列索引失效
```
sql
select
*
from
tb_user
where
profession
=
'美术'
and
age
>
22
and
status
=
1
;
+
----+--------+-------------+------------+------+--------+------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+------------+
1
row
in
set
(
0
.
00
sec
)
-- age使用了大于号>,只有profession和age用到了索引
explain
select
*
from
tb_user
where
profession
=
'美术'
and
age
>
22
and
status
=
1
;
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
range
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
48
|
NULL
|
1
|
25
.
00
|
Using
index
condition
|
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
1
row
in
set
,
1
warning
(
0
.
01
sec
)
-- 三个字段都用到了索引
explain
select
*
from
tb_user
where
profession
=
'美术'
and
age
>=
22
and
status
=
1
;
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
range
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
53
|
NULL
|
1
|
25
.
00
|
Using
index
condition
|
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
```
## 7、索引失效的场景
### 7.1、索引列运算
不要在索引列上进行运算操作,否则索引将失效
```
sql
-- 查看数据
mysql
>
select
*
from
tb_user
;
+
----+--------+-------------+------------+------+--------+-------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+-------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
|
2
|
关羽
|
15210231297
|
物理
|
24
|
1
|
3339
@
qq
.
com
|
|
3
|
刘备
|
15214231297
|
数学
|
25
|
0
|
666
@
qq
.
com
|
|
4
|
孙权
|
15215231297
|
语文
|
20
|
1
|
111
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+-------------+
4
rows
in
set
(
0
.
00
sec
)
-- 查看索引
mysql
>
show
index
from
tb_user
;
+
---------+------------+--------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
|
Table
|
Non_unique
|
Key_name
|
Seq_in_index
|
Column_name
|
Collation
|
Cardinality
|
Sub_part
|
Packed
|
Null
|
Index_type
|
Comment
|
Index_comment
|
Visible
|
Expression
|
+
---------+------------+--------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
|
tb_user
|
0
|
PRIMARY
|
1
|
id
|
A
|
4
|
NULL
|
NULL
|
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
0
|
idx_user_phone
|
1
|
phone
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_name
|
1
|
name
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_profession_age_status
|
1
|
profession
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_profession_age_status
|
2
|
age
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_profession_age_status
|
3
|
status
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
|
tb_user
|
1
|
idx_user_email
|
1
|
email
|
A
|
4
|
NULL
|
NULL
|
YES
|
BTREE
|
|
|
YES
|
NULL
|
+
---------+------------+--------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
7
rows
in
set
(
0
.
01
sec
)
-- 按照手机号查询数据
mysql
>
select
*
from
tb_user
where
phone
=
'15210231227'
;
+
----+--------+-------------+------------+------+--------+------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+------------+
1
row
in
set
(
0
.
00
sec
)
-- 分析索引使用情况
explain
select
*
from
tb_user
where
phone
=
'15210231227'
;
+
----+-------------+---------+------------+-------+----------------+----------------+---------+-------+------+----------+-------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+-------+----------------+----------------+---------+-------+------+----------+-------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
const
|
idx_user_phone
|
idx_user_phone
|
47
|
const
|
1
|
100
.
00
|
NULL
|
+
----+-------------+---------+------------+-------+----------------+----------------+---------+-------+------+----------+-------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
-- 查询手机号后两位
select
*
from
tb_user
where
substring
(
phone
,
10
,
2
)
=
'27'
;
+
----+--------+-------------+------------+------+--------+------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+------------+
1
row
in
set
(
0
.
00
sec
)
-- 查看执行效率
explain
select
*
from
tb_user
where
substring
(
phone
,
10
,
2
)
=
'27'
;
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ALL
|
NULL
|
NULL
|
NULL
|
NULL
|
4
|
100
.
00
|
Using
where
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
1
row
in
set
,
1
warning
(
0
.
01
sec
)
```
### 7.2、字符串不加引号
字符串类型字段使用时,不加引号,索引将失效
```
sql
-- 字符串查询可以不加引号
select
*
from
tb_user
where
phone
=
15210231227
;
+
----+--------+-------------+------------+------+--------+------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+------------+
1
row
in
set
(
0
.
00
sec
)
-- 不加引号的字符串查询没有走索引
explain
select
*
from
tb_user
where
phone
=
15210231227
;
+
----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ALL
|
idx_user_phone
|
NULL
|
NULL
|
NULL
|
4
|
25
.
00
|
Using
where
|
+
----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+
1
row
in
set
,
3
warnings
(
0
.
00
sec
)
```
### 7.3、模糊查询
-
如果仅仅是尾部模糊匹配,索引不会失效;
-
如果是头部模糊匹配,索引失效
```
sql
-- 尾部模糊查询
select
*
from
tb_user
where
profession
like
'美%'
;
+
----+--------+-------------+------------+------+--------+------------+
|
id
|
name
|
phone
|
profession
|
age
|
status
|
email
|
+
----+--------+-------------+------------+------+--------+------------+
|
1
|
张飞
|
15210231227
|
美术
|
23
|
1
|
123
@
qq
.
com
|
+
----+--------+-------------+------------+------+--------+------------+
1
row
in
set
(
0
.
00
sec
)
-- 尾部模糊匹配,索引不会失效;
explain
select
*
from
tb_user
where
profession
like
'美%'
;
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
range
|
idx_user_profession_age_status
|
idx_user_profession_age_status
|
43
|
NULL
|
1
|
100
.
00
|
Using
index
condition
|
+
----+-------------+---------+------------+-------+--------------------------------+--------------------------------+---------+------+------+----------+-----------------------+
1
row
in
set
,
1
warning
(
0
.
00
sec
)
-- 头部模糊匹配,索引失效
explain
select
*
from
tb_user
where
profession
like
'%术'
;
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
id
|
select_type
|
table
|
partitions
|
type
|
possible_keys
|
key
|
key_len
|
ref
|
rows
|
filtered
|
Extra
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|
1
|
SIMPLE
|
tb_user
|
NULL
|
ALL
|
NULL
|
NULL
|
NULL
|
NULL
|
4
|
25
.
00
|
Using
where
|
+
----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
1
row
in
set
,
1
warning
(
0
.
01
sec
)
```
blog/php-mysql/index.md
浏览文件 @
34ec7ab9
...
...
@@ -60,7 +60,7 @@
[
流程结构 if while
](
blog/php-mysql/sql-if-while.md
)
[
函数 function
](
blog/php-mysql/sql-function.md
)
[
内置函数和自定义
函数 function
](
blog/php-mysql/sql-function.md
)
[
存储过程 procedure
](
blog/php-mysql/sql-procedure.md
)
...
...
blog/php-mysql/sql-function.md
浏览文件 @
34ec7ab9
# 函数 function
#
内置函数和自定义
函数 function
函数分为两类:系统函数和自定义函数
...
...
@@ -19,9 +19,12 @@ length | 判断字符串的字节数,与字符集有关
concat | 连接字符串
insrt | 检查字符是否在目标字符串中,存在返回其位置,不存在返回 0
lcase | 全部小写
left | 左侧开始截取字符串,直到指定位置
ltrim | 消除左边的空格
left(str, length) | 左侧开始截取字符串,直到指定位置
right(str, length) | 右侧开始截取字符串,直到指定位置
mid | 从中间指定位置开始截取,如果不指定截取长度,直接到最后
`substring(str, index, [length]`
) | 从指定位置开始,指定截取长度
substring_index(str, delim, count) | 按照关键字截取
示例
...
...
@@ -39,9 +42,15 @@ select instr('你好中国', '我'); // 0
select
lcase
(
'aBcd'
);
//
abcd
select
left
(
'aBcd'
,
2
);
//
aB
select
right
(
'abcdef'
,
2
);
//
ef
select
substring
(
'abcdef'
,
2
,
3
);
//
bcd
select
substring
(
'abcdef'
,
-
2
,
3
);
//
ef
select
ltrim
(
' abc d '
);
//
abc
d
select
mid
(
'你好中国'
,
3
);
//
中国
select
substring_index
(
'www.baidu.com'
,
'.'
,
2
);
//
www
.
baidu
select
substring_index
(
'www.baidu.com'
,
'.'
,
-
2
);
//
baidu
.
com
```
### 1.2、时间函数
...
...
index.html
浏览文件 @
34ec7ab9
...
...
@@ -18,6 +18,15 @@
href=
"static/js/vue@4.12.2.min.css"
>
<style>
:root
{
--toc-width
:
300px
;
}
/* 左侧导航 */
.sidebar
{
width
:
220px
;
}
/* 更新时间 */
.last-update-time-wrap
{
padding
:
20px
0
;
...
...
@@ -33,7 +42,29 @@
/* 右侧目录 */
.page_toc
{
margin-top
:
20px
;
max-height
:
calc
(
100vh
-
100px
);
overflow-y
:
auto
;
/* height: 100px; */
}
.page_toc
::-webkit-scrollbar
{
width
:
4px
}
.page_toc
::-webkit-scrollbar-thumb
{
background
:
0
0
;
border-radius
:
4px
}
.page_toc
:hover::-webkit-scrollbar-thumb
{
background
:
hsla
(
0
,
0%
,
53.3%
,
.4
)
}
.page_toc
:hover::-webkit-scrollbar-track
{
background
:
hsla
(
0
,
0%
,
53.3%
,
.1
)
}
</style>
</head>
...
...
@@ -85,12 +116,16 @@
<script
src=
"static/js/prism-bash.min.js"
></script>
<script
src=
"static/js/prism-php.min.js"
></script>
<script
src=
"static/js/prism-sql.min.js"
></script>
<script
src=
"static/js/prism-python.min.js"
></script>
<script
src=
"static/js/prism-json@1.26.0.min.js"
></script>
<script
src=
"static/js/search@4.12.2.min.js"
></script>
<script
src=
"static/js/docsify-copy-code@2.1.1.min.js"
></script>
<script
src=
"static/js/time-updater@1.min.js"
></script>
<!-- 下载地址 -->
<!-- https://cdn.jsdelivr.net/npm/prismjs@1/components/prism-python.min.js-->
<link
rel=
"stylesheet"
href=
"static/js/toc.css"
>
<script
src=
"static/js/toc.js"
></script>
...
...
static/js/prism-python.min.js
0 → 100644
浏览文件 @
34ec7ab9
Prism
.
languages
.
python
=
{
comment
:{
pattern
:
/
(
^|
[^\\])
#.*/
,
lookbehind
:
!
0
,
greedy
:
!
0
},
"
string-interpolation
"
:{
pattern
:
/
(?:
f|fr|rf
)(?:(
"""|'''
)[\s\S]
*
?\1
|
(
"|'
)(?:\\
.|
(?!\2)[^\\\r\n])
*
\2)
/i
,
greedy
:
!
0
,
inside
:{
interpolation
:{
pattern
:
/
((?:
^|
[^
{
])(?:\{\{)
*
)\{(?!\{)(?:[^
{}
]
|
\{(?!\{)(?:[^
{}
]
|
\{(?!\{)(?:[^
{}
])
+
\})
+
\})
+
\}
/
,
lookbehind
:
!
0
,
inside
:{
"
format-spec
"
:{
pattern
:
/
(
:
)[^
:(){}
]
+
(?=\}
$
)
/
,
lookbehind
:
!
0
},
"
conversion-option
"
:{
pattern
:
/!
[
sra
](?=[
:}
]
$
)
/
,
alias
:
"
punctuation
"
},
rest
:
null
}},
string
:
/
[\s\S]
+/
}},
"
triple-quoted-string
"
:{
pattern
:
/
(?:[
rub
]
|br|rb
)?(
"""|'''
)[\s\S]
*
?\1
/i
,
greedy
:
!
0
,
alias
:
"
string
"
},
string
:{
pattern
:
/
(?:[
rub
]
|br|rb
)?(
"|'
)(?:\\
.|
(?!\1)[^\\\r\n])
*
\1
/i
,
greedy
:
!
0
},
function
:{
pattern
:
/
((?:
^|
\s)
def
[
\t]
+
)[
a-zA-Z_
]\w
*
(?=\s
*
\()
/g
,
lookbehind
:
!
0
},
"
class-name
"
:{
pattern
:
/
(\b
class
\s
+
)\w
+/i
,
lookbehind
:
!
0
},
decorator
:{
pattern
:
/
(
^
[\t
]
*
)
@
\w
+
(?:\.\w
+
)
*/m
,
lookbehind
:
!
0
,
alias
:[
"
annotation
"
,
"
punctuation
"
],
inside
:{
punctuation
:
/
\.
/
}},
keyword
:
/
\b(?:
_
(?=\s
*:
)
|and|as|assert|async|await|break|case|class|continue|def|del|elif|else|except|exec|finally|for|from|global|if|import|in|is|lambda|match|nonlocal|not|or|pass|print|raise|return|try|while|with|yield
)\b
/
,
builtin
:
/
\b(?:
__import__|abs|all|any|apply|ascii|basestring|bin|bool|buffer|bytearray|bytes|callable|chr|classmethod|cmp|coerce|compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|intern|isinstance|issubclass|iter|len|list|locals|long|map|max|memoryview|min|next|object|oct|open|ord|pow|property|range|raw_input|reduce|reload|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip
)\b
/
,
boolean
:
/
\b(?:
False|None|True
)\b
/
,
number
:
/
\b
0
(?:
b
(?:
_
?[
01
])
+|o
(?:
_
?[
0-7
])
+|x
(?:
_
?[
a-f0-9
])
+
)\b
|
(?:\b\d
+
(?:
_
\d
+
)
*
(?:\.(?:\d
+
(?:
_
\d
+
)
*
)?)?
|
\B\.\d
+
(?:
_
\d
+
)
*
)(?:
e
[
+-
]?\d
+
(?:
_
\d
+
)
*
)?
j
?(?!\w)
/i
,
operator
:
/
[
-+%=
]
=
?
|!=|:=|
\*\*?
=
?
|
\/\/?
=
?
|<
[
<=>
]?
|>
[
=>
]?
|
[
&|^~
]
/
,
punctuation
:
/
[
{}[
\]
;(),.:
]
/
},
Prism
.
languages
.
python
[
"
string-interpolation
"
].
inside
.
interpolation
.
inside
.
rest
=
Prism
.
languages
.
python
,
Prism
.
languages
.
py
=
Prism
.
languages
.
python
;
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录