26wan数据mysql优化实践之一_MySQL学习笔记(三)数据优化

本文档详细介绍了MySQL数据库优化的关键步骤,包括慢查日志的分析与使用工具(如mysqldumpslow和pt-query-digest),选择和创建索引,优化SQL语句(如COUNT(), MAX()和子查询),以及数据库结构(如数据类型、范式化与拆分)的调整。涵盖了从基本查询优化到系统配置的全面内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第1章 数据库优化简介

1-1 MySQL优化简介

0569c7837826fcd6c84d60328382dfc5.png

第2章 SQL语句优化

2-1 数据准备

2-2 MySQL慢查日志的开启方式

b28a962bf84d2a5a3a3465f545498da7.png

2-3 MySQL慢查日志分析工具之 mysqldumpslow

www@AliYun:~$ sudo mysqldumpslow -t 3 /var/log/mysql/slow.log | more

Reading mysql slow query log from /var/log/mysql/slow.log

Count: 1 Time=0.16s (0s) Lock=0.00s (0s) Rows=0.0 (0), www[www]@[115.193.170.169]

DELETE FROM `resty_invitation`

Count: 1 Time=0.14s (0s) Lock=0.00s (0s) Rows=1000.0 (1000), www[www]@[115.193.170.169]

SELECT * FROM `resty_logs` LIMIT N, N

Count: 2 Time=0.05s (0s) Lock=0.01s (0s) Rows=0.0 (0), []@[]

throttle: N 'S' warning(s) suppressed.

2-4 MySQL慢查日志分析工具之 pt-querey-diget

4245eae5be3a86c1d42bf08299e4b142.png

2-5 如何通过慢查日志发现有问

a5349e8e12e882ab69bd68f4116a7c1d.png

2-6 通过explain查询和分析SQL的执行计划

mysql> explain select username,logintime,status,expire from resty_user limit 4;

+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |

+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+

| 1 | SIMPLE | resty_user | NULL | ALL | NULL | NULL | NULL | NULL | 9 | 100.00 | NULL |

+----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+-------+

1 row in set, 1 warning (0.01 sec)

3595b9c0667f30c082932e8bf46df956.png

c37338f3c81cdf60e6a4cbff8faccdbb.png

2-7 Count()和Max()的优化

mysql> explain select MAX(fileSize) from resty_stream_video \G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: resty_stream_video

partitions: NULL

type: ALL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 177

filtered: 100.00

Extra: NULL

1 row in set, 1 warning (0.00 sec)

建立索引

create index idx_fileSize on resty_stream_video(fileSize);

继续查询

mysql> explain select MAX(fileSize) from resty_stream_video \G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: NULL

partitions: NULL

type: NULL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: NULL

filtered: NULL

Extra: Select tables optimized away

1 row in set, 1 warning (0.00 sec)

77df3f4cb5093645c9a140799846628a.png

c744cc1b412814c86d8d9d18621542ed.png

38426e71ef718a34f79df060a2772714.png

count(*)和count(某一列) 那个好?

mysql> select * from test;

+------+

| id |

+------+

| 1 |

| 2 |

| NULL |

+------+

3 rows in set (0.00 sec)

mysql> select count(*),count(id) from test;

+----------+-----------+

| count(*) | count(id) |

+----------+-----------+

| 3 | 2 |

+----------+-----------+

1 row in set (0.00 sec)

小结:count(某一列)  不包括为null的值

2-8 子查询的优化

e5f915991715b1538c30229f342a9072.png

2-9 group by的优化

2-10 Limit查询的优化

第3章 索引优化

3-1 如何选择合适的列建立索引...(03:25)

3-2 索引优化SQL的方法(07:17)

3-3 索引维护的方法(02:43)

第4章 数据库结构优化

4-1 选择合适的数据类型...(06:43)

4-2 数据库表的范式化优化...(05:03)

4-3 数据库表的反范式化优化...(04:23)

4-4 数据库表的垂直拆分...(02:59)

4-5 数据库表的水平拆分...(03:13)

第5章 系统配置优化

5-1 数据库系统配置优化...(04:24)

5-2 MySQL配置文件优化(10:26)

5-3 第三方配置工具使用...(06:17)

第6章 服务器硬件优化

6-1 服务器硬件优化(05:59)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值