473,692 Members | 2,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10639
"Paul Reddin" <pa**@abacus.co .uk> wrote in message
news:1f******** *************** ***@posting.goo gle.com...
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.goo gle.com...
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.e ye-be-em.com> wrote in message news:<ca******* ***@hanover.tor olab.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
2344
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 of the AUTOMATED tags equals YES. I can change the data so that each row whould contain a tag that shows if the EQUIP_TYPE has an AUTOMATED member but that seems wasteful.
17
5015
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 cust_no, ded_type_cd, chk_no)
3
7283
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 Margaret
12
7692
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 million rows and if I do a select * then I do get 1 million rows. I want to be able to provide page navigation as google does, page
6
3529
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 for online user queries switched overnight from some minutes runtime to a few hours or "never come back" statements. We didn't change a single database or instance parameter. Even the
2
9921
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 (multi-select without modifier keys). I got that working fine, but I also wanted to keep rows selected after a sort, which I do by storing the row's id in an arraylist. The idea was to do the sort and then go back and re-select the rows with that...
33
6652
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 say: A select * from makes sql server does a table scan.
3
8693
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 as a single database giving me one answer on a select statement but the answer fetched from all my defined databases.
11
4500
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 rows inserted/sec up to 500, and 100rows/6secs up to 3000, which isn't great but then the Row Count is clicking away for the user to see and they can hit the "cancel" button at anytime, so overall I'm happy), what really disappoints me is the...
0
8611
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8547
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9090
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8812
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6462
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5822
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2984
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.