Home Database Mysql Tutorial MySQL SQL优化笔记的实际操作流程

MySQL SQL优化笔记的实际操作流程

Jun 07, 2016 pm 04:09 PM
mysql sql optimization article process

以下的文章主要讲述的是MySQL SQL优化的实际应用笔记的实际操作流程,以及关于Mysql查询使用 index 的问题解决, 如果你对其相关的实际操作有兴趣的话,你就可以对以下的文章点击观看了。 Mysql的强制索引(Force Index)都为我们做了哪些优化? mysql5下大

以下的文章主要讲述的是 MySQL SQL优化的实际应用笔记的实际操作流程,以及关于Mysql查询使用 index 的问题解决, 如果你对其相关的实际操作有兴趣的话,你就可以对以下的文章点击观看了。

Mysql的强制索引(Force Index)都为我们做了哪些优化?

mysql5下大数据量查询优化的问题

推荐圈子: Database圈子

更多相关推荐 一般使用SQL的时候你是不会去想到优化。但是面对一个有MySQL SQL性能问题的数据库时,我们应该如何入手进行系统的分析,使得能够尽快定位问题SQL,并且尽快解决问题。

1.使用show status 命令了解各种MySQL SQL的执行频率

引用

例如在Mysql的Cline上输入

show status like 'Com_%';

显示的是一些:Com_xxx.

Com_xxx 表示每个xx语句执行的次数。通常情况下我们比较关注如下一些操作:

引用

Com_select:执行select操作的次数

Com_insert:执行Insert操作的次数,对于批量插入的INSERT操作,只累加一次

Com_update:执行update操作的次数

Com_delete:执行Delete操作的次数

上面这些参数对于所有存储引擎的表操作都会进行累加。下面有些参数只针对InnoDB存储引擎的,累加的算法也有点不一样。

引用

Innodb_rows_read:select查询返回的行数

Innodb_rows_inserted:执行INSERT操作插入的行数

Innodb_rows_updated:执行Update操作更新的行数

Innodb_rows_deleted:执行Delete操作删除的行数

通过上面的一些参数,我们可以了解当前数据库的应用是以插入为主还是以查询为主。以及各种类型的MySQL SQL大致的执行比例是多少。对于更新操作的计数,是对执行次数的计数,不管提交还是回滚都会进行累加。

对于事务型的应用,通过Com_commit和Com_rollback进行分析。如果回滚操作非常频繁那么要思考下是不是编写存在问题。

下面有几个参数用于了解数据库的基本情况

引用

Connections:试图连接Mysql服务器的次数(执行的命令是:show status like 'Con_%';)

Uptime: 服务器工作时间(执行的命令是:show status like 'Up_%';)

Slow_queries:慢查询的次数(执行的命令是:show status like 'Slow_%';)

2. 定位执行效率较低的MySQL SQL语句

要想定义效率较低的SQL可以按照下面两种方式试试。

引用

1. 通过慢查询日志定位那些执行效率较低的SQL语句,用 --log-slow-queries[=file_name]选项启动时,mysqld写一个包含所有执行时间超过long_query_time秒的SQL语句的日志文件。

2. 慢查询日志在查询结束以后才记录,所以在应用反映执行效率出现问题的时候进行查询慢查询日志并不能定位问题,可以使show processlist 命令查看当前MySQL在进行的线程,包括线程的状态,是否锁表等,可以实时地查看SQL的执行情况,同时对一些锁表操作进优化。

3. 使用EXPLAIN分析低效SQL的执行计划。

在查询到效率低的MySQLSQL语句后,那我们可以使用explain或者DESC命令获取Myswl如何执行SELECT语句的信息,包括在Select语句执行过程中表如何连接和连接的顺序。

例如你想计数xxxx年公司的销售额,那么需要操作sales和comapny table,并对money字段进行sum操作。看看怎么使用explain:

引用

explain select sum(moneys) from sales a company b where a.company_id = b.id and a.year=XXXX \G;(注意加上\G是为了更好的看)

显示如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<ol class="dp-xml">

<li class="alt"><span><span>*********************** 1. row***************************   </span></span></li>

<li><span>id: 1   </span></li>

<li class="alt"><span>select_type: SIMPLE   </span></li>

<li><span>table: a   </span></li>

<li class="alt"><span>type: ALL   </span></li>

<li><span>possible_keys: NULL   </span></li>

<li class="alt"><span>key:NULL   </span></li>

<li><span>key_len: NULL   </span></li>

<li class="alt"><span>ref: NULL   </span></li>

<li><span>rows:1000   </span></li>

<li class="alt"><span>Extra: Using where   </span></li>

<li><span>*********************** 2. row***************************   </span></li>

<li class="alt"><span>id: 2   </span></li>

<li><span>select_type: SIMPLE   </span></li>

<li class="alt"><span>table: b   </span></li>

<li><span>type: ref   </span></li>

<li class="alt"><span>possible_keys: ind_company_id   </span></li>

<li><span>key:ind_comapany_id   </span></li>

<li class="alt"><span>key_len: 5   </span></li>

<li><span>ref: sakila.a.company_id   </span></li>

<li class="alt"><span>rows:1   </span></li>

<li><span>Extra: Using where;Using index  </span></li>

</ol>

Copy after login

下面解释下每个列的含义:

引用

select_type: 表示SELECT的类型,常见的取值为SIMPLE(简单表,不使用表连接或者子查询)、PRIMARY(主查询,即外层的查询)、UNION、SUBQUERY

table: 输出结果集的表

type: 表示表的连接类型,性能由好到差的类型类型为

(System(表中仅有一行,即常量表),

const(单表中最多有一个匹配行),

eq_ref(对于前面的每一行,在此表中只查询一条记录),

ref(使用普通的索引),

ref_or_null(和ref类似,但是条件中包含对于NULL查询),

index_merge(索引合并优化),

unique_subquery(in的后面是一个查询主键字段的子查询),

index_subquery(类似unique_subquery,主要是in的后面是查询非唯一索引字段的子查询),

range(单表中的范围查询),

index(对于当前的每一行,都通过查询索引来得到数据),

all(对于当前的每一行,都通过全表扫描来得到数据))

possible_keys: 表示查询时,可能使用的索引

key:表示实际使用的索引

key_len:索引字段的长度

rows:扫描行的数量

Extra:执行情况的说明和描述

以上的相关内容就是对MySQL SQL优化的笔记的介绍,望你能有所收获。 


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

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

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

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

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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.

See all articles