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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器