Home Database Mysql Tutorial Mysql优化之延迟索引和分页优化_MySQL

Mysql优化之延迟索引和分页优化_MySQL

Jun 01, 2016 pm 01:04 PM
index

什么是延迟索引?使用索引查询出来数据,之后把查询结果和同一张表中数据进行连接查询,进而提高查询速度!

分页是一个很常见功能,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 < 1000000 do
		set num := num +1;
		insert into smth1 values (num ,num,&#39;我是*****&#39;,&#39;我是谁&#39;);
	end while ;

end;
Copy after login

查询:

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 |
Copy after login
可见随着查询$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 |
Copy after login
此外,还会想到用in来查询而不是子查询,为什么不用in,使用in会先查询出来一条id,之后再去和下面进行匹配,会进行smth1进行全表扫描!

 

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)

How to fix 100% disk usage on Windows 11 How to fix 100% disk usage on Windows 11 Apr 20, 2023 pm 12:58 PM

How to Fix 100% Disk Usage on Windows 11 The straightforward way to find the problematic application or service causing 100% disk usage is to use Task Manager. To open Task Manager, right-click on the Start menu and select Task Manager. Click the Disk column header to see what's using the most resources. From there, you'll have a good idea of ​​where to start. However, the problem may be more serious than simply closing an application or disabling a service. Read on to find more potential causes of problems and how to fix them. Disabling SuperfetchSuperfetch feature (also known as SysMain in Windows 11) helps reduce startup time by accessing prefetch files

How to hide files and folders and remove them from search in Windows 11? How to hide files and folders and remove them from search in Windows 11? Apr 26, 2023 pm 11:07 PM

&lt;h2&gt;How to Hide Files and Folders from Search on Windows 11&lt;/h2&gt;&lt;p&gt;The first thing we need to look at is customizing the location of Windows Search files. By skipping these specific locations, you should be able to see results faster while also hiding any files you want to protect. &lt;/p&gt;&lt;p&gt;If you want to exclude files and folders from searches on Windows 11, use the following steps: &lt;/p&gt;&lt;ol&

Here are 6 ways to fix Windows 11 search bar not available. Here are 6 ways to fix Windows 11 search bar not available. May 08, 2023 pm 10:25 PM

If your search bar isn't working in Windows 11, there are a few quick ways to get it up and running in no time! Any Microsoft operating system can experience glitches from time to time, and the latest operating systems are not exempt from this rule. Additionally, as pointed out by user u/zebra_head1 on Reddit, the same error appears on Windows 11 with 22H2Build22621.1413. Users complained that the option to toggle the taskbar search box randomly disappeared. Therefore, you must be prepared for any situation. Why can't I type in the search bar on my computer? The inability to type on the computer can be attributed to different factors and processes. Here are some things you should be aware of: Ctfmon.

What are the Oracle index types? What are the Oracle index types? Nov 16, 2023 am 09:59 AM

Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

Windows 11 Outlook Search Not Working: 6 Fixes Windows 11 Outlook Search Not Working: 6 Fixes Apr 22, 2023 pm 09:46 PM

Run the Search and Indexing Troubleshooter in Outlook One of the more straightforward fixes you can start is to run the Search and Indexing Troubleshooter. To run the troubleshooter on Windows 11: Click the Start button or press the Windows key and select Settings from the menu. When Settings opens, select System > Troubleshooting > Additional Troubleshooting. Scroll down on the right side, find SearchandIndexing and click the Run button. Select Outlook Search to return no results and continue with the on-screen instructions. When you run it, the troubleshooter will automatically identify and fix the problem. After running the troubleshooter, open Outlook and see if the search is working properly. like

How to solve the problem that the index exceeds the array limit How to solve the problem that the index exceeds the array limit Nov 15, 2023 pm 05:22 PM

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Oct 15, 2023 am 11:39 AM

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages ​​and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve

See all articles