欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Oracle硬解析的几个例子

程序员文章站 2024-02-13 21:12:16
...

Oracle进行软解析的SQL必须是完全相同的,所谓相同的SQL必须是大小写一致(测试例子3),甚至是不能多一个或者少一个空格,这个结

为了验证SQL硬解析的场景,设置了下面六个测试用的例子:

1、没有绑定变量下的普通查询
2、测试绑定变量下的查询
3、测试绑定变量下sql有变化的查询
4、测试DML非绑定变量的解析
5、测试在过程中执行插入的时候非绑定变量的SQL解析
6、使用了绑定变量之后的,过程中的SQL解析情况

/**
测试例子1:
没有绑定变量下的普通查询
**/
drop table foo purge;

CREATE TABLE foo AS
SELECT LEVEL AS x,100000-LEVEL AS y FROM dual CONNECT BY LEVEL

ALTER SYSTEM FLUSH SHARED_POOL;

SELECT * FROM foo WHERE x = 100;
SELECT * FROM foo WHERE x =999;
SELECT * FROM foo WHERE x=10000;

SELECT T.SQL_TEXT, T.SQL_ID, T.EXECUTIONS, T.PARSE_CALLS
FROM V$SQL T
WHERE UPPER(T.SQL_TEXT) LIKE '%FOO%';

/**
测试例子2:
测试绑定变量下的查询
**/
drop table foo purge;

CREATE TABLE foo AS
SELECT LEVEL AS x,100000-LEVEL AS y FROM dual CONNECT BY LEVEL

ALTER SYSTEM FLUSH SHARED_POOL;

VARIABLE temp NUMBER;

exec :temp :=99;
SELECT * FROM foo WHERE X = :temp;

exec :temp :=100;
SELECT * FROM foo WHERE X = :temp;

exec :temp :=101;
SELECT * FROM foo WHERE X = :temp;

SELECT T.SQL_TEXT, T.SQL_ID, T.EXECUTIONS, T.PARSE_CALLS
FROM V$SQL T
WHERE UPPER(T.SQL_TEXT) LIKE '%FOO%';

/**

测试例子3:
测试绑定变量下sql有变化的查询
**/

drop table foo purge;

CREATE TABLE foo AS
SELECT LEVEL AS x,100000-LEVEL AS y FROM dual CONNECT BY LEVEL

ALTER SYSTEM FLUSH SHARED_POOL;

VARIABLE temp NUMBER;

exec :temp :=99;
SELECT * FROM foo WHERE X = :temp;

exec :temp :=100;
SELECT * FROM FOO WHERE X = :temp;

exec :temp :=101;
SELECT * FROM foo WHERE X = :temp;

SELECT T.SQL_TEXT, T.SQL_ID, T.EXECUTIONS, T.PARSE_CALLS
FROM V$SQL T
WHERE UPPER(T.SQL_TEXT) LIKE '%FOO%';

/**
测试例子4:
测试DML非绑定变量的解析
**/

drop table foo purge;

CREATE TABLE foo AS
SELECT LEVEL AS x,100000-LEVEL AS y FROM dual CONNECT BY LEVEL

ALTER SYSTEM FLUSH SHARED_POOL;

INSERT INTO FOO VALUES(100,200);
INSERT INTO FOO VALUES(101,201);
INSERT INTO FOO VALUES(103,203);

SELECT T.SQL_TEXT, T.SQL_ID, T.EXECUTIONS, T.PARSE_CALLS
FROM V$SQL T
WHERE UPPER(T.SQL_TEXT) LIKE '%FOO%';

/**
测试例子5:
测试在过程中执行插入的时候的SQL解析
**/

drop table foo purge;

CREATE TABLE foo AS
SELECT LEVEL AS x,100000-LEVEL AS y FROM dual CONNECT BY LEVEL

ALTER SYSTEM FLUSH SHARED_POOL;

BEGIN
FOR I IN 1..3 LOOP
IF I=1 THEN
INSERT INTO FOO VALUES(1,1);
ELSIF I=2 THEN
INSERT INTO FOO VALUES(2,2);
ELSIF I=3 THEN
INSERT INTO FOO VALUES(3,3);
END IF;
END LOOP;
END;
/
SELECT T.SQL_TEXT, T.SQL_ID, T.EXECUTIONS, T.PARSE_CALLS
FROM V$SQL T
WHERE UPPER(T.SQL_TEXT) LIKE '%FOO%';

/**
测试例子6:
使用了绑定变量之后的,过程中的SQL解析情况
**/

drop table foo purge;

CREATE TABLE foo AS
SELECT LEVEL AS x,100000-LEVEL AS y FROM dual CONNECT BY LEVEL

ALTER SYSTEM FLUSH SHARED_POOL;

BEGIN
FOR I IN 1..200 LOOP
INSERT INTO FOO VALUES(I,100000-I);
END LOOP;
END;
/
SELECT T.SQL_TEXT, T.SQL_ID, T.EXECUTIONS, T.PARSE_CALLS
FROM V$SQL T
WHERE UPPER(T.SQL_TEXT) LIKE '%FOO%';

通过上述六个情况的试验,我们最终可以得到如下结论:

Oracle进行软解析的SQL必须是完全相同的,所谓相同的SQL必须是大小写一致(测试例子3),甚至是不能多一个或者少一个空格,这个结论可以通过修改测试例子3增加一个空格得到,结果就得到了不同的SQL_ID。只有完全一致的SQL,才可以得到相应的HASH值,从而才可以进行软解析。对于在SQL池中,我们需要分析在SQL池中出现的只有参数部分不同的SQL,如果出现了很多次,,我们就有必要对其进行相应的变量绑定,从而降低硬解析成本,提高性能。