473,411 Members | 1,937 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,411 software developers and data experts.

select rownumber() over(), ... affecting plan very badly.

Hi,

using ROWNUMBER() is affecting the plan of a view very badly...

is there a way of writing the following view to ensure rownumber()
is done as the last thing done?
i.e after the calling predicates have been applied to a_cte

e.g
I have rewritten the view as follows using a CTE, trying to
'influence' the rownumber()to be run last, but this doesn't help?

create view
as
with a_cte(.......)
select rownumber() over(), a_cte.* from a_cte;
-- This runs in 40seconds when called with predicates on a_cte

create view
as
with a_cte(.......)
select a_cte.* from a_cte;
-- This runs in 0.03seconds when called with predicates on a_cte

Thanks.

Paul.
Nov 12 '05 #1
5 10624
"Paul Reddin" <pa**@abacus.co.uk> wrote in message
news:1f**************************@posting.google.c om...
Hi,

using ROWNUMBER() is affecting the plan of a view very badly...

is there a way of writing the following view to ensure rownumber()
is done as the last thing done?
i.e after the calling predicates have been applied to a_cte
Views don't work that way. I.e.

SELECT rownumber() over() rn, t.* FROM t WHERE col1 = 'A'

is a different query than

CREATE VIEW view AS (SELECT rownumber() over() rn, t.* FROM t );
SELECT * FROM view WHERE col1 = 'A';

as that is equivalent to this

SELECT * FROM (
SELECT rownumber() over() rn, t.* FROM t
) WHERE col1 = 'A'

Now, an inner query can reference an outer value, such as

WITH p(param1) AS (VALUES('A'))
SELECT * FROM (
SELECT rownumber() over() rn, t.* FROM t, p
WHERE col1 = param1
) v, p
WHERE col1 = param1
;

But it's no good creating a view such as

CREATE VIEW v AS (
SELECT rownumber() over() rn, t.* FROM t, p
WHERE col1 = param1
)

because p is unresolved and vies cannot have unresolved parameters (in fact,
views cannot have parameters full stop)

So your only solution, short of hoping for parameterised views (whatever
that would mean) in some future SQL standard, is a table function...

create table t (id int primary key , col1 char(1));
insert into t values(1,'A'),(2,'B'),(3,'A'),(4,'C'),(5,'B');

CREATE FUNCTION T.RN (x varchar(1))
RETURNS TABLE (rn int, id int, col1 char(1))
LANGUAGE SQL READS SQL DATA NO EXTERNAL ACTION DETERMINISTIC
RETURN
SELECT rownumber() over() rn, t.* FROM t WHERE col1 = x
;
select * from table(T.RN(CHAR('A'))) as rn
;

Regards
Paul Vernon
Business Intelligence, IBM Global Services

e.g
I have rewritten the view as follows using a CTE, trying to
'influence' the rownumber()to be run last, but this doesn't help?

create view
as
with a_cte(.......)
select rownumber() over(), a_cte.* from a_cte;
-- This runs in 40seconds when called with predicates on a_cte

create view
as
with a_cte(.......)
select a_cte.* from a_cte;
-- This runs in 0.03seconds when called with predicates on a_cte

Thanks.

Paul.

Nov 12 '05 #2
Paul,

How do use the view?
If you simply do a select * from v
there shoucln't be a problem.
But as soon as you add a predicate;
select * from v WHERE c1 = 5
This predicate which would ordinarily find it's way down to the table to
be used in a scan or index fetch cannot be pushed past teh rownumber()
function becuase you would get different results.

Now on another note, if you did NOT select the result of the rownumber()
DB2 should drop it out of the plan. If that fails to happen (the
optimized SQL in db2exfmt would show) I would see that as a problem to
look into.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
As far as i can remember, cte's are evaluated in early stage to be able to
share/reuse them later.
Where did you put the rownumber() in your 2nd example?
Are you doing some kind of recursion?

PM

"Paul Reddin" <pa**@abacus.co.uk> a écrit dans le message de
news:1f**************************@posting.google.c om...
Hi,

using ROWNUMBER() is affecting the plan of a view very badly...

is there a way of writing the following view to ensure rownumber()
is done as the last thing done?
i.e after the calling predicates have been applied to a_cte

e.g
I have rewritten the view as follows using a CTE, trying to
'influence' the rownumber()to be run last, but this doesn't help?

create view
as
with a_cte(.......)
select rownumber() over(), a_cte.* from a_cte;
-- This runs in 40seconds when called with predicates on a_cte

create view
as
with a_cte(.......)
select a_cte.* from a_cte;
-- This runs in 0.03seconds when called with predicates on a_cte

Thanks.

Paul.

Nov 12 '05 #4
Paul,

Thanks for the explanation, I don't follow it 100%, but it looks
like a familiar problem we have running across continuously with
views, that is, the predicate push down isn't always as hoped for!

As background, we are using views extensively to encapsulate Objects,
which works fine 98% of the time, but sometimes our queries/views are
painful/time consuming to tune/write correctly!

Many Thanks.

PaulR.
Nov 12 '05 #5
Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<ca**********@hanover.torolab.ibm.com>...
This predicate which would ordinarily find it's way down to the table to
be used in a scan or index fetch cannot be pushed past teh rownumber()
function becuase you would get different results.
OK, this makes sense now.
Now on another note, if you did NOT select the result of the rownumber()
DB2 should drop it out of the plan. If that fails to happen (the
optimized SQL in db2exfmt would show) I would see that as a problem to
look into.


I have tested this, and the optimiser is fine when I don't actually
select the rownumber, not much use having it in the view then though :-)

Many thanks.

Paul.
Nov 12 '05 #6

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

Similar topics

2
by: Bill Sneddon | last post by:
I am using the for-each below to build a table where the header is a unique nonblank EQUIP_TYPE. This works well. What I would like to do is not execute the for loop on on EQUIP_TYPE unless one...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
3
by: Jacinle Young | last post by:
Hi If I have a table with two colum ITEM and NAME, like the following ITEM NAME ======= ============ Apple Nancy Orange Nancy Lemon Margaret Orange ...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
6
by: florian | last post by:
Hello, we are running DB2 UDB EEE Version 7.2 Fixpack 12 on a two machine Windows 2000 Advanced Server Cluster in a dss environment. Some dynamic sql statements for etl processes and even some...
2
by: Chris Plowman | last post by:
Hi all, I was wondering if anyone can help me with a really annoying problem I have been having. I made a derived datagrid class that will select the row when a user clicks anywhere on a cell...
33
by: Peter | last post by:
People are telling me it is bad to put select * from <atable> in a view. I better should list all fields of the table inside the definition of the view. I dont know exactly why but some...
3
by: rallykarro | last post by:
Hi, How do I at the best way perform select statements over multiple databases? I have a couple of databases containing the same table definitions with diffrent data. Now I want them to act...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.