search
HomeDatabaseMysql TutorialIntroduction to mysql optimization ideas
Introduction to mysql optimization ideasMar 22, 2019 am 11:13 AM
linuxmysqlphp

The content of this article is an introduction to mysql optimization ideas. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Database level problem solving ideas

General emergency tuning ideas:
In case of sudden business processing lag, normal business processing cannot be carried out! A scenario that needs to be resolved immediately!

1、show processlist

2、explain select id ,name from stu where name='clsn'; # ALL id name age sex
select id,name from stu where id=2-1 函数 结果集>30;
show index from table;

3、通过执行计划判断,索引问题(有没有、合不合理)或者语句本身问题

4、show status like '%lock%'; # 查询锁状态
kill SESSION_ID; # 杀掉有问题的session

General tuning ideas:
For periodic business lags, for example, the business is extremely slow at 10-11 o'clock every day, but it can still be used, and it will be fine after this period.

1. Check the slowlog, analyze the slowlog, and analyze the slow query statements.

2. Check all slow statements one by one according to a certain priority.

3. Analyze top sql, perform explain debugging, and check the statement execution time.

4. Adjust the index or the statement itself.

  1. System level

cpu aspect:
vmstat, sar top, htop, nmon, mpstat

Memory:
free, ps -aux,

IO devices (disk, network):
iostat, ss, netstat, iptraf, iftop, lsof,

vmstat Command description:
Procs: r displays How many processes are waiting for CPU time. b Displays the number of processes in uninterruptible sleep. Waiting for I/O

Memory: swpd displays the number of data blocks swapped to disk. The number of unused data blocks, user buffer data blocks, and data blocks used by the operating system

Swap: The number of data blocks that the operating system swaps from disk to memory and from memory to disk per second . s1 and s0 are preferably 0

Io: The number of data blocks written to device b0 that are read from device b1 per second. Reflects disk I/O

System: Shows the number of interrupts (in) and context switches (cs) that occur per second

Cpu: Shows the number used to run user code, system code , idle, CPU time waiting for I/O

iostat command description
Example command: iostat -dk 1 5
iostat -d -k -x 5 (View device usage (%util) and response time (await))

tps: The number of transmissions per second of the device. "A transfer" means "an I/O request." Multiple logical requests may be combined into a "single I/O request".

iops: When the hardware leaves the factory, the manufacturer defines a maximum number of IOs per second. The size of the "one transfer" request is unknown.

kB_read/s: The amount of data read from the device (drive expressed) per second;

KB_wrtn/s: The amount of data written to the device (drive expressed) per second;

kB_read: The total amount of data read;

kB_wrtn: The total amount of data written; these units are Kilobytes.

  1. Solutions to system-level problems

Do you think it is better to have a high load or a low load?
In actual production, it is generally believed that there is no problem as long as the CPU does not exceed 90%.

Of course, the following special situations are not excluded:
Problem 1: High CPU load, low IO load
Insufficient memory

Poor disk performance

SQL problem- ----->Go to the database layer to further troubleshoot the sql problem

There is a problem with IO (the disk is critical, the raid design is not good, the raid is degraded, locked, and the tps per unit time is too high)

tps is too high: a large number of small data IO, a large number of full table scans

Problem 2: high IO load, low cpu load
A large number of small IO write operations:

autocommit, which generates a large number of small IOs

IO/PS is a fixed value of the disk. When the hardware leaves the factory, the manufacturer defines a maximum number of IOs per second.

A large number of large IO write operations

The probability of SQL problems is relatively high

Problem 3: IO and CPU loads are very high
The hardware is not enough or there is a problem with SQL

5. Basic optimization

  1. Optimization ideas

Positioning problem points:
Hardware--> System--> Application-- > Database --> Architecture (high availability, read-write separation, sub-database and sub-table)

Processing direction:
Clear optimization goals, compromise between performance and security, and prevent problems before they occur

  1. Hardware optimization

Host aspect:
According to database type, host CPU selection, memory capacity selection, disk selection

Balance memory and disk resources

Random I/O and sequential I/O

BBU (Battery Backup Unit) of the host RAID card is turned off

Selection of cpu:
Two key factors of cpu: Number of cores and main frequency

Choose according to different business types:

Cpu-intensive: more calculations, OLTP CPUs with high main frequency and more cores

IO-intensive: For query comparison, the number of OLAP cores is larger, and the main frequency is not necessarily high.

Memory selection:
OLAP type database requires more memory, which is related to the level of data acquisition.

OLTP type data generally requires 2 to 4 times the number of CPU cores in memory, and there is no best practice.

Storage:
Choose different storage devices according to different types of stored data

Configure a reasonable RAID level (raid 5, raid 10, hot spare disk)

For the operating system, there is no need to make too special choices. It is best to make redundant (raid1) (ssd, sas, sata)

raid card: host raid card selection:
implementation Operating system disk redundancy (raid1)

Balance memory and disk resources

Random I/O and sequential I/O

BBU (Battery) of the host RAID card Backup Unit) to close

Network equipment:
Use network equipment that supports higher traffic (switches, routers, network cables, network cards, HBA cards)

Note: The above planning should be considered when initially designing the system good.

  1. Server hardware optimization

1. Physical status light:

2. Comes with management device: remote control card (FENCE device: ipmi ilo idarc ), power on/off, hardware monitoring.

3. Third-party monitoring software and equipment (snmp, agent) monitor physical facilities

4. Storage equipment: built-in monitoring platform. EMC2 (acquired by HP), Hitachi (hds), IBM low-end OEM hds, high-end storage is its own technology, Huawei storage

  1. System optimization

Cpu:
Basically no adjustment is required, just focus on hardware selection.

Memory:
Basically no adjustment is needed, just focus on hardware selection.

SWAP:
MySQL try to avoid using swap. The default swap in Alibaba Cloud's server is 0

IO:
raid, no lvm, ext4 or xfs, ssd, IO scheduling strategy

Swap adjustment (do not use swap partition)

This parameter determines whether Linux prefers to use swap or release the file system cache. In situations where memory is tight, lower values ​​tend to free up the file system cache. Of course, this parameter can only reduce the probability of using swap, but cannot prevent Linux from using swap.

Modify the MySQL configuration parameter innodb_flush_method and enable O_DIRECT mode. In this case, InnoDB's buffer pool will directly bypass the file system cache to access the disk, but the redo log will still use the file system cache. It is worth noting that Redo log is in overwrite mode. Even if the file system cache is used, it will not take up too much

IO scheduling strategy:

  1. System parameter adjustment

Linux system kernel parameter optimization:

User restriction parameters (MySQL does not need to set the following configuration):

  1. Application optimization

Business applications and database applications are independent, firewall: iptables, selinux and other useless services (closed):

Do not start the graphical interface runlevel 3 when installing a server with a graphical interface. In addition, think about whether our business will be real in the future. Do you need MySQL or use another kind of database? The highest state of using a database is not to use a database.

6. Database optimization
SQL optimization direction:
Execution plan, index, SQL rewriting

Architecture optimization direction:
High availability architecture, high performance architecture, sub-database Sub-table

  1. Database parameter optimization

Adjustment:
Instance overall (advanced optimization, expansion)

Connection layer (basic optimization)
Set up reasonable connection clients and connection methods

This article has ended here. For more other exciting content, you can pay attention to the MySQL Tutorial Video column of the PHP Chinese website!

The above is the detailed content of Introduction to mysql optimization ideas. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

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

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

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

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

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

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

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

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

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

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

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

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

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

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

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

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!