Home Database Mysql Tutorial 为什么全表扫描成本(COST)公式里面要除以sreadtim

为什么全表扫描成本(COST)公式里面要除以sreadtim

Jun 07, 2016 pm 04:04 PM
cost sre Why official cost scanning

全表扫描的成本计算公式 如下: Cost = ( #SRds * sreadtim + #MRds * mreadtim + CPUCycles / cpuspeed ) / sreadtim 全表扫描的时候,单块读次数=0,#SRds表示单块读次数。全表扫描的成本里面,CPU消耗其实非常少,可以忽略不计,所以全表扫描的公式可以改

全表扫描的成本计算公式 如下:

Cost = (
       #SRds * sreadtim +
       #MRds * mreadtim +
       CPUCycles / cpuspeed
       ) / sreadtim
Copy after login

全表扫描的时候,单块读次数=0,#SRds表示单块读次数。全表扫描的成本里面,CPU消耗其实非常少,可以忽略不计,所以全表扫描的公式可以改写为:

Cost = #MRds * mreadtim  / sreadtim
Copy after login

#MRds 表示多块读io次数

mreadtim 表示一次多块读耗费时间

sreadtim 表示一次单块读耗费时间

全表扫描的COST意思其实就是 多块读io次数 * 多块读时间 / 单块读时间

那么为什么 全表扫描成本公式里面要除以 sreadtim呢? 我们来看一下 索引扫描的成本计算公式:

cost =  
 blevel +  
 celiling(leaf_blocks *effective index selectivity) +  
 celiling(clustering_factor * effective table selectivity)  
Copy after login

blevel 是 扫描索引 的 root到branch ---单块读

celiling(leaf_blocks *effective index selectivity) 是扫描的 也在块的个数 ---单块读

ceiling(clustering_factor*effective table selectivity) 是 通过索引的rowid回表的次数 ---单块读

那么 索引扫描的成本公式的本质含义 其实就是 单块读的 io次数

对于单表访问(什么是单表访问? 就是只select一个表),要么走全表扫描,要么走索引扫描,当然了还可以走物化视图(这个不考虑)

ORACLE 究竟是走全表扫描还是走索引扫描呢? 它是计算全表扫描的COST,计算索引扫描的COST 哪个COST消耗少就走哪个。

问题来了,为啥ORACLE计算COST能准确的判断走哪个是最优的呢? 现在我们来比较一下 全表扫描的 COST 与 索引扫描的COST

全表扫描COST 多块读io次数 * 多块读时间 / 单块读时间

索引扫描COST 单块读io次数

那么现在 我们都对 全表扫描 COST , 都对 索引扫描 COST 乘以一个 单块读时间

全表扫描COST* 单块读时间 = 多块读io次数*多块读时间 = 总的耗费时间

索引扫描COST* 单块读时间 = 单块读io次数*单块读时间 = 总的耗费时间

说白了,就是比较全表扫描与索引扫描谁 总的耗费的时间最少,就选谁。

到这里你应该看懂了吧。 不得不佩服ORACLE 设计 人员的头脑,真是牛逼。

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months 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)

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

Does H5 page production require continuous maintenance? Does H5 page production require continuous maintenance? Apr 05, 2025 pm 11:27 PM

The H5 page needs to be maintained continuously, because of factors such as code vulnerabilities, browser compatibility, performance optimization, security updates and user experience improvements. Effective maintenance methods include establishing a complete testing system, using version control tools, regularly monitoring page performance, collecting user feedback and formulating maintenance plans.

Can JS run without H5? Can JS run without H5? Apr 06, 2025 am 09:06 AM

Is JavaScript available to run without HTML5? The JavaScript engine itself can run independently. Running JavaScript in a browser environment depends on HTML5 because it provides the standardized environment required to load and execute code. The APIs and features provided by HTML5 are crucial to modern JavaScript frameworks and libraries. Without HTML5 environments, many JavaScript features are difficult to implement or cannot be implemented.

Why can custom style sheets take effect on local web pages in Safari but not on Baidu pages? Why can custom style sheets take effect on local web pages in Safari but not on Baidu pages? Apr 05, 2025 pm 05:15 PM

Discussion on using custom stylesheets in Safari Today we will discuss a custom stylesheet application problem for Safari browser. Front-end novice...

What are the advantages of H5 page production What are the advantages of H5 page production Apr 05, 2025 pm 11:48 PM

The advantages of H5 page production include: lightweight experience, fast loading speed, and improving user retention. Cross-platform compatibility, no need to adapt to different platforms, improving development efficiency. Flexibility and dynamic updates, no audit required, making it easier to modify and update content. Cost-effective, lower development costs than native apps.

Unable to log in to mysql as root Unable to log in to mysql as root Apr 08, 2025 pm 04:54 PM

The main reasons why you cannot log in to MySQL as root are permission problems, configuration file errors, password inconsistent, socket file problems, or firewall interception. The solution includes: check whether the bind-address parameter in the configuration file is configured correctly. Check whether the root user permissions have been modified or deleted and reset. Verify that the password is accurate, including case and special characters. Check socket file permission settings and paths. Check that the firewall blocks connections to the MySQL server.

Why does a specific div element in the Edge browser not display? How to solve this problem? Why does a specific div element in the Edge browser not display? How to solve this problem? Apr 05, 2025 pm 08:21 PM

How to solve the display problem caused by user agent style sheets? When using the Edge browser, a div element in the project cannot be displayed. After checking, I posted...

The relationship between Bootstrap Table garbled and page encoding The relationship between Bootstrap Table garbled and page encoding Apr 07, 2025 pm 12:03 PM

Bootstrap Table garbled is usually because the page encoding is inconsistent with the table data encoding. To solve this problem, you need to make sure they are consistent. The specific steps include: checking page and table data encoding, setting page encoding, and verifying the encoding. If UTF-8 is used, the server should also support it. If it cannot be resolved, try using the JavaScript encoding library.

See all articles