Home Database Mysql Tutorial 执行计划中Using filesort,Using temporary相关语句的优化解决_MySQL

执行计划中Using filesort,Using temporary相关语句的优化解决_MySQL

Jun 01, 2016 pm 01:36 PM
develop Web page plan

bitsCN.com


昨天听开发人员提到,相关的彩票网页当中一个页面刷新的很慢,特别是在提取数据的时候,
今天早上一到,便去找开发人员要去相关的也没进行浏览,窥探哪些数据出现了问题,开发人员
使用PHP开发,所以我用IE很容易就可以窥探到哪些sql执行的很慢,比如下;

执行计划中Using filesort,Using temporary相关语句的优化解决_MySQL

这个图上列出了,也没中取sql语句的相关执行时间预估比例,以此我可以探查到大概哪些语句会
影响到我们的业务系统!首先看到了有个500,200毫秒的问题,熟话说,枪打出头鸟,哈哈,优化
也一样,先把大的问题解决了,在来收拾小的问题(小的问题,也有可能受到大问题的干预造成),
于是我便把该语句找出来;如下;

SELECT 
  a.user_name as username, 
  a.order_date as ordertime, 
  a.bonus_value as bonus, 
  cm.name_1 as lname 
FROM 
  lot_sellform AS a 
INNER JOIN 
  code_mst AS cm ON a.lottery_id = cm.cd AND a.lottery_type = cm.lot_type
WHERE 
  a.bonus_value > 0 
ORDER BY 
  a.order_date DESC 
limit 
  10

基本上改弄的索引信息都弄到了,但是我在页面中却看到了这样的情况;如图;

执行计划中Using filesort,Using temporary相关语句的优化解决_MySQL

看到type类型基本都走了索引,而且extra列内还有using temporary,using filesort,他们用到了
临时表和在文件内进行了排序,才返回出来,这肯定不是按照我们原先设计的最优路线来走的,
而且相关的索引路线都没走上,这里我有查了相关的资料,在官网上,看到如下内容;(我用蓝色
来表名相关的信息)

在某些情况中,MySQL可以使用一个索引来满足ORDER BY子句,而不需要额外的排序。

即使ORDER BY不确切匹配索引,只要WHERE子句中的所有未使用的索引部分和所有额外的
ORDER BY 列为常数,就可以使用索引。下面的查询使用索引来解决ORDER BY部分:

SELECT * FROM t1

  ORDER BY key_part1,key_part2,... ;

 

SELECT * FROM t1

  WHERE key_part1=constant

  ORDER BY key_part2;

 

SELECT * FROM t1

  ORDER BY key_part1 DESC, key_part2 DESC;

 

SELECT * FROM t1

  WHERE key_part1=1

  ORDER BY key_part1 DESC, key_part2 DESC;

这几句话严重勾起了我的兴趣,爱好!哈,在排序中,去查看没有进行索引,而且我在日期列上
添加了btree索引了!怎么会没走呢?以下是图信息;

执行计划中Using filesort,Using temporary相关语句的优化解决_MySQL

从上图可以看出,排序仍然是在临时表,和文件中进行了,而且type还是ALL比较耗时的操作,
这里我又会想起前面官网中提及到的,key_part1,key_part2这两列,在where语句中,和order by中
出现的比率这么频繁,而且上面说,如果where语句中只要为啥用索引语句列的部分,和所有order by
列的数据如果为常数,可以使用索引路线来走,那如果我对两者来进行彼此的绑定了,比如;让其
来做个组合索引!

首先where条件中bonus_value的值,我们取得是常数,而且在进行排序的时候,我们选择的是
order_date日期的列值,如果彼此来进行绑定组合,sql在选择路线的窥探中首先会尝试,组合索
引中位于第一列的数列,进行handle的锁定,遍历到数值后会继续留住该handle的位于LRU列表
头中,接着继续进行数值的排序遍历结果集合,直到handle列被挤出index维护的元头之外!

其实这个不是让其走我们的bonus_value,order_date索引路径,而且让其走到我们前面INNER JOIN
中的索引路线,避免了让数据在临时表中出现,或者在磁盘文件中排序,其实就是增大了,我们在链接
条件中我们设计索引路线的概率问题!有点声东击西的概念!哈!以下图供参考:
 

执行计划中Using filesort,Using temporary相关语句的优化解决_MySQL

以此看到走了我们需要的索引路径了!
 

bitsCN.com
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 send web pages to desktop as shortcut in Edge browser? How to send web pages to desktop as shortcut in Edge browser? Mar 14, 2024 pm 05:22 PM

How to send web pages to the desktop as a shortcut in Edge browser? Many of our users want to display frequently used web pages on the desktop as shortcuts for the convenience of directly opening access pages, but they don’t know how to do it. In response to this problem, the editor of this issue will share the solution with the majority of users. , let’s take a look at the content shared in today’s software tutorial. The shortcut method of sending web pages to the desktop in Edge browser: 1. Open the software and click the "..." button on the page. 2. Select "Install this site as an application" in "Application" from the drop-down menu option. 3. Finally, click it in the pop-up window

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

What should I do if the images on the webpage cannot be loaded? 6 solutions What should I do if the images on the webpage cannot be loaded? 6 solutions Mar 15, 2024 am 10:30 AM

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

What to do if the webpage cannot be opened What to do if the webpage cannot be opened Feb 21, 2024 am 10:24 AM

How to solve the problem of web pages not opening With the rapid development of the Internet, people increasingly rely on the Internet to obtain information, communicate and entertain. However, sometimes we encounter the problem that the web page cannot be opened, which brings us a lot of trouble. This article will introduce you to some common methods to help solve the problem of web pages not opening. First, we need to determine why the web page cannot be opened. Possible reasons include network problems, server problems, browser settings problems, etc. Here are some solutions: Check network connection: First, we need

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

How to open php on the web page How to open php on the web page Mar 22, 2024 pm 03:20 PM

Executing PHP code in a web page requires ensuring that the web server supports PHP and is properly configured. PHP can be opened in three ways: * **Server environment:** Place the PHP file in the server root directory and access it through the browser. * **Integrated Development Environment: **Place PHP files in the specified web root directory and access them through the browser. * **Remote Server:** Access PHP files hosted on a remote server via the URL address provided by the server.

Summary of the five most popular Go language libraries: essential tools for development Summary of the five most popular Go language libraries: essential tools for development Feb 22, 2024 pm 02:33 PM

Summary of the five most popular Go language libraries: essential tools for development, requiring specific code examples. Since its birth, the Go language has received widespread attention and application. As an emerging efficient and concise programming language, Go's rapid development is inseparable from the support of rich open source libraries. This article will introduce the five most popular Go language libraries. These libraries play a vital role in Go development and provide developers with powerful functions and a convenient development experience. At the same time, in order to better understand the uses and functions of these libraries, we will explain them with specific code examples.

See all articles