Oracle获取执行计划的几种方法
为了获取缓存库中的执行计划,可以直接查询动态性能视图v$sql_plan和v$sql_plan_statistics_all等,但更方便的方法是以sql_id和子
1. 预估执行计划 - Explain Plan
Explain plan以SQL语句作为输入,得到这条SQL语句的执行计划,并将执行计划输出存储到计划表中。
首先,在你要执行的SQL语句前加explain plan for,此时将生成的执行计划存储到计划表中,语句如下:
explain plan for SQL语句
然后,在计划表中查询刚刚生成的执行计划,语句如下:
select * from table(dbms_xplan.display);
注意:Explain plan只生成执行计划,并不会真正执行SQL语句,因此产生的执行计划有可能不准,因为:
1)当前的环境可能和执行计划生成时的环境不同;
2)不会考虑绑定变量的数据类型;
3)不进行变量窥视。
2. 查询内存中缓存的执行计划 (dbms_xplan.display_cursor)
如果你想获取正在执行的或刚执行结束的SQL语句真实的执行计划(即获取library cache中的执行计划),可以到动态性能视图里查询。方法如下:
1)获取SQL语句的游标
游标分为父游标和子游标,父游标由sql_id(或联合address和hash_value)字段表示,子游标由child_number字段表示。
如果SQL语句正在运行,可以从v$session中获得它的游标信息,如:
select status, sql_id, sql_child_number from v$session where status='ACTIVE' and ....
如果知道SQL语句包含某些关键字,可以从v$sql视图中获得它的游标信息,如:
select sql_id, child_number, sql_text from v$sql where sql_text like '%关键字%‘
2)获取库缓存中的执行计划
为了获取缓存库中的执行计划,可以直接查询动态性能视图v$sql_plan和v$sql_plan_statistics_all等,但更方便的方法是以sql_id和子游标为参数,执行如下语句:
select * from table(dbms_xplan.display_cursor('sql_id',child_number));
3)获取前一次的执行计划:
set serveroutput off
select * from table(dbms_xplan.display_cursor(null,null,'ALLSTATS LAST'));
3. 查询历史执行计划(dbms_xplan.display_awr)
AWR会定时把动态性能视图中的执行计划保存到dba_hist_sql_plan视图中,如果你想要查看历史执行计划,可以采用如下方法查询:
select * from table(dbms_xplan.display_awr('sql_id');
4. 在用sqlplus做SQL开发是(Autotrace)
set autotrace是sqlplus工具的一个功能,只能在通过sqlplus连接的session中使用,它非常适合在开发时测试SQL语句的性能,有以下几种参数可供选择:
SET AUTOTRACE OFF ---------------- 不显示执行计划和统计信息,这是缺省模式
SET AUTOTRACE ON EXPLAIN ------ 只显示优化器执行计划
SET AUTOTRACE ON STATISTICS -- 只显示统计信息
SET AUTOTRACE ON ----------------- 执行计划和统计信息同时显示
SET AUTOTRACE TRACEONLY ------ 不真正执行,,只显示预期的执行计划,同explain plan
5. 生成Trace文件查询详细的执行计划 (SQL_Trace, 10046)
SQL_TRACE作为初始化参数可以在实例级别启用,也可以只在会话级别启用,在实例级别启用SQL_TRACE会导致所有进程的活动被跟踪,包括后台进程及所有用户进程,这通常会导致比较严重的性能问题,所以在一般情况下,我们使用sql_trace跟踪当前进程,方法如下:
SQL>alter session set sql_trace=true;
...被跟踪的SQL语句...
SQL>alter session set sql_trace=false;
如果要跟踪其它进程,可以通过Oracle提供的系统包DBMS_SYSTEM. SET_SQL_TRACE_IN_SESSION来实现,例如:
SQL> exec dbms_system.set_sql_trace_in_session(sid,serial#,true) --开始跟踪
SQL> exec dbms_system.set_sql_trace_in_session(sid,serial#,false) --结束跟踪
生成trace文件后,再用tkprof 工具将sql trace 生成的跟踪文件转换成易读的格式,语法如下:
tkprof inputfile outputfile
10046事件是SQL_TRACE的一个升级版,它也是追踪会话,生成Trace文件,只是它里面的内容更详细,

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PrimeFactor−Innumbertheory,theprimefactorsofapositiveintegeraretheprimenumbersthatdividethatintegerexactly.Theprocessoffindingthesenumbersiscalledintegerfactorization,orprimefactorization.Example−Primefactorsof288are:288=2x2x2x2x2

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

This article recommends ten digital currency trading apps: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. Poloniex. When choosing a platform, you need to consider factors such as security, liquidity, transaction fees, currency selection, user interface, customer service support and regulatory compliance, and carefully evaluate risks and never blindly follow the trend.

Reliable digital currency platforms include: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini. These exchanges have their own characteristics. Users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.

This article provides Android and Apple mobile APP download methods for mainstream digital currency trading platforms such as Binance, OKX, Gate.io, Huobi Global, Coinbase, KuCoin, Kraken and Bitfinex. Whether it is an Android user or an Apple user, you can easily find the official APP download link for the corresponding platform and complete the installation according to the steps. The article provides detailed guidance on searching and downloading on their respective official websites or app stores, and provides instructions on the special steps for installing APK files on Android, so that users can download and use them quickly and easily.

The rankings of the top ten virtual currency trading platforms are: 1. OKX; 2. Binance; 3. Gate.io; 4. Huobi Global; 5. Kraken; 6. Coinbase; 7. KuCoin; 8. Crypto.com; 9. Bitfinex; 10. Gemini. The ranking is based on comprehensive considerations such as platform liquidity, currency selection, security, user experience, handling fees and compliance, but is for reference only. Investment should be cautious and at your own risk.
