搜尋
首頁資料庫mysql教程如何获取执行计划

如何获取执行计划

Jun 07, 2016 pm 04:09 PM
oracle如何執行獲取計劃

如何oracle的获取执行计划1.获取普通执行计划,效果类似于先执行set autot on exp;然后执行sql。 explan plan for your_sql; select * from table(dbms_xplan.display);2.获取具有outline信息的执行计划,用sqlprofile调优时非常有用,或者用这个执行计划了

如何oracle的获取执行计划
1.获取普通执行计划,效果类似于先执行set autot on exp;然后执行sql。
  explan plan for your_sql;
  select * from table(dbms_xplan.display);
2.获取具有outline信息的执行计划,用sqlprofile调优时非常有用,或者用这个执行计划了解更多oracle内部的hint
  explan plan for your_sql;
  select * from table(dbms_xplan.display(null, null,'advanced -projection'))
3.真实的执行计划,可以看到实际的 Starts(执行次数) | E-Rows(估算的返回行数) | A-Rows(实际的返回行数)
  ALTER SESSION SET STATISTICS_LEVEL=ALL; 
  execute your_sql;
  SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST'))
  
那么这3中获取执行计划的方式可以写到一个脚本getplan.sql,用的时候非常方便。
--getplan.sql
set feedback off timing off ver off
pro 'general,outline,starts'
pro
acc type prompt 'Enter value for plan type:' default 'general'
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL,NULL,'ALLSTATS LAST')) where '&&type'='starts';
select * from table(dbms_xplan.display) where '&&type'='general';
select * from table(dbms_xplan.display(null, null,'advanced -projection')) where '&&type'='outline';
set feedback on timing on ver on
undef type


测试如下:
SQL> select * from a;

        ID NAME
---------- ----------
         1 a1
         2 a2
         3 a3
         4 a4
         5 a5
SQL> select * from b;

        ID NAME
---------- ----------
         1 b1
         2 b2
         
         
         
--执行计划1:普通执行计划
SQL> explain plan for select a.*,(select name from b where b.id=a.id) from a;

Explained.

Elapsed: 00:00:00.04
SQL> @getplan
'general,outline,starts'

Enter value for plan type:                ----这里输入general或回车

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------
Plan hash value: 3653839899

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     5 |   100 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| B    |     1 |    20 |     3   (0)| 00:00:01 |
|   2 |  TABLE ACCESS FULL| A    |     5 |   100 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("B"."ID"=:B1)

Note
-----
   - dynamic sampling used for this statement
   
   
   
--执行计划2:outline执行计划
SQL> explain plan for select a.*,(select name from b where b.id=a.id) from a;

Explained.

Elapsed: 00:00:00.01
SQL> @getplan
'general,outline,starts'

Enter value for plan type:outline           --这里输入outline

PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------
Plan hash value: 3653839899

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     5 |   100 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| B    |     1 |    20 |     3   (0)| 00:00:01 |
|   2 |  TABLE ACCESS FULL| A    |     5 |   100 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$2 / B@SEL$2
   2 - SEL$1 / A@SEL$1

Outline Data
-------------

  /*+
      BEGIN_OUTLINE_DATA
      FULL(@"SEL$2" "B"@"SEL$2")
      FULL(@"SEL$1" "A"@"SEL$1")
      OUTLINE_LEAF(@"SEL$1")
      OUTLINE_LEAF(@"SEL$2")
      ALL_ROWS
      OPTIMIZER_FEATURES_ENABLE('10.2.0.4')
      IGNORE_OPTIM_EMBEDDED_HINTS
      END_OUTLINE_DATA
  */

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("B"."ID"=:B1)

Note
-----
   - dynamic sampling used for this statement   




--执行计划3:real执行计划
SQL> set serveroutput off
SQL> ALTER SESSION SET STATISTICS_LEVEL=ALL;

Session altered.

Elapsed: 00:00:00.00
SQL> select a.*,(select name from b where b.id=a.id) from a;

        ID NAME       (SELECTNAM
---------- ---------- ----------
         1 a1         b1
         2 a2         b2
         3 a3
         4 a4
         5 a5

5 rows selected.

Elapsed: 00:00:00.03
SQL> @getplan
'general,outline,starts'

Enter value for plan type:starts                  --这里输入starts

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------
SQL_ID  8rv825dykpx1m, child number 0
-------------------------------------
select a.*,(select name from b where b.id=a.id) from a

Plan hash value: 3653839899

------------------------------------------------------------------------------------
| Id  | Operation         | Name | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
------------------------------------------------------------------------------------
|*  1 |  TABLE ACCESS FULL| B    |      5 |      1 |      2 |00:00:00.01 |      35 |
|   2 |  TABLE ACCESS FULL| A    |      1 |      5 |      5 |00:00:00.01 |       8 |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("B"."ID"=:B1)

Note
-----
   - dynamic sampling used for this statement

--注意:
--第3种执行计划不能多次获取,只能执行1次,获取一次,否则会获取不到
下面再次获取一下试试:
SQL> @getplan
'general,outline,starts'

Enter value for plan type:starts

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------
SQL_ID  dvp8nn63wuhs8, child number 0
-------------------------------------
select * from table(dbms_xplan.display(null, null,'advanced -projection'))
where 'starts'='outline'

Plan hash value: 3440229843

-------------------------------------------------------------------------------------
| Id  | Operation                          | Name    | Starts | A-Rows |   A-Time   |
-------------------------------------------------------------------------------------
|*  1 |  FILTER                            |         |      1 |      0 |00:00:00.01 |
|   2 |   COLLECTION ITERATOR PICKLER FETCH| DISPLAY |      0 |      0 |00:00:00.01 |   --第二次无法获取真实的执行计划
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(NULL IS NOT NULL)

Note
-----
   - rule based optimizer used (consider using cbo)
   
   
--第3种执行计划要关掉set serveroutput off,否则也不能获取执行计划。
测试如下:
这里和上面的测试是同一个会话,所以没有再执行ALTER SESSION SET STATISTICS_LEVEL=ALL;了。
SQL> set serveroutput on
SQL> select a.*,(select name from b where b.id=a.id) from a;

        ID NAME       (SELECTNAM
---------- ---------- ----------
         1 a1         b1
         2 a2         b2
         3 a3
         4 a4
         5 a5

5 rows selected.

Elapsed: 00:00:00.03
SQL> @getplan
'general,outline,starts'

Enter value for plan type:starts

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------------------------
SQL_ID  9babjv8yq8ru3, child number 0

BEGIN DBMS_OUTPUT.GET_LINES(:LINES, :NUMLINES); END;                    --也无法获取真实的执行计划

NOTE: cannot fetch plan for SQL_ID: 9babjv8yq8ru3, CHILD_NUMBER: 0
      Please verify value of SQL_ID and CHILD_NUMBER;
      It could also be that the plan is no longer in cursor cache (check v$sql_plan)   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存儲斑點安全嗎?mysql:存儲斑點安全嗎?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通過PHP Web界面添加用戶mySQL:通過PHP Web界面添加用戶May 14, 2025 am 12:04 AM

通過PHP網頁界面添加MySQL用戶可以使用MySQLi擴展。步驟如下:1.連接MySQL數據庫,使用MySQLi擴展。 2.創建用戶,使用CREATEUSER語句,並使用PASSWORD()函數加密密碼。 3.防止SQL注入,使用mysqli_real_escape_string()函數處理用戶輸入。 4.為新用戶分配權限,使用GRANT語句。

mysql:blob和其他無-SQL存儲,有什麼區別?mysql:blob和其他無-SQL存儲,有什麼區別?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而ilenosqloptionslikemongodb,redis和calablesolutionsolutionsolutionsoluntionsoluntionsolundortionsolunsonstructureddata.blobobobissimplobisslowdeperformberbutslowderformandperformancewithlararengedata;

mySQL添加用戶:語法,選項和安全性最佳實踐mySQL添加用戶:語法,選項和安全性最佳實踐May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串數據類型常見錯誤?MySQL:如何避免字符串數據類型常見錯誤?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingSefectery.1)usecharforfixed lengengtrings,varchar forvariable-varchar forbariaible length,andtext/blobforlargerdataa.2 seterters seterters seterters

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。