Re: Views vs Stored Procedures, whats the difference?
- From: Serge Rielau <srielau@xxxxxxxxxx>
- Date: Sat, 04 Feb 2006 16:32:58 -0500
Tony Rogerson wrote:
OK.. perhaps we violently agree? You are hung up on embedded SQL. I'm not debating that point at all.One does not exclude the other. You can use procedures as external interface for the application and still use views within the procedures as appropriate.
Absolutely, so long as you are using stored procedures as the external access mechanism then that is a good thing.
No I'm not. Each their paradigm. I'm just trying to differentiate between two fundamentally different SQL objects.
I hope you are not suggesting you embed SQL queries into the application?
<snip>
These two, asside from an unmeasurable amount of CPU time in plan compilation will give the same performance...So the CALL is free? No doubt it's highly optimized, but it won't be free.
You are looking only at one end of the picture.
SELECT ... FROM yourview
Or..
CREATE PROC yourproc
AS
BEGIN
SELECT .... FROM yourview
END
Let's focus on the scenario where the select is NOT in the application, but it is inside of a nested proecdure.
Let's use the following SQL (never mind if I mess up the dialect.. that's not the point):
CREATE TABLE P(pk INT NOT NULL PRIMARY KEY, C1 INT)
CREATE TABLE C(pk INT NOT NULL PRIMARY KEY,
fk INT FOREIGN KEY T(pk), C2 INT)
CREATE PROCEDURE nestedproc(IN arg)
BEGIN
SELECT c1, c2 FROM P, C WHERE P.pk = C.fk AND P.pk = arg;
END
CREATE VIEW nestedview AS SELECT c1, c2 FROM P, C WHERE P.pk = C.fk;
Now let's consider the following:
"What is the maximum value c2 for a given arg?"
Using the procedure you CALL the procedure with the argument and you then have to process the resultset (with swoem sort of logic unless I missed some T-SQL feature) to find the MAX.
Using the view I simply write:
SELECT MAX(c2) FROM nestedview
WHERE pk = arg
What's the difference? An SQL optimizer can clearly see that there is nothing interesting in P. It will DROP access to table P altogether because first it projects out c1, then it kicks out P using the foreign key relationship to prove that the join is row preserving.
Even better, with the right index (fk, c2 desc) this whole thing will be done in a single I/O.
Using the procedure you have broken this vital link between the consumer of the data and the data access. The DBMS has no clue that c1 will never be consumed because the procedure is written generically for users who want to retrieve c1 and c2.
Now, even if your DBMS were not to do this optimization today. Chances are it will next time you upgrade; the DBMS will show why you paid more for the new release than the old one.
The problem lies NOT in the application. The problem lies in the fact that procedures are being nested and important context is being lost.
The smart developer considers such things as scalability, maintainability and supportability, they do not try to apply 80's programming models where most products didn't have the concept of stored procedures, I remember - I used to program PL/1, CICS and DB2 on the mainframe that way.You are older than me... anyway being too heavy on procedures is poison for scalability. When I teach customers I teach that both logic and SQL have their place. What I call "trivial procedures" are my prime example to explain what I mean.
Cheers
Serge
PS: Check out table functions (aka parameterized views).
--
Serge Rielau
DB2 Solutions Development
DB2 UDB for Linux, Unix, Windows
IBM Toronto Lab
.
- Follow-Ups:
- Re: Views vs Stored Procedures, whats the difference?
- From: Doug
- Re: Views vs Stored Procedures, whats the difference?
- From: Tony Rogerson
- Re: Views vs Stored Procedures, whats the difference?
- References:
- Re: Views vs Stored Procedures, whats the difference?
- From: --CELKO--
- Re: Views vs Stored Procedures, whats the difference?
- From: Tony Rogerson
- Re: Views vs Stored Procedures, whats the difference?
- From: Serge Rielau
- Re: Views vs Stored Procedures, whats the difference?
- From: Tony Rogerson
- Re: Views vs Stored Procedures, whats the difference?
- Prev by Date: Re: ORDER BY in VIEW not working
- Next by Date: Re: can you tell me why this causes a deadlock?
- Previous by thread: Re: Views vs Stored Procedures, whats the difference?
- Next by thread: Re: Views vs Stored Procedures, whats the difference?
- Index(es):
Relevant Pages
|