测量plsql过程中sql语句的时间
我必须编写一个过程来保存表中任何 sql 语句的执行时间。
该过程由 execmeasuresqltime('sql statements as string');
调用,
我的想法是这样的:
--declarations
timestart NUMBER;
BEGIN
dbms_output.enable;
timestart:=dbms_utility.get_time();
EXECUTE IMMEDIATE sql
COMMIT;
dbms_output.put_line(dbms_utility.get_time()-timestart);
-- save time
但它对我来说不起作用 SELECT *...
条款。 (我认为 sql 需要 INTO 顺序)
有没有办法在过程中执行任何 sql 数据?
I must write a procedure which save the execute time of any sql-statement in a table.
The procedure is calling by exec measuresqltime('sql statement as string');
My idea is like this:
--declarations
timestart NUMBER;
BEGIN
dbms_output.enable;
timestart:=dbms_utility.get_time();
EXECUTE IMMEDIATE sql
COMMIT;
dbms_output.put_line(dbms_utility.get_time()-timestart);
-- save time
But it didn't work for me for a SELECT *...
clause. (I think sql need a INTO-order)
Is there a way to execute any sql-atatements in a procedure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的 SQL 语句是 SELECT,则需要从游标中获取数据以对其执行时间进行有意义的测量。
如果不从游标中获取,则仅测量“解析”和“执行”阶段所花费的时间,而大部分工作通常在 SELECT 语句的“获取”阶段完成。
如果您不知道实际语句将具有的列数,您将无法使用
EXECUTE IMMEDIATE
或OPENcursor FOR 'string'
进行获取。您必须使用动态 SQL 包DBMS_SQL
< /a> 如果 SELECT 的列数/类型未知。下面是一个示例:
请注意,这将测量获取 SELECT 的最后一行所需的时间。
If your SQL statement is a SELECT, you need to fetch from the cursor to have a meaningful measure of its execution time.
If you don't fetch from the cursor, you only measure the time spent in "parse" and "execution" phases, whereas much of the work is usually done in the "fetch" phase for SELECT statements.
You won't be able to fetch with
EXECUTE IMMEDIATE
orOPEN cursor FOR 'string'
if you don't know the number of columns the actual statement will have. You will have to use the dynamic SQL packageDBMS_SQL
if the number/type of columns of the SELECT is unknown.Here's an example:
Note that this will measure the time needed to fetch to the last row of the SELECT.
完成 devosJava 的回答...避免在黎明时使用它;P
completing devosJava answered... avoid using it at dawn ;P
计算执行时间的持续时间
To calculate the duration for an execution time
插入 PROC_RUNTIMES(PROC_NAME、START_TIME、END_TIME)
值('PROC_NAME',TO_CHAR
(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'),NULL);
您的查询在这里;
插入 PROC_RUNTIMES(PROC_NAME、START_TIME、END_TIME)
值('PROC_NAME',NULL,TO_CHAR
(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'));
INSERT INTO PROC_RUNTIMES (PROC_NAME, START_TIME, END_TIME)
VALUES ('PROC_NAME', TO_CHAR
(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'),NULL );
Your query here;
INSERT INTO PROC_RUNTIMES (PROC_NAME, START_TIME, END_TIME)
VALUES ('PROC_NAME',NULL, TO_CHAR
(SYSDATE, 'DD/MM/YYYY HH24:MI:SS'));