douse8732 2010-01-28 20:26
浏览 44
已采纳

如何从PHP获取Oracle SQL查询的EXPLAIN PLAN?

I'm using Oracle 10g Express on Windows XP, and on a Macbook OS X 10.5.8 running PHP 5.3.1 with PDO and Oracle Instant Client 10.2.0.4.0_4, I run the following script:

<?php
$pdo = new PDO("oci:dbname=winders:1521/xe", "system", "XXXXXX");
...

$sql = "WITH NumberedBugs AS (
    SELECT b.*, ROW_NUMBER() OVER (ORDER BY bug_id) AS RN 
    FROM Bugs b
) SELECT * FROM NumberedBugs WHERE RN = :offset";

$stmt = $pdo->prepare($sql);
$stmt->execute($offset);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

No problem. But I also want to be able to write a PHP script that gets the EXPLAIN PLAN report for this query. When I try this, I get an error:

$sql = "EXPLAIN PLAN FOR WITH NumberedBugs AS (
    SELECT b.*, ROW_NUMBER() OVER (ORDER BY bug_id) AS RN 
    FROM Bugs b
) SELECT * FROM NumberedBugs WHERE RN = 1234";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);

(I changed the query parameter to a hard-coded integer literal for this test.)

I get this error when I try to call fetch():

ORA-24374: define not done before fetch or execute and fetch

What's the explanation of this, and how can I get the EXPLAIN PLAN report from a PHP script? I've done some web searches for this error, but I couldn't find a clear enough explanation.

  • 写回答

1条回答 默认 最新

  • dqvzfp6468 2010-01-28 20:31
    关注

    EXPLAIN PLAN FOR only writes the execution plan into a table (default plan_table).

    To actually read the explain plan you could do a plain select from this table, or do a

    SELECT * FROM TABLE(dbms_xplan.display);
    

    after your first query to get a formatted output.

    See Using EXPLAIN PLAN for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊