Home Database Mysql Tutorial MySQL数据库优化SQL语句的步骤4

MySQL数据库优化SQL语句的步骤4

Jun 07, 2016 pm 04:11 PM
mysql sql optimization I database article step statement

以上的文章我们讲过MySQL数据库优化SQL语句的前三步骤,今天我们就和大家一起来讲述MySQL数据库优化SQL语句的实际操作的第四步骤,以下就是文章的具体内容描述,望你在浏览之后会有所收获。 1:索引的使用,索引的重要性就不说了,功能也不说了,只说怎么做. 首

以上的文章我们讲过MySQL数据库优化SQL语句的前三步骤,今天我们就和大家一起来讲述MySQL数据库优化SQL语句的实际操作的第四步骤,以下就是文章的具体内容描述,望你在浏览之后会有所收获。

1:索引的使用,索引的重要性就不说了,功能也不说了,只说怎么做. 首先要明确所有的MySQL(和PHP搭配之最佳组合)索引(Prima(最完善的虚拟主机管理系统)ry,unique,index)在b树中有存储.索引主要用语:

a:快速找到where指定条件的记录 b:执行联结时,从其他表检索行 c:对特定的索引列找出max()和min()值

d:如果排序或者分组在一个可用键的最前面加前缀,排序或分组一个表

e:一个查询可能被用来MySQL数据库优化检索值,而不用访问数据文件.如果某些表的列是数字型并且正好是某个列的前缀,为了更快,值可以从索引树中取出

2:存储或者更新数据的查询速度  grant的执行会稍稍的减低效率.

MySQL(和PHP搭配之最佳组合)的函数应该被高度的优化.可以用benchmark(loop_count,expression)来找出是否查询有问题

select的查询速度:如果想要让一个select...where...更快,我能想到的只有建立索引.可以在一个表上运行myisamchk--analyze来更好的MySQL数据库优化查询.可以用myisamchk--sort-index--sort-records=1来设置用一个索引排序一个索引和数据.

3:MySQL(和PHP搭配之最佳组合)优化where子句

3.1:删除不必要的括号:

((a AND b) AND c OR (((a AND b) AND (a AND d))))>(a AND b AND c) OR (a AND b AND c AND d)

3.2:使用常数

(ab>5 AND b=c AND a=5

3.3:删除常数条件

(b>=5 AND b=5) OR (b=6 AND 5=5) OR (b=100 AND 2=3) >b=5 OR b=6

3.4:索引使用的常数表达式仅计算一次

3.5:在一个表中,没有一个where的count(*)直接从表中检索信息

3.6:所有常数的表在查询中在任何其他表之前读出

3.7:对外联结表最好联结组合是尝试了所有可能性找到的
 

3.8:如果有一个order by字句和一个不同的group by子句或者order by或者group by包含不是来自联结的第一个表的列,那么创建一个临时表

3.9:如果使用了sql_small_result,那么msyql使用在内存中的一个表

3.10:每个表的索引给查询并且使用跨越少于30%的行的索引.

3.11在每个记录输出前,跳过不匹配having子句的行

4:MySQL数据库优化left join

在MySQL(和PHP搭配之最佳组合)中 a left join b按以下方式实现

a:表b依赖于表a 

b:表a依赖于所有用在left join条件的表(除了b)

c:所有left join条件被移到where子句中

d:进行所有的联结MySQL数据库优化,除了一个表总是在所有他依赖的表后读取.如果有一个循环依赖,那么将发生错误

e:进行所有的标准的where优化 f:如果在a中有一行匹配where子句,但是在b中没有任何匹配left join条件,那么,在b中生成的所有设置为NULL的一行

g:如果使用left join来找出某些表中不存在的行并且在where部分有column_name IS NULL测试(column_name为NOT NULL列).那么,MySQL(和PHP搭配之最佳组合)在它已经找到了匹配left join条件的一行后,将停止在更多的行后寻找

5:MySQL数据库优化limit

a:如果用limit只选择一行,当MySQL(和PHP搭配之最佳组合)需要扫描整个表时,它的作用相当于索引

b:如果使用limit#与order by,MySQL(和PHP搭配之最佳组合)如果找到了第#行,将结束排序,而不会排序正个表

c:当结合limit#和distinct时,MySQL(和PHP搭配之最佳组合)如果找到了第#行,将停止

d:只要MySQL(和PHP搭配之最佳组合)已经发送了第一个#行到客户,MySQL(和PHP搭配之最佳组合)将放弃查询

e:limit 0一直会很快的返回一个空集合.

f:临时表的大小使用limit#计算需要多少空间来解决查询

6:MySQL数据库优化insert

插入一条记录的是由以下构成:

a:连接(3)

b:发送查询给服务器(2)

c:分析查询(2)

d:插入记录(1*记录大小)

e:插入索引(1*索引)

f:关闭(1)

以上数字可以看成和总时间成比例

改善插入速度的一些方法:

6.1:如果同时从一个连接插入许多行,使用多个值的insert,这比用多个语句要快

6.2:如果从不同连接插入很多行,使用insert delayed语句速度更快

6.3: 用myisam,如果在表中没有删除的行,能在select:s正在运行的同时插入行

6.4: 当从一个文本文件装载一个表时,用load data infile.这个通常比insert快20 倍
3.6:所有常数的表在查询中在任何其他表之前读出

3.7:对外联结表最好联结组合是尝试了所有可能性找到的
 

3.8:如果有一个order by字句和一个不同的group by子句或者order by或者group by包含不是来自联结的第一个表的列,那么创建一个临时表

3.9:如果使用了sql_small_result,那么msyql使用在内存中的一个表

3.10:每个表的索引给查询并且使用跨越少于30%的行的索引.

3.11在每个记录输出前,跳过不匹配having子句的行

4:MySQL数据库优化left join

在MySQL(和PHP搭配之最佳组合)中 a left join b按以下方式实现

a:表b依赖于表a 

b:表a依赖于所有用在left join条件的表(除了b)

c:所有left join条件被移到where子句中

d:进行所有的联结优化,除了一个表总是在所有他依赖的表后读取.如果有一个循环依赖,那么将发生错误

e:进行所有的标准的where优化 f:如果在a中有一行匹配where子句,但是在b中没有任何匹配left join条件,那么,在b中生成的所有设置为NULL的一行

g:如果使用left join来找出某些表中不存在的行并且在where部分有column_name IS NULL测试(column_name为NOT NULL列).那么,MySQL(和PHP搭配之最佳组合)在它已经找到了匹配left join条件的一行后,将停止在更多的行后寻找

5:MySQL数据库优化limit

a:如果用limit只选择一行,当MySQL(和PHP搭配之最佳组合)需要扫描整个表时,它的作用相当于索引

b:如果使用limit#与order by,MySQL(和PHP搭配之最佳组合)如果找到了第#行,将结束排序,而不会排序正个表

c:当结合limit#和distinct时,MySQL(和PHP搭配之最佳组合)如果找到了第#行,将停止

d:只要MySQL(和PHP搭配之最佳组合)已经发送了第一个#行到客户,MySQL(和PHP搭配之最佳组合)将放弃查询

e:limit 0一直会很快的返回一个空集合.

f:临时表的大小使用limit#计算需要多少空间来解决查询

6:MySQL数据库优化insert

插入一条记录的是由以下构成:

a:连接(3)

b:发送查询给服务器(2)

c:分析查询(2)

d:插入记录(1*记录大小)

e:插入索引(1*索引)

f:关闭(1)

以上数字可以看成和总时间成比例

改善插入速度的一些方法:

6.1:如果同时从一个连接插入许多行,使用多个值的insert,这比用多个语句要快

6.2:如果从不同连接插入很多行,使用insert delayed语句速度更快

6.3: 用myisam,如果在表中没有删除的行,能在select:s正在运行的同时插入行

6.4: 当从一个文本文件装载一个表时,用load data infile.这个通常比insert快20 倍

6.5:可以锁定表然后插入--主要的速度差别是在所有insert语句完成后,索引缓冲区仅被存入到硬盘一次.一般与有不同的insert语句那样多次存入要快.如果能用一个单个语句插入所有的行,锁定就不需要.锁定也降低连接的整体时间.但是对某些线程最大等待时间将上升.例如

<ol class="dp-xml">
<li class="alt"><span><span>thread 1 does 1000 inserts  </span></span></li>
<li><span>thread 2,3 and 4 does 1 insert  </span></li>
<li class="alt"><span>thread 5 does 1000 inserts  </span></li>
</ol>
Copy after login

如果不使用锁定,2,3,4将在1和5之前完成.如果使用锁定,2,3,4,将可能在1和5之后完成.但是整体时间应该快40%.因为insert,update,delete操作在MySQL(和PHP搭配之最佳组合)中是很快的,通过为多于大约5次连续不断的插入或更新一行的东西加锁,将获得更好的整体性能.

如果做很多一行的插入,可以做一个lock tables,偶尔随后做一个unlock tables(大约每1000行)以允许另外的线程存取表.这仍然将导致获得好的性能.load data infile对装载数据仍然是很快的.

为了对load data infile和insert得到一些更快的速度,扩大关键字缓冲区.

7优化update的速度

它的速度依赖于被更新数据的大小和被更新索引的数量

使update更快的另一个方法是推迟修改,然后一行一行的做很多修改.如果锁定表,做一行一行的很多修改比一次做一个快

8MySQL数据库优化delete速度

删除一个记录的时间与索引数量成正比.为了更快的删除记录,可以增加索引缓存的大小 从一个表删除所有行比删除这个表的大部分要快的多


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'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.

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.

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

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

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.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

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.

See all articles