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. 5 10541
"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.
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
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 =...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
| | |