Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL avoid calling same function twice in select statement

I have a select statement calling same function two times for returning two different columns, Since the same function is called twice, it's creating a performance issue.

I want to call function only once and copy its value to another column. Is it possible in oracle ?

SELECT ID
    ,PKGRESTFUNCTION.getBlock(table.ID, table.TYPE) "BLOCK" 
    ,PKGRESTFUNCTION.getBlock(table.ID, table.TYPE) "MASK"
    from table 
    where ID=condition;
like image 980
2787184 Avatar asked Dec 22 '25 22:12

2787184


1 Answers

You can use with clause to get data once

with block as (
select PKGRESTFUNCTION.getBlock(table.ID, table.TYPE) as block 
from table
 where ID=condition;
) select ID, block.block as "BLOCK" , block.block as "MASK" from block 

The WITH clause, or subquery factoring clause, is part of the SQL-99 standard and was added into the Oracle SQL syntax in Oracle 9.2. The WITH clause may be processed as an inline view or resolved as a temporary table. The advantage of the latter is that repeated references to the subquery may be more efficient as the data is easily retrieved from the temporary table, rather than being requeried by each reference. You should assess the performance implications of the WITH clause on a case-by-case basis.

like image 132
user7294900 Avatar answered Dec 25 '25 09:12

user7294900



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!