When it comes to database performance optimization, the most important thing is to choose the right one. You should decide whether your application requires a relational or non-relational database. Even within one genre, you'll have multiple options to choose from. As well as relational databases, you may find Oracle, MySQL, SQL Server, and PostgreSQL. On the other hand, non-relational databases introduced MongoDB, Cassandra, and CouchDB.
You might want me to suggest using a non-relational database for faster read/write performance. However, with some improvements and tweaks, you can push a relational database beyond its known limits. So, in this article, I will introduce you some tips to make your MySQL database faster.
If you are specifically wondering why you should use MySQL, the answer is simple because it is free, open source, and very popular in the PHP community, while Oracle is not widely used due to its high price. Other options are less popular than MySQL.
- MySQL Server Configuration: \
Well, first you should know the location of the configuration file, depending on your operating system. On Linux systems, it is located in "/etc/mysql/my.cnf". \
Now it's time to choose your engine InnoDB and MyISAM. To make the choice easier, you should know that InnoDB became the default engine in MySQL 5.5 because it supports "row-level locking, foreign keys and transactions", while MyISAM does not support any of the mentioned features, which makes it rarely useful in modern applications program. \
After selecting the correct engine, it's time to set some configuration variables in the my.cnf file.
max_connection variable: \
The max_connection variable represents the number of connections allowed by the application. The default value is 151 connections, however, if you get the error message "MySQL error, too many connections..." you can easily increase this number\
最大连接数 = 170
innodb_buffer_pool_size variable: \
To speed things up, MySQL will cache the data in your server's memory. This variable tells MySQL how many GigaBytes it can use. This variable is useful if you save large blobs in the database. You can set this to 80–90% of the server's memory. So if your server has 16GB of memory, you can set it to 14GB.
innodb_buffer_pool_size = 14GB
innodb_io_capacity variable: \
This variable tells MySQL how many input/output operations it can use, and it depends on your disk. For example, a single 7200 RPM drive is limited to 200 I/O, while an enterprise SSD disk is limited to 50,000 I/O. You can easily find the input/output values from the command line on your operating system and set the variables to 90% of the available I/O so MySQL never uses too many I/O operations.
innodb_io_capacity = 21000
query_cache_limit and query_cache_size variables:\
MySQL also supports caching data in memory, but we cannot rely on it as a caching system, because every time your program requests When data is written to a database table, MySQL will rebuild the entire table's query cache. So if your program has a high load, the MySQL cache will be completely useless. In this case, it is better to set both variables to 0 to save the overhead of MySQL cache. Instead, you can use something like Redis to manage the cache.
query_cache_limit = 0 query_cache_size = 0
Slow query log:\
The slow query log will show you which of your queries exceed the threshold you define, eliminating the need to guess which query is slower. \
First, you must enable slow_query_log
in your configuration file. In the Linux server, open "/etc/mysql/my.cnf" or the equivalent file on your system. \
And add:
slow_query_log = 1 long_query_time = 1
Then these two options will enable slow query logging and log any query that takes more than one second. If you prefer to view the logs in a table instead of a file, you can add:
log_output = 'TABLE'
Then you can find your logs in the "slow_log" table. There you can see information about all slow queries that performed for more than one second. This information includes the exact execution time and number of rows affected by the query, as well as which user executed it.
Query Optimization\
After you get all the slow queries, you need a way to optimize them to make them faster. Therefore, you can add the "explain" keyword in front of the query statement to obtain detailed information about the relevant query, for example: explain select * from users where active=1;
「Explanation ” keywords help you define which indexes your query hits and the number of rows you query to get the data. This information can tell you whether you need to create more indexes or restructure the database tables.
Denormalization and constraints: \
Denormalization is the process of improving read performance by adding redundant data or grouping them. For example, if you have a "Products" table and a "Category" table, and every time you query the "Products" table, you also need to get the "Category Name" of each product. In this case, you can use "join" to retrieve "category_name". However, this means that every time the user opens the product page, a complex join query is executed. Therefore, you may consider adding a "Category Name" to the "Products" table. Despite the redundant data, the increase in read performance is worth it.
The denormalization method may cause the "Category Name" in the "Product" table to be outdated. So you need to define a "foreign key" constraint, but you need to be aware that a "foreign key" will make write performance slightly slower because MySQL needs to check the constraint before writing the data. So it is always your task to make the best choice.
English original address: https://codeburst.io/database-performance-optimization-8d8407808b5b
Translation address: https://learnku.com/mysql/t/71571
[Related recommendations: mysql video tutorial]
The above is the detailed content of How to optimize performance of MySQL? Optimization Tips Sharing. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
