Recommended tutorial: MySQL tutorial
## 1. Choose the appropriate storage engine: InnoDB
Unless your data table is used for reading only or full-text retrieval (I believe no one will use MYSQL when it comes to full-text retrieval nowadays) ). You should select InnoDB by default.
When you test it yourself, you may find that MyISAM is faster than InnoDB. This is because: MyISAM only caches indexes, while InnoDB caches data and indexes, and MyISAM does not support transactions. However, assuming you use innodb_flush_log_at_trx_commit = 2 you can get close read performance (a hundred times difference).
1.1 How to convert an existing MyISAM database to InnoDB:
mysql -u [USER_NAME] -p -e "SHOW TABLES IN [DATABASE_NAME];" | tail -n +2 | xargs -I '{}' echo "ALTER TABLE {} ENGINE=InnoDB;" > alter_table.sql perl -p -i -e 's/(search_[a-z_]+ ENGINE=)InnoDB//1MyISAM/g' alter_table.sql mysql -u [USER_NAME] -p [DATABASE_NAME] < alter_table.sql
1.2 For each Create InnoDB FILE for each table:
innodb_file_per_table=1
This ensures that the ibdata1 file will not be too large. out of control. Especially when running mysqlcheck -o –all-databases.
#2. Ensure data is read from memory. Talking about data being stored in memory
##2.1 A large enough innodb_buffer_pool_size
It is recommended to store all data in innodb_buffer_pool_size, that is, planning the capacity of innodb_buffer_pool_size according to the storage amount. This way you can read the data entirely from memory. Minimize disk operations.
2.1.1 How to determine innodb_buffer_pool_size is large enough. Is the data being read from memory instead of hard drive?
Method 1mysql> SHOW GLOBAL STATUS LIKE 'innodb_buffer_pool_pages_%';
+----------------------------------+--------+
| Variable_name | Value |
+----------------------------------+--------+
| Innodb_buffer_pool_pages_data | 129037 |
| Innodb_buffer_pool_pages_dirty | 362 |
| Innodb_buffer_pool_pages_flushed | 9998 |
| Innodb_buffer_pool_pages_free | 0 | !!!!!!!!
| Innodb_buffer_pool_pages_misc | 2035 |
| Innodb_buffer_pool_pages_total | 131072 |
+----------------------------------+--------+
6 rows in set (0.00 sec)
Several other parameters of InnoDB:
innodb_additional_mem_pool_size = 1/200 of buffer_pool
innodb_max_dirty_pages_pct 80%
Or use the iostat -d -x -k 1 command to check the operation of the hard disk.
2.1.2 Whether there is enough memory on the server for planning
Run echo 1 > /proc/ sys/vm/drop_caches Clears the operating system's file cache. Ability to see true memory usage.
2.2 Data Warming
By default, a piece of data will only be cached if it is read once in innodb_buffer_pool. Therefore, the database has just started and needs to warm up the data and cache all the data on the disk into the memory.Data preheating can increase reading speed.
For InnoDB database, you can use the following method to warm up the data:
1. Save the following script as MakeSelectQueriesToLoad.sql
SELECT DISTINCT
CONCAT('SELECT ',ndxcollist,' FROM ',db,'.',tb,
' ORDER BY ',ndxcollist,';') SelectQueryToLoadCache
FROM
(
SELECT
engine,table_schema db,table_name tb,
index_name,GROUP_CONCAT(column_name ORDER BY seq_in_index) ndxcollist
FROM
(
SELECT
B.engine,A.table_schema,A.table_name,
A.index_name,A.column_name,A.seq_in_index
FROM
information_schema.statistics A INNER JOIN
(
SELECT engine,table_schema,table_name
FROM information_schema.tables WHERE
engine='InnoDB'
) B USING (table_schema,table_name)
WHERE B.table_schema NOT IN ('information_schema','mysql')
ORDER BY table_schema,table_name,index_name,seq_in_index
) A
GROUP BY table_schema,table_name,index_name
) AA
ORDER BY db,tb
;
mysql -uroot -AN < /root/MakeSelectQueriesToLoad.sql > /root/SelectQueriesToLoad.sql
3. Run every time you restart the database, or when you need to warm up before backing up the entire database:
mysql -uroot < /root/SelectQueriesToLoad.sql > /dev/null 2>&1
2.3 Do not allow data to be saved in SWAP
Assuming it is a dedicated MYSQL server. SWAP can be disabled, assuming it is a shared server, and make sure innodb_buffer_pool_size is large enough. Or use a fixed memory space for caching and use the memlock instruction.
3. Regularly optimize and rebuild the database
mysqlcheck - o –all-databases will make ibdata1 continue to grow. The real optimization only involves rebuilding the data table structure:CREATE TABLE mydb.mytablenew LIKE mydb.mytable;
INSERT INTO mydb.mytablenew SELECT * FROM mydb.mytable;
ALTER TABLE mydb.mytable RENAME mydb.mytablezap;
ALTER TABLE mydb.mytablenew RENAME mydb.mytable;
DROP TABLE mydb.mytablezap;
4. Reduce disk write operations
4.1 Use a large enough write cache innodb_log_file_size
But you need to pay attention to the assumption that 1G innodb_log_file_size is used. If the server crashes. It takes 10 minutes to recover.It is recommended to set innodb_log_file_size to 0.25 * innodb_buffer_pool_size
##4.2 innodb_flush_log_at_trx_commit
这个选项和写磁盘操作密切相关: innodb_flush_log_at_trx_commit = 1 则每次改动写入磁盘 innodb_flush_log_at_trx_commit = 0/2 每秒写入磁盘 假设你的应用不涉及非常高的安全性 (金融系统),或者基础架构足够安全,或者 事务都非常小,都能够用 0 或者 2 来减少磁盘操作。 4.3 避免双写入缓冲
5. 提高磁盘读写速度 RAID0 尤其是在使用 EC2 这样的虚拟磁盘 (EBS) 的时候,使用软 RAID0 很重要。 6. 充分使用索引 6.1 查看现有表结构和索引 6.2 加入必要的索引 索引是提高查询速度的唯一方法。比方搜索引擎用的倒排索引是一样的原理。 索引的加入须要依据查询来确定。比方通过慢查询日志或者查询日志,或者通过 EXPLAIN 命令分析查询。 6.2.1 比方,优化用户验证表: 加入索引 每次重新启动server进行数据预热 加入启动脚本到 my.cnf
6.2.2 使用自己主动加索引的框架或者自己主动拆分表结构的框架 比方。Rails 这种框架。会自己主动加入索引。Drupal 这种框架会自己主动拆分表结构。 会在你开发的初期指明正确的方向。所以,经验不太丰富的人一開始就追求从 0 開始构建,实际是不好的做法。 7. 分析查询日志和慢查询日志 记录全部查询。这在用 ORM 系统或者生成查询语句的系统非常实用。 注意不要在生产环境用。否则会占满你的磁盘空间。 记录运行时间超过 1 秒的查询: 8. 激进的方法。使用内存磁盘 如今基础设施的可靠性已经非常高了,比方 EC2 差点儿不用操心server硬件当机。并且内存实在是廉价。非常easy买到几十G内存的server,能够用内存磁盘。定期备份到磁盘。 将 MYSQL 文件夹迁移到 4G 的内存磁盘 9. 用 NOSQL 的方式使用 MYSQL B-TREE 仍然是最高效的索引之中的一个,全部 MYSQL 仍然不会过时。 用 HandlerSocket 跳过 MYSQL 的 SQL 解析层。MYSQL 就真正变成了 NOSQL。 10. 其它 ●单条查询最后添加 LIMIT 1,停止全表扫描。 ●将非”索引”数据分离,比方将大篇文章分离存储,不影响其它自己主动查询。 ●不用 MYSQL 内置的函数。由于内置函数不会建立查询缓存。 ●PHP 的建立连接速度很快,全部能够不用连接池。否则可能会造成超过连接数。当然不用连接池 PHP 程序也可能将 ●连接数占满比方用了 @ignore_user_abort(TRUE); ●使用 IP 而不是域名做数据库路径。避免 DNS 解析问题innodb_flush_method=O_DIRECT
SHOW CREATE TABLE db1.tb1/G
ADD UNIQUE INDEX
ADD INDEX
ALTER TABLE users ADD UNIQUE INDEX username_ndx (username);
ALTER TABLE users ADD UNIQUE INDEX username_password_ndx (username,password);
echo “select username,password from users;” > /var/lib/mysql/upcache.sql
[mysqld]
init-file=/var/lib/mysql/upcache.sql
log=/var/log/mysql.log
long_query_time=1
log-slow-queries=/var/log/mysql/log-slow-queries.log
mkdir -p /mnt/ramdisk
sudo mount -t tmpfs -o size=4000M tmpfs /mnt/ramdisk/
mv /var/lib/mysql /mnt/ramdisk/mysql
ln -s /tmp/ramdisk/mysql /var/lib/mysql
chown mysql:mysql mysql
The above is the detailed content of Several methods of mysql optimization. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software