473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Function used in where clause - executed too many times

HI,
I have a function that is used to constrain a query:

Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
And MyFunction(col1) = ...

My problem is that MyFunction is executed as many times that there are
rows in MyTable. I would like that it is being eecuted only when the
MyTable data has been filtered by two previous where conditions. This
way, MyFunction would be executed minimal times. So I did this:

Select a.*
From (Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...)
Where MyFunction(a.col1) =...
With no success. MyFunction is executed as many times as there are
rows into MyTable.

Is There a way to ensure that a function is being executed at the end
of the where clause, when the data is filtered by previous conditions
as much as possible?

Thank you for your help,
Christian
Jul 19 '05 #1
6 14649
cc*******@yahoo.com (Christian) wrote in message news:<99**************************@posting.google. com>...
HI,
I have a function that is used to constrain a query:

Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
And MyFunction(col1) = ...

My problem is that MyFunction is executed as many times that there are
rows in MyTable. I would like that it is being eecuted only when the
MyTable data has been filtered by two previous where conditions. This
way, MyFunction would be executed minimal times. So I did this:

Select a.*
From (Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...)
Where MyFunction(a.col1) =...
With no success. MyFunction is executed as many times as there are
rows into MyTable.

Is There a way to ensure that a function is being executed at the end
of the where clause, when the data is filtered by previous conditions
as much as possible?

Thank you for your help,
Christian


There is not. In Oracle all predicates will always be evaluated.
The only 'solution' is to use Function Based Indexes (Enterprise Edition required).
The best advice is to avoid functions with embedded selects in them like hell.

Sybrand Bakker,

Senior Oracle DBA
Jul 19 '05 #2
cc*******@yahoo.com (Christian) wrote in message news:<99**************************@posting.google. com>...
HI,
I have a function that is used to constrain a query:

Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
And MyFunction(col1) = ...

My problem is that MyFunction is executed as many times that there are
rows in MyTable. I would like that it is being eecuted only when the
MyTable data has been filtered by two previous where conditions. This
way, MyFunction would be executed minimal times. So I did this:

Select a.*
From (Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...)
Where MyFunction(a.col1) =...
With no success. MyFunction is executed as many times as there are
rows into MyTable.

Is There a way to ensure that a function is being executed at the end
of the where clause, when the data is filtered by previous conditions
as much as possible?

Thank you for your help,
Christian

Hi Christian,
Try this way..

Select a.*
From MyTable
Where (COl1,Col2) In (Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
)
And MyFunction(a.col1) =...

I think it help you.

Thanks and Regards
Khurram Naseem
Jul 19 '05 #3

<sy******@yahoo.com> wrote in message
news:a1**************************@posting.google.c om...
| cc*******@yahoo.com (Christian) wrote in message
news:<99**************************@posting.google. com>...
| > HI,
| > I have a function that is used to constrain a query:
| >
| > Select COl1, Col2
| > From MyTable
| > WHERE col1 = ...
| > AND col2 = ...
| > And MyFunction(col1) = ...
| >
| > My problem is that MyFunction is executed as many times that there are
| > rows in MyTable. I would like that it is being eecuted only when the
| > MyTable data has been filtered by two previous where conditions. This
| > way, MyFunction would be executed minimal times. So I did this:
| >
| > Select a.*
| > From (Select COl1, Col2
| > From MyTable
| > WHERE col1 = ...
| > AND col2 = ...)
| > Where MyFunction(a.col1) =...
| > With no success. MyFunction is executed as many times as there are
| > rows into MyTable.
| >
| > Is There a way to ensure that a function is being executed at the end
| > of the where clause, when the data is filtered by previous conditions
| > as much as possible?
| >
| > Thank you for your help,
| > Christian
|
| There is not. In Oracle all predicates will always be evaluated.
| The only 'solution' is to use Function Based Indexes (Enterprise Edition
required).
| The best advice is to avoid functions with embedded selects in them like
hell.
|
| Sybrand Bakker,
|
| Senior Oracle DBA
not that simple

in 8.1.7.0.0, for this query:

select ename
from emp
where deptno = 20
and fnc(ename,'test5') = 'TRUE'

the function generally got executed once per employee in department 20, not
once per row in the table -- but as long as i had and index on deptno, or
had statistics on the table

with no index on deptno and no statistics, the function got evaluated for
each row -- unless i reversed the order of the predicates, then the
optimizer only executed the function for each deptno 20 row

it also appears that the ORDERED_PREDICATES hint works to force evaluation
of predicates in WHERE-clause order, thus preventing extra comparisons
(there are limitations -- see the docs)

btw: a function based index is only good advise if the index is going to be
selective enough (bad idea to create an index that is never used, just adds
DML overhead)

i would suggest posting version info and the explain plan. you've got some
work to do with indexes or statistics or hints

;-{ mcs
Jul 19 '05 #4
cc*******@yahoo.com (Christian) wrote in message news:<99**************************@posting.google. com>...
HI,
I have a function that is used to constrain a query:

Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
And MyFunction(col1) = ...

My problem is that MyFunction is executed as many times that there are
rows in MyTable. I would like that it is being eecuted only when the
MyTable data has been filtered by two previous where conditions. [] Is There a way to ensure that a function is being executed at the end
of the where clause, when the data is filtered by previous conditions
as much as possible?

Thank you for your help,
Christian

The real question is why do you care?
What side effect are you trying to avoid?
There is always a side effect, even if it is only the response time of the query.
Jul 19 '05 #5
dx
cc*******@yahoo.com (Christian) wrote in message news:<99**************************@posting.google. com>...
HI,
I have a function that is used to constrain a query:

Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
And MyFunction(col1) = ...

Suppose the original query is:

select col1, col2
from mytable
where col1 = 'abc' and col2 = 'def' and myfunction(col1) = 'xyz';

Suppose no appropriate index on col1 and/or col2 can be used, you can
use decode to filter like this:

select col1, col2
from mytable
where decode(col1, 'abc', decode(col2, 'def', myfunction(col1), null),
null) = 'xyz';

My problem is that MyFunction is executed as many times that there are
rows in MyTable. I would like that it is being eecuted only when the
MyTable data has been filtered by two previous where conditions. This
way, MyFunction would be executed minimal times. So I did this:

Select a.*
From (Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...)
Where MyFunction(a.col1) =...
With no success. MyFunction is executed as many times as there are
rows into MyTable.

Is There a way to ensure that a function is being executed at the end
of the where clause, when the data is filtered by previous conditions
as much as possible?

Thank you for your help,
Christian

Jul 19 '05 #6
cc*******@yahoo.com (Christian) wrote in message news:<99**************************@posting.google. com>...
HI,
I have a function that is used to constrain a query:

Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...
And MyFunction(col1) = ...

My problem is that MyFunction is executed as many times that there are
rows in MyTable. I would like that it is being eecuted only when the
MyTable data has been filtered by two previous where conditions. This
way, MyFunction would be executed minimal times. So I did this:

Select a.*
From (Select COl1, Col2
From MyTable
WHERE col1 = ...
AND col2 = ...)
Where MyFunction(a.col1) =...
With no success. MyFunction is executed as many times as there are
rows into MyTable.

Is There a way to ensure that a function is being executed at the end
of the where clause, when the data is filtered by previous conditions
as much as possible?

Thank you for your help,
Christian


Christian,

In 10.1.0.2 on WinXP Pro, the function is only called for the number of rows
that pass filtering on the two prior predicates.

To demonstrate this, I shall the method outlined by Thomas Kyte in
"Effective Oracle by Design" (Oracle Press:2003) on p. 493. This method
uses the CLIENT_INFO functionality.

First, I create some test data with just two (2) columns. This creates a
table with 39,196 rows:
CREATE TABLE mytable AS
SELECT
MOD( rownum, 100 ) AS col1,
SUBSTR( object_name, 1, 1 ) AS col2
FROM
all_objects
;

Second, I coded the following function to record how many times this
function is called by incrementing the value in the CLIENT_INFO variable
(Yes I know I am relying on implicit type conversions!). The function
simply returns the parameter unchanged:

CREATE OR REPLACE FUNCTION myfunction
( p_col1 IN NUMBER )
RETURN NUMBER
AS
BEGIN
dbms_application_info.set_client_info(
USERENV( 'client_info' ) + 1
);
RETURN p_col1;
END;
/

Third, I count how many rows match the first two predicates:

SELECT
COUNT(*) match_count
FROM
mytable
WHERE
col1 >= 10
AND
col2 = 'j'
/

MATCH_COUNT
-----------
3056

Fourth, I run the whole query after setting the CLIENT_INFO variable to '0'.

exec dbms_application_info.set_client_info(0)

SELECT
COUNT(*) final_count
FROM
mytable
WHERE
col1 >= 10
AND
col2 = 'j'
AND
myfunction( col1 ) = 10
/

FINAL_COUNT
-----------
34

Lastly, I find how many times the function was called:

SELECT USERENV( 'client_info' ) num_calls FROM dual;

NUM_CALLS
----------------------------------------------------------------
3056

This count matches the count from the query using the first two predicates.
This is what you want.

Douglas Hawthorne
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johm | last post by:
In my update clause using the NZ function, i would like to add a condition to exclude substracting from a Null.I do not want the NULL items to be included in the UPDATE, and then i will have to ...
1
by: Peter Alberer | last post by:
Hi there, i have a problem with a query that uses the result of a plsql function In the where clause: SELECT assignments.assignment_id, assignments.package_id AS package_id,...
2
by: Ajitha Devi | last post by:
My record set will look like this ENAme -Part Time- Dept qwe - Y - 10 fjj - N - 10 ghsd - N - 10 How to count using...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
5
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
2
by: vinayakkale2 | last post by:
Hi I am using a select query from front end to generate report in such a way that I should be able to get records of Table W which are present in Table Y The query which i am using is as follows: ...
1
by: ashishtech | last post by:
In the following query D_CNT_LINE_EXPIRE comparison with '2008-09-19' is failing altough value is there in this field. D_CNT_LINE_EXPIRE is a timestamp field in SQLSERVER table CCG03. I doubt...
5
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
4
by: George Allen | last post by:
I have been experimenting wiht using the Listbox Callback function to solve an issue I have with my db tool. The function below connects ADO to a SQL server database and table and reads those...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.