Home Database Mysql Tutorial gather_plan_statistics查看sql的join部分的内存消耗

gather_plan_statistics查看sql的join部分的内存消耗

Jun 07, 2016 pm 04:40 PM
sql Check

遇见一个sql语句,感觉驱动表的顺序选择有问题,就倒腾了一会儿,具体的sql语句如下,这里推荐使用gather_plan_statistics来查看具体的每个执行计划消耗的IO资源、执行时间、预估和实际返回的rows。 SQL_ID dq4pj5cnn0gb8, child number 0 -----------------

遇见一个sql语句,感觉驱动表的顺序选择有问题,就倒腾了一会儿,具体的sql语句如下,这里推荐使用gather_plan_statistics来查看具体的每个执行计划消耗的IO资源、执行时间、预估和实际返回的rows。

SQL_ID  dq4pj5cnn0gb8, child number 0
-------------------------------------
select /*+ gather_plan_statistics*/a.SERVNUMBER, a.REGION   from
tbcs.SUBS_USEDTEL a, tbcs.CS_SUBS_SERVNUMBER_TRANS b  where a.SUBSID =
b.TRANSIN_SUBSID    and a.REGION = b.TRANSIN_REGION    and a.INTIME >
sysdate - 90    and a.RECDEFID in ('DropSubs', 'FraudDropSubs')    and
a.REGION = 20
 
Plan hash value: 2146127278
 
-----------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation               | Name                     | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |                          |      1 |        |    100 |00:00:01.08 |   19453 |       |       |          |
|*  1 |  HASH JOIN              |                          |      1 |   4749 |    100 |00:00:01.08 |   19453 |    24M|  3319K|   25M (0)|
|   2 |   PARTITION RANGE SINGLE|                          |      1 |   4749 |    374K|00:00:00.83 |   17257 |       |       |          |
|*  3 |    TABLE ACCESS FULL    | SUBS_USEDTEL             |      1 |   4749 |    374K|00:00:00.66 |   17257 |       |       |          |
|*  4 |   TABLE ACCESS FULL     | CS_SUBS_SERVNUMBER_TRANS |      1 |  13477 |   8795 |00:00:00.05 |    2196 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - access("A"."SUBSID"="B"."TRANSIN_SUBSID" AND "A"."REGION"="B"."TRANSIN_REGION")
   3 - filter(("A"."REGION"=20 AND INTERNAL_FUNCTION("A"."RECDEFID") AND "A"."INTIME">SYSDATE@!-90))
   4 - filter("B"."TRANSIN_REGION"=20)

这里cbo在执行计划3中预估SUBS_USEDTEL通过谓词条件返回的数据只有4749,而实际返回了374K数据,初步来看这个sql应该交换下驱动表的顺序,让CS_SUBS_SERVNUMBER_TRANS去做驱动表。

SQL_ID  8px917y6cub58, child number 0
-------------------------------------
select /*+ gather_plan_statistics leading(b a) */
 a.SERVNUMBER, a.REGION
  from tbcs.SUBS_USEDTEL a, tbcs.CS_SUBS_SERVNUMBER_TRANS b
 where a.SUBSID = b.TRANSIN_SUBSID
   and a.REGION = b.TRANSIN_REGION
   and a.INTIME > sysdate - 90
   and a.RECDEFID in ('DropSubs', 'FraudDropSubs')
   and a.REGION = 20
 
Plan hash value: 2680037744
 
-----------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation               | Name                     | Starts | E-Rows | A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |                          |      1 |        |    346 |00:00:00.66 |   20281 |       |       |          |
|*  1 |  HASH JOIN              |                          |      1 |   4749 |    346 |00:00:00.66 |   20281 |  1998K|  1998K| 2083K (0)|
|*  2 |   TABLE ACCESS FULL     | CS_SUBS_SERVNUMBER_TRANS |      1 |  13477 |  14135 |00:00:00.06 |    3024 |       |       |          |
|   3 |   PARTITION RANGE SINGLE|                          |      1 |   4749 |    374K|00:00:00.78 |   17257 |       |       |          |
|*  4 |    TABLE ACCESS FULL    | SUBS_USEDTEL             |      1 |   4749 |    374K|00:00:00.61 |   17257 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------
 
Predicate Information (identified by operation id):
---------------------------------------------------
 
   1 - access("A"."SUBSID"="B"."TRANSIN_SUBSID" AND "A"."REGION"="B"."TRANSIN_REGION")
   2 - filter("B"."TRANSIN_REGION"=20)
   4 - filter(("A"."REGION"=20 AND INTERNAL_FUNCTION("A"."RECDEFID") AND "A"."INTIME">SYSDATE@!-90))

我们添加了hint lleading(b a)强制指定关联顺序,在整个sql消耗的逻辑读其实是没多大的变化,其实这里主要需要普及的一个知识点就是hash join的关联cbo是不会计算到逻辑读的。

那么这两个sql好像IO成本每多大的变化啊,但是我们观察OMem、1Mem、Used-Mem三项是有显著变化的,这里简单解释下这三个指标的信息
OMem为最优执行模式所需的内存评估值
1Mem为one-pass模式所需的内存评估值
Used-Mem则为实际执行时消耗的内存,而且我们还看见25M (0)和2083K (0)都有一个括号0,这个表示该sql是最优执行模式执行的

可以看出制定了正确的驱动表可以大幅度的减轻系统的内存消耗,这里也提供了我们一个思路就是优化sql时不能仅仅去关注IO资源,还要关注下内存的消耗,通过gather_plan_statistics可以很直观的观察到sql执行时join关联部分的内存消耗,

oracle官当对于memstats的解释(allstats=iostats+memstats的组合):

?MEMSTATS – Assuming that PGA memory management is enabled (that is,pga_aggregate_target parameter is set to a non 0 value), this format allows to display memory management statistics (for example, execution mode of the operator, how much memory was used, number of bytes spilled to disk, and so on). These statistics only apply to memory intensive operations like hash-joins, sort or some bitmap operators.

这个used-men和v$sql或者v$sqlarea的视图记录内存消耗的列是不相同的,used-mem是执行sql部分join消耗的pga内存部分,而v$sql或者v$sqlarea记录的是cursor的信息

sharable_mem:Amount of shared memory used by a cursor. If multiple child cursors exist, then the sum of all shared memory used by all child cursors.
persistent_mem:Fixed amount of memory used for the lifetime of an open cursor. If multiple child cursors exist, then the fixed sum of memory used for the
lifetime of all the child cursors.
runtime_mem:Fixed amount of memory required during execution of a cursor. If multiple child cursors exist, then the fixed sum of all memory required
during execution of all the child cursors.

这里我们需要注意的时优化sql时不能仅仅只是以逻辑读去衡量某个sql的性能,对于用户而言我们肯定是最关注sql的响应时间,我们优化IO、减少内存和cpu消耗等都是为了让执行sql时做尽可能少的事情,进而提高sql的响应时间。

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
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Check out the steps to delete a logged-in device on Douyin Check out the steps to delete a logged-in device on Douyin Mar 26, 2024 am 09:01 AM

1. First, click to open the Douyin app and click [Me]. 2. Click the three-dot icon in the upper right corner. 3. Click to enter [Settings]. 4. Click to open [Account and Security]. 5. Select and click [Log in to device management]. 6. Finally, click to select the device and click [Remove].

How to check your own ID on Xianyu_Introduction to how to check your personal nickname on Xianyu How to check your own ID on Xianyu_Introduction to how to check your personal nickname on Xianyu Mar 22, 2024 am 08:21 AM

As a trading platform, Xianyu requires you to register and log in to your account before using it. Users can set an ID name for their account. What if they want to check what their ID is? Let’s find out together below! Introduction to how to view personal nicknames on Xianyu. First, start the Xianyu app. After entering the homepage, switch to the page of selling idle, messages, and me, and click the [My] option in the lower right corner. 2. Then on my page we need to click [Avatar] in the upper left corner; 2. Then when we go to the personal homepage page we can see different information, we need to click the [Edit Information] button here; 4. Finally click We can see it later on the page where we edit information;

Where to check music rankings on NetEase Cloud Music_How to check music rankings on NetEase Cloud Music Where to check music rankings on NetEase Cloud Music_How to check music rankings on NetEase Cloud Music Mar 25, 2024 am 11:40 AM

1. After turning on the phone, select NetEase Cloud Music. 2. After entering the homepage, you can see the [Ranking List] and click to enter. 3. In the ranking list, you can select any list and click [New Song List]. 4. Select your favorite song and click on it. 5. Return to the previous page to see more lists.

How to view the hot list of Kuaishou Live Companion videos How to view the hot list of Kuaishou Live Companion videos Mar 29, 2024 pm 08:09 PM

Kuaishou Live Companion is not only a powerful live broadcast auxiliary tool, but also a real-time insight platform for hot topics and trends created for broadcasters. Through this function, anchors can quickly capture the content that audiences are most concerned about, and then adjust the live content to make it more in line with the audience's tastes and interests. So how to check the hot video list in the Kuaishou Live Companion app? This tutorial guide will provide you with a detailed introduction to the steps. I hope it can help you. How to view the hot video list on Kuaishou Live Companion? The second step is to click on the daily video hot list. The third step is to check the daily video hot list.

How to check how many groups you have joined on WeChat: a simple step How to check how many groups you have joined on WeChat: a simple step Mar 26, 2024 am 10:06 AM

Regardless of life or work, many people have long been deeply tied to WeChat and will be pulled into various groups at any time. So how many WeChat groups have you joined? You may immediately want to view the group chats in your address book, but only the WeChat groups you have saved in your address book will appear there, and other groups will not be visible. If you want to see all the WeChat groups you have joined, it is very simple: enter your nickname in the search box on the WeChat homepage, then find the group chat section in the search results, and click "More Group Chats" to view all related group chat information. Anyway, I was shocked. There were more than a hundred of them, and the scroll bar on the right became very small. Unfortunately, there is no specific number statistics... This method is also applicable to checking the QQ groups you have joined. PS: Some netizens also provided a trick:

How do I check which groups I have joined? How do I check which groups I have joined? Apr 01, 2024 pm 05:34 PM

WeChat group chat is not only a simple chat platform, but also a communication circle that brings together elites and enthusiastic friends from all walks of life. So today I will teach you how to see how many groups you have added on WeChat and how to save group chats. Usually Users who use WeChat must not miss it. How to check how many groups you have added to WeChat and how to save group chats To check how many groups you have added to WeChat: 1. You can view your group chat window in the WeChat main interface 2. If you have already saved the group chat, you can tap [ Address Book] - [Group Chat] 3. After entering the group chat, you can view the saved group. Save the WeChat group: 1. Select the group you want to save, top right [...] 2. Open in the chat message [Save to address book] 3. On the main WeChat interface, tap [Address Book]-[Group Chat] to view

How to view the Amap Help Center_How to view the Amap Help Center How to view the Amap Help Center_How to view the Amap Help Center Apr 01, 2024 pm 05:26 PM

1. We first open the Gaode map. 2. Then click (My) in the lower right corner of the Amap homepage and then click Settings in the upper right corner. 3. Finally, you can see the help center of Amap.

See all articles