【发布时间】:2014-02-19 22:29:29
【问题描述】:
我必须将大约 50 多个验证函数转移到 Oracle 中。我正在寻找运行速度最快的方法,但如果可能的话,我还想解决boolean 问题。它们的返回对象都需要相同,以便应用程序能够以一致的方式对结果做出反应并提醒用户或显示我们可能需要的任何弹出窗口、消息。我为此创建了一个valObj,但还不确定这是否是最好的方法。可以更改返回格式,因为对其做出反应的前端尚未开发。最后它将包含许多不同的验证功能,从整数、数字、电话、电子邮件、IPv4、IPv6 等......这是我目前所拥有的......
/***
This is the validation object.
It stores 1 for valid, 0 for not valid and some helper text that can be relayed back to the user.
***/
create or replace type valObj as object (
result number(1),
resultText varchar(32000)
);
/***
Coming from ColdFusion this seems clean to me but the function
will end up being a couple thousand lines long.
***/
create or replace function isValid(v in varchar2, format in varchar2)
return valObj
is
test number;
begin
if format = 'number' then
begin
test := to_number(v);
return valObj(1,null);
exception when VALUE_ERROR then return valObj(0,'Invalid number. Valid formats are: 12345, 12345.67, -12345, etc...');
end;
elsif format = 'integer' then
null; --TO DO
elsif format = 'email' then
null; --TO DO
elsif format = 'IPv4' then
null; --TO DO
elsif format = 'IPv6' then
null; --TO DO
end if;
--dozens of others to follow....
end;
/
/* Example Usage in SQL */
select isValid('blah','number') from dual; -- returns: (0, Invalid number. Valid formats are: 12345, 12345.67, -12345, etc...)
select isValid('blah','number').result from dual; -- returns: 0
select isValid('blah','number').resulttext from dual; -- returns: Valid formats are: 12345, 12345.67, -12345, etc...
select isValid(1234567890.123,'number') from dual; -- returns: 1,{null}
select isValid(1234567890.123,'number').result from dual; -- returns: 1
select isValid(1234567890.123,'number').resulttext from dual; -- returns: {null}
/* Example Usage in PL/SQL */
declare
temp valObj;
begin
temp := isValid('blah','number');
if (temp.result = 0) then
dbms_output.put_line(temp.resulttext);
else
dbms_output.put_line('Valid');
end if;
end;
/
我的问题是:
- 在 PL/SQL 中使用它时,我希望能够像这样进行
boolean检查:if (temp.result) then但我想不出办法,因为这在 SQL 中不起作用。我应该只向valObj添加第三个布尔属性还是有其他我不知道的方法? - 这些验证函数最终可能会在大循环中被调用。知道这一点,这是完成这些验证的最有效方法吗?
如果有任何帮助,我将不胜感激。谢谢!
更新:我忘了MEMBER FUNCTIONS。感谢@Brian McGinity 提醒我。所以我想采用这种方法,因为它将type 和它的functions 封装在一起。 这种方法和单机功能会不会有速度上的差异?这会像独立函数一样编译和存储吗?
create or replace type isValid as object (
result number(1),
resulttext varchar2(32000),
constructor function isValid(v varchar, format varchar) return self as result );
/
create or replace type body isValid as
constructor function isValid(v varchar, format varchar) return self as result as
test number;
begin
if format = 'number' then
begin
test := to_number(v);
self.result := 1;
self.resulttext := null;
return;
exception when VALUE_ERROR then
self.result := 0;
self.resulttext := 'Invalid number. Valid formats are: 12345, 12345.67, -12345, etc...';
return;
end;
elsif format = 'phone' then
null; --TO DO
end if;
--and many others...
end;
end;
/
/* Example Usage in SQL */
select isValid('a','number') from dual;
/* Example Usage in PL/SQL */
declare
begin
if (isValid('a','number').result = 1) then
null;
end if;
end;
/
测试结果:
/* Test isValid (the object member function), this took 7 seconds to run */
declare
begin
for i in 1 .. 2000000 loop
if (isValid('blah','number').result = 1) then
null;
end if;
end loop;
end;
/* Test isValid2 (the stand-alone function), this took 16 seconds to run */
declare
begin
for i in 1 .. 2000000 loop
if (isValid2('blah','number').result = 1) then
null;
end if;
end loop;
end;
isValid 和 isValid2 都执行相同的代码,他们只是运行这一行 test := to_number(v); 然后如果失败则执行异常并返回结果。这似乎是一个有效的测试? Object成员函数方法其实比独立函数快???
【问题讨论】:
-
函数在数据库中编译,执行速度非常快,当没有
TABLE引用时(没有SQL)。对于boolean,您必须指定两个操作数以进行比较,例如true或false。boolean仅在内存使用方面对您有益。然后,您的两种方法都与我认为的相同。即使在循环中调用,也不应该成为问题。(如果您只在内部处理常量) -
谢谢。是的,它们几乎是一样的。是的,我知道如果验证函数中有 SQL(查询),它会减慢速度。我还试图让他们将
PLSQL_CODE_TYPE更改为 NATIVE:oracle-base.com/articles/11g/… “记住,本机编译会提高过程代码的速度,但对 SQL 的性能没有影响。”但是包也是在数据库中编译的,你还认为单个大函数会更快吗? -
是的,函数的大小应该无关紧要,因为在第一次执行后,它会在oracle最近的内存中,除非它很少被调用。并且连续调用不会最终将函数加载到内存中,而是立即执行。还有一些叫做pinning 的对象是SGA 的。这使得 Oracle 在 SGA 中始终拥有它,即使它不经常被调用。但不建议固定到 SGA,除非你需要它!
-
@OracleUser 我刚刚被提醒另一个选项。如果您有时间查看问题的 UPDATE 部分,想知道您的想法是什么。谢谢。
-
我很想看看你的结果。我不知道如何获得这两个不同的测量值,但我确实运行了两个相同的 PL/SQL 块,一个调用对象成员函数 200 万次,另一个调用独立函数。令我惊讶的是,这个物体的速度要快得多,7 秒而不是 16 秒!我会提出测试代码的问题。
标签: sql oracle validation plsql