Notes on MySQL optimization
mysql video tutorial column introduces the optimization method of MySQL
Recommended (free): mysql video tutorial
Record study notes and continuously update.
Optimization direction
SQL optimization
- sql optimization analysis
- Index optimization
Optimize database objects
- Optimize the data type of the table
- Table split (horizontal, vertical)
- Anti-paradigm
- Use intermediate table
Optimize mysql server
- mysql memory management optimization
- log mechanism And optimization
- Adjust mysql concurrency parameters
Application optimization
- Database connection pool
- Use cache to reduce Pressure
- Load balancing to establish a cluster
- Master-master synchronization, master-slave replication
##Mysql optimization problem analysis and positioning
Analyze SQL execution frequencyshow status
例如:分析读为主,还是写为主
Copy after login
show status 例如:分析读为主,还是写为主
Locate SQl with low execution efficiency慢查询日志定位-log-slow-queries = xxx(指定文件名)SHOW PROCESSLIST查看当前正在进行的线程,包括线程状态、是否锁表
Copy after login
慢查询日志定位-log-slow-queries = xxx(指定文件名)SHOW PROCESSLIST查看当前正在进行的线程,包括线程状态、是否锁表
Analyze SQL execution planexplain "your sql"desc "your sql"- 部分参数分析
select_type: SIMPLE 简单表,不使用表连接或子查询PRIMARY 主查询,即外层的查询UNION SUBQUER 子查询的第一个select
type: ALL 全表扫描
index 索引全扫描
range 索引范围扫描
ref 使用非唯一索引或唯一索引的前缀扫描
eq_ref 类似ref,使用的索引是唯一索引const/system 单表中最多有一个匹配行NULL 不用访问表或者索引,直接得到结果
Copy after login
explain "your sql"desc "your sql"- 部分参数分析 select_type: SIMPLE 简单表,不使用表连接或子查询PRIMARY 主查询,即外层的查询UNION SUBQUER 子查询的第一个select type: ALL 全表扫描 index 索引全扫描 range 索引范围扫描 ref 使用非唯一索引或唯一索引的前缀扫描 eq_ref 类似ref,使用的索引是唯一索引const/system 单表中最多有一个匹配行NULL 不用访问表或者索引,直接得到结果
show profile analysis SQLselect @@have_profiling 是否支持
select @@profiling 是否开启
执行 "your sql"show profiles
show profile block io for QUERY 17
Copy after login
select @@have_profiling 是否支持 select @@profiling 是否开启 执行 "your sql"show profiles show profile block io for QUERY 17
Index optimization
Index storage classificationB-TREE索引:常见,大部分都支持HASH索引:只有memory引擎支持R-TREE索引:空间索引是MyISAM的一个特殊索引类型,主要用于地理空间数据类型
full-text索引:全文索引,MyISAM的一个特殊索引类型,innodb从5.6开始支持
Copy after login
B-TREE索引:常见,大部分都支持HASH索引:只有memory引擎支持R-TREE索引:空间索引是MyISAM的一个特殊索引类型,主要用于地理空间数据类型 full-text索引:全文索引,MyISAM的一个特殊索引类型,innodb从5.6开始支持
Creation and deletion of indexes添加索引ALTER Table `table_name` ADD PRIMARY KEY(`column`)ALTER Table `table_name` ADD UNIQUE(`column`)ALTER Table `table_name` ADD INDEX(`column`)ALTER Table `table_name` ADD FULLTEXT(`column`)
删除ALTER Table `table_name` drop index index_name
Copy after login
添加索引ALTER Table `table_name` ADD PRIMARY KEY(`column`)ALTER Table `table_name` ADD UNIQUE(`column`)ALTER Table `table_name` ADD INDEX(`column`)ALTER Table `table_name` ADD FULLTEXT(`column`) 删除ALTER Table `table_name` drop index index_name
Situations where indexes can be used in Mysql匹配全值
匹配值范围查询
匹配最左前缀
仅仅对索引进行查询(覆盖查询)
匹配列前缀 (添加前缀索引)
部分精确+部分范围
Copy after login
匹配全值 匹配值范围查询 匹配最左前缀 仅仅对索引进行查询(覆盖查询) 匹配列前缀 (添加前缀索引) 部分精确+部分范围
Scenarios where indexes cannot be used 以%开关的like查询
数据类型出现隐式转换
复合索引查询条件不包含最左部分
使用索引仍比全表扫描慢
用or分割开的条件
Copy after login
以%开关的like查询 数据类型出现隐式转换 复合索引查询条件不包含最左部分 使用索引仍比全表扫描慢 用or分割开的条件
mysql statement optimization
Optimize the table regularlyoptimize table table_name 合并表空间碎片,对MyISAM、BDB、INNODB有效
如果提示不支持,可以用 mysql --skip-new 或者 mysql --safe-mode 来重启,以便让其他引擎支持
Copy after login
optimize table table_name 合并表空间碎片,对MyISAM、BDB、INNODB有效 如果提示不支持,可以用 mysql --skip-new 或者 mysql --safe-mode 来重启,以便让其他引擎支持
Commonly used optimization尽量避免全表扫描,对where及orderby的列建立索引
尽量避免where使用 != 或 <>尽量避免where子句用 or 连接条件
乱用%导致全表扫描
尽量避免where子句对字段进行表达式操作
尽量避免where子句对字段进行函数操作
覆盖查询,返回需要的字段
优化嵌套查询,关联查询优于子查询
组合索引或复合索引,最左索引原则
用exist代替in当索引列有大量重复数据时,SQL查询可能不会去利用索引
Copy after login
尽量避免全表扫描,对where及orderby的列建立索引 尽量避免where使用 != 或 <>尽量避免where子句用 or 连接条件 乱用%导致全表扫描 尽量避免where子句对字段进行表达式操作 尽量避免where子句对字段进行函数操作 覆盖查询,返回需要的字段 优化嵌套查询,关联查询优于子查询 组合索引或复合索引,最左索引原则 用exist代替in当索引列有大量重复数据时,SQL查询可能不会去利用索引
Optimize database objects
Optimize table data typesPROCEDURE ANALYSE (16,256) 排除多于16个,大于256字节的ENUM建议"your sql" PROCEDURE ANALYSE ()
Copy after login
PROCEDURE ANALYSE (16,256) 排除多于16个,大于256字节的ENUM建议"your sql" PROCEDURE ANALYSE ()
Table split垂直拆分
针对某些列常用、不常用
水平拆分
表很大
表中的数据有独立性,能简单分类
需要在表存放多种介质
Copy after login
垂直拆分 针对某些列常用、不常用 水平拆分 表很大 表中的数据有独立性,能简单分类 需要在表存放多种介质
Anti-Normal增加冗余列、增加派生列、重新组表和分割表
Copy after login
增加冗余列、增加派生列、重新组表和分割表
Use intermediate table数据查询量大
数据统计、分析场景
Copy after login
数据查询量大 数据统计、分析场景
Mysql engine comparison
What engine does mysql have?
Commands about table engineshow engines; 查看myql所支持的存储引擎
show variables like '%storage_engine'; 查看mysql默认的存储引擎
show create table table_name 查看具体表使用的存储引擎
Copy after login
show engines; 查看myql所支持的存储引擎 show variables like '%storage_engine'; 查看mysql默认的存储引擎 show create table table_name 查看具体表使用的存储引擎
About innodb1. 提供事务、回滚、系统奔溃修复能力、多版本并发控制事务2. 支持自增列3. 支持外键4. 支持事务以及事务相关联功能5. 支持mvcc的行级锁
Copy after login
1. 提供事务、回滚、系统奔溃修复能力、多版本并发控制事务2. 支持自增列3. 支持外键4. 支持事务以及事务相关联功能5. 支持mvcc的行级锁
About MyISAM1. 不支持事务、不支持行级锁,只支持并发插入的表锁,主要用于高负载的select2. 支持三种不同的存储结构:静态、动态、压缩
Copy after login
1. 不支持事务、不支持行级锁,只支持并发插入的表锁,主要用于高负载的select2. 支持三种不同的存储结构:静态、动态、压缩
Adjust parameters to optimize mysql background service
MyISAM memory optimization#修改相应服务器位置的配置文件 my.cnf
key_buffer_size
决定myisam索引块缓存区的大小,直接影响表的存取效率,建议1/4可用内存
read_buffer 读缓存
write_buffer 写缓存
Copy after login
#修改相应服务器位置的配置文件 my.cnf key_buffer_size 决定myisam索引块缓存区的大小,直接影响表的存取效率,建议1/4可用内存 read_buffer 读缓存 write_buffer 写缓存
InnoDB memory optimizationinnodb_buffer_pool_size 存储引擎表数据和索引数据的最大缓存区大小
innodb_old_blocks_pct LRU算法 决定old sublist的比例
innodb_old_blocks_time LRU算法 数据转移间隔时间
Copy after login
innodb_buffer_pool_size 存储引擎表数据和索引数据的最大缓存区大小 innodb_old_blocks_pct LRU算法 决定old sublist的比例 innodb_old_blocks_time LRU算法 数据转移间隔时间
mysql concurrency parametersmax_connections 最大连接数,默认151back_log 短时间内处理大量连接,可适当增大
table_open_cache 控制所有SQL执行线程可打开表缓存的数量,受其他参数制约
thread_cache_size 控制缓存客户服务线程数量,加快数据库连接速度,根据threads_created/connections来衡量是否合适
innodb_lock_wait_timeout 控制事务等待行锁时间,默认50ms
Copy after login
max_connections 最大连接数,默认151back_log 短时间内处理大量连接,可适当增大 table_open_cache 控制所有SQL执行线程可打开表缓存的数量,受其他参数制约 thread_cache_size 控制缓存客户服务线程数量,加快数据库连接速度,根据threads_created/connections来衡量是否合适 innodb_lock_wait_timeout 控制事务等待行锁时间,默认50ms
Mysql application optimization introduction
Why do we need to apply application optimization
- The importance of data
- mysql service and its own performance bottleneck
- Ensure stable and reliable operation of large systems
Application optimization method
- Use connection pool
- Reduce the real connection to mysql
a. Avoid Repeated execution of the same data (query cache)
b. Use mysql cache (sql cache) - Load balancing
a. LVS distributed
b. Read and write separation (Master-master replication and master-slave replication ensure data consistency)
Database connection pool
php-cp extension, just record this This solution may be outdatedMaster-slave backup and read-write separation
Master-master backup
Load balancing
Related free learning recommendations: php programming (video)
The above is the detailed content of Notes on MySQL optimization. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
