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

Pattern to ensure executing ONLY required SQL statements ?

Hi,

We have a whole set of Views we would like to structure a follows, but
they don't seem to optimise in the way we would like:

e.g

CREATE VIEW1 (col1,col2,col3....)
as
(
(SELECT col1 from tab1 where tab1.col1 = XYZ.col1),
(SELECT col1 from tab2 where tab2.col1 = XYZ.col1),
(SELECT col1 from tab3 where tab3.col1 = XYZ.col1), ...
FROM
XYZ
)
Now if we simply select ONLY col1 column from the view
e.g
SELECT col1 from VIEW1...

I would expect only the first SELECT in the view to be executed, but
instead
the view calculates all of the statements ??
- NB. the SELECTS are fairly heavyweight so we really don't want to
have to execute them unnecessarily.

Is there a way to structure this so only the required selects are
executed?

UNION or TABLE FUNCTION perhaps?

Many Thanks

Paul.
Nov 12 '05 #1
3 1371
Hi,

I think I've maybe realised my own explanation for this, after thinking about it
a little more...

I don't think the optimiser can ignore the 2nd an 3rd selects , as of course they
will affect the cardinality of col1.

Maybe, if we performed a
SELECT DISTINCT col1 FROM view1 ...

The other selects would then be redundant and not be performed.

Ultimately, where we are trying to go with this also, is to be able to perform a
COALESCE on statements where we only execute those statements up to
the point of the first NON NULL result.

i.e COALESCE ( (select1...), (select2 ....) ,(select3...)) always executes all
statements even if select1 is NON NULL.

Does this make sense sound a familiar problem?

Thanks.

Paul
Paul.

pa**@abacus.co.uk (Paul Reddin) wrote in message news:<1f**************************@posting.google. com>...
Hi,

We have a whole set of Views we would like to structure a follows, but
they don't seem to optimise in the way we would like:

e.g

CREATE VIEW1 (col1,col2,col3....)
as
(
(SELECT col1 from tab1 where tab1.col1 = XYZ.col1),
(SELECT col1 from tab2 where tab2.col1 = XYZ.col1),
(SELECT col1 from tab3 where tab3.col1 = XYZ.col1), ...
FROM
XYZ
)
Now if we simply select ONLY col1 column from the view
e.g
SELECT col1 from VIEW1...

I would expect only the first SELECT in the view to be executed, but
instead
the view calculates all of the statements ??
- NB. the SELECTS are fairly heavyweight so we really don't want to
have to execute them unnecessarily.

Is there a way to structure this so only the required selects are
executed?

UNION or TABLE FUNCTION perhaps?

Many Thanks

Paul.

Nov 12 '05 #2
AK
Paul,
I don't think the optimiser can ignore the 2nd an 3rd selects , as of course they
will affect the cardinality of col1.


I would disagree, the subselect won't affect the cardinality at all,
The cardinality is gonna be 1 value from a subselect per a result set row.

Should you ever get more thatn 1 row in a subselect, you'd get an error
Nov 12 '05 #3
Paul,

Which version and platform are you on? DB2 for Multiplatforms has the
capability to drop unreferenced scalar subqueries.
I recall to have added some further improvements to it in FP4.
You may want to try that. If that doesn't work I'd love to see a full
repro-script.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #4

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

Similar topics

2
by: Tony Marston | last post by:
For those who you thought my method of implementing OO principles in PHP was totally wrong - see http://www.tonymarston.co.uk/php-mysql/good-bad-oop.html for details, you can now read...
13
by: Brian | last post by:
Hi all... This question is more for the GURUs out there. It is not a question on how to do something, but why it happens, and I am trying to figure out if there is a pattern. I am using IE, but...
6
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
10
by: richard.markiewicz | last post by:
Hi Gurus, Working with a .NET 2 C# web application. Using System.DirectoryServices a lot to read information from AD. There seemed to be several bugs in the .NET 1.1 implementation of that DLL...
8
by: Chris Forone | last post by:
hello group, is there a possibility to implement the decorator-pattern without new/delete (nor smartpt)? if not, how to ensure correct deletion of the objects? thanks & hand, chris
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.