MySQL优化之延迟索引和分页优化
什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!
什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!
分页是一个很常见功能,select ** from tableName limit ($page - 1 ) * $n ,$n
通过一个存储过程插入10000条数据进行测试:
create table smth1 (
id int auto_increment ,
ver int(11) default null,
content varchar(1000) not null,
intro varchar(1000) not null,
primary key(id),
key idver(id,ver)
)engine = innodb default charset = utf8;
create procedure smthTest1()
begin
declare num int default 100001;
while num set num := num +1;
insert into smth1 values (num ,num,'我是*****','我是谁');
end while ;
end;
查询:
mysql> show profiles;
+----------+------------+----------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------------------------+
| 1 | 0.002006 | select id ,content from smth1 limit 1000,10 |
| 2 | 0.030106 | select id ,content from smth1 limit 5000,10 |
| 3 | 0.042428 | select id ,content from smth1 limit 9000,10 |
| 4 | 0.01297225 | select id ,content from smth1 limit 10000,10 |
| 5 | 0.13077625 | select id ,content from smth1 limit 20000,10 |
可见随着查询$page 变大,时间会越来越大!
怎样避免这种情况?
一般我们数据库里面数据都不会直接删除,数据时很宝贵的,不舍得删除,另一方便能提高查询数据
先利用索引查询出来数据,再进行联合查询不就行了
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 1000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 5000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 9000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 10000 limit 10
) as t on C.id = t.id ;
select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 20000 limit 10
) as t on C.id = t.id ;
进行执行计划分析,没有一个大于1s的
11 | 0.04538625 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 5000 limit 10
) as t on C.id = t.id |
| 12 | 0.023278 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 9000 limit 10
) as t on C.id = t.id |
| 13 | 0.02320425 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 10000 limit 10
) as t on C.id = t.id |
| 14 | 0.001938 | select C.id,C.content from smth1 C inner join
(
select id from smth1 where id > 20000 limit 10
) as t on C.id = t.id |
此外,还会想到用in来查询而不是子查询,为什么不用in,,使用in会先查询出来一条id,之后再去和下面进行匹配,会进行smth1进行全表扫描!
--------------------------------------分割线 --------------------------------------
Ubuntu 14.04下安装MySQL
《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF
Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL
Ubuntu 14.04下搭建MySQL主从服务器
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb
MySQL-5.5.38通用二进制安装
--------------------------------------分割线 --------------------------------------
本文永久更新链接地址:

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

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



How to improve performance by optimizing the AVG function in MySQL MySQL is a popular relational database management system that contains many powerful functions and functions. The AVG function is widely used in calculating averages, but because this function needs to traverse the entire data set, it will cause performance problems in the case of large-scale data. This article will introduce in detail how to optimize the AVG function through MySQL to improve performance. 1. Using indexes Indexes are the most important part of MySQL optimization.

MySQL is a relational database management system widely used in the field of e-commerce. In e-commerce applications, it is crucial to optimize and secure MySQL. This article will analyze MySQL’s optimization and security project experience in e-commerce applications. 1. Performance optimization database architecture design: In e-commerce applications, database design is the key. Reasonable table structure design and index design can improve the query performance of the database. At the same time, using table splitting and partitioning technology can reduce the amount of data in a single table and improve query efficiency.

MySQL optimization based on TokuDB engine: improving writing and compression performance Introduction: As a commonly used relational database management system, MySQL is facing increasing writing pressure and storage requirements in the context of the big data era. To meet this challenge, TokuDB engine came into being. This article will introduce how to use the TokuDB engine to improve MySQL's writing performance and compression performance. 1. What is TokuDB engine? TokuDB engine is a big data-oriented engine designed to handle high write

MySQL is a widely used relational database management system commonly used for web application development and data storage. In practical applications, the underlying optimization of MySQL is particularly important, among which the advanced optimization of SQL statements is the key to improving database performance. This article will introduce some tips and best practices for implementing MySQL's underlying optimization, as well as specific code examples. Determine the query conditions When writing SQL statements, you must first clearly define the query conditions and avoid using unlimited wildcard queries, that is, avoid using "%" to open the query.

How to optimize MySQL connection number management MySQL is a popular relational database management system that is widely used in various websites and applications. In the actual application process, MySQL connection number management is a very important issue, especially in high concurrency situations. Reasonable management of the number of connections can improve the performance and stability of the system. This article will introduce how to optimize MySQL connection number management, including detailed code examples. 1. Understand connection number management In MySQL, the number of connections refers to the number of connections that the system can connect at the same time.

How to properly configure and optimize MySQL's double-write buffering technology Introduction: MySQL's double-write buffering technology is an important technology that improves data security and performance. This article will introduce how to properly configure and optimize MySQL's double-write buffering technology to better protect data and improve database performance. 1. What is double-write buffering technology? Double-write buffering technology is an I/O optimization technology of MySQL. It can significantly reduce the number of disk I/O operations and improve the write performance of the database. When MySQL performs a write operation, first

MySQL database is a common relational database. As the amount of data in the database increases and query requirements change, underlying optimization becomes particularly important. In the process of underlying optimization of MySQL, SQL statement optimization is a crucial task. This article will discuss common techniques and principles for SQL statement optimization and provide specific code examples. First of all, SQL statement optimization needs to consider the following aspects: index optimization, query statement optimization, stored procedure and trigger optimization, etc. In these aspects, we will have

MySQL is a widely used open source database management system for storing and managing large amounts of data. However, when using MySQL, you may encounter a variety of problems, from simple syntax errors to more complex performance issues and glitches. In this article, we will explore some of the most common MySQL problems and solutions. Connection Problems Connection problems are common. If you cannot connect to the MySQL server, please check the following points: 1) Whether the MySQL server is running 2) Whether the network connection is normal 3) MySQ
