MySQL优化之 定位性能问题
要优化MySQL, 先要找出问题的所在, 是那一块东西拖累的数据库的速度。 大多数的MySQL性能问题是因为数据算法设计不合理,导致某些SQL语句占用了非常多的资源。 我们按照这个步骤来进行: 1. 先分析那些SQL是执行次数最多的。 我们会用到?tcpdump?(一个抓包
要优化MySQL, 先要找出问题的所在, 是那一块东西拖累的数据库的速度。
大多数的MySQL性能问题是因为数据算法设计不合理,导致某些SQL语句占用了非常多的资源。 我们按照这个步骤来进行:
1. 先分析那些SQL是执行次数最多的。
我们会用到?tcpdump?(一个抓包工具)?, 来抓取到3306端口的数据包, 进而分析MySQL都在做了些什么
首先我们来安装tcpdump
wget http://maatkit.googlecode.com/files/maatkit-6652.tar.gz
tar zxvf maatkit-6652.tar.gz -C /usr/local/
cd /usr/local/maatkit-6652
perl Makefile.PL make install
然后用下边的命令分析MySQL正在做什么, 并写到临时文件 mysql.tmp里
time tcpdump -i eth0 -s 1500 src host 192.168.2.10 -w mysql.tmp
接下来分析 那些SQL执行的次数最多
strings 20060427-db-traffic-01.dmp | grep -i 'select' | awk '{printf("%s %s %s %s\n", $1,$2,$3, $4);}'| sort| uniq -c | awk '{printf("%06ld %s %s %s %s\n", $1,$2,$3,$4,$5);}'|sort
得到结果:
cpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 1500 bytes
12000 packets captured
12000 packets received by filter
0 packets dropped by kernel
real 0m8.666s
user 0m0.006s
sys 0m0.016s
--------
000001 select last_insert_id() from system_parameter
...
000122 select count(1) from visit_tracking
000122 select visitor_id
000800 select web_page_id , web_page_type_id
000800 select web_page_type_id , name
003200 select count(1) from hit_count
006400 select pd.parameter_value,
006400 select rp.user_id , rp.update_time
可以看到执行各种select执行的次数
2. 分析比较慢的查询
//TODO
3. 找到为什么 SQL语句执行比较慢
//TODO
声明: 本文采用 CC BY-NC-SA 3.0 协议进行授权
转载请注明来源:小景的博客
本文链接地址:http://www.phpv5.com/blog/archives/47/

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.

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

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.

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.

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.
