472,117 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,117 software developers and data experts.

Slow Stored Procedure - Easy located but wtf?

Hi,

Plz, I need some info (SQL2000) :)

A stored procedure is like this:

"Select table1.id, table1.txt, (select table2.nr from table2 where
table2.fk_table1=table1.id) as nr where table1.id<>10"

The essence here is that "select table2.nr from table2 where
table2.fk_table1=table1.id" returns either the integer in table2.nr, or NULL
if there isnt a match. The whole sentence runs EXTREMELY slow...3-4 sec.
What is wrong?

"select table2.nr from table2 where table2.fk_table1=table1.id" runs quickly
outside the stored procedure. The original sentence without the "nr" (Select
table1.id, table1.txt where table1.id<>10) runs quickly too...

But together it slows down dramatically..why? I should mention that the
sub-query could return NULL if theres no match in table2...But i cant see
why that should slow things down (remember - it runs fine outside the SP)?

Thx,
PipHans
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11-09-2003
Jul 20 '05 #1
3 2904
[posted and mailed, please reply in news]

PipHans (pi*****@hotmail.com) writes:
A stored procedure is like this:

"Select table1.id, table1.txt, (select table2.nr from table2 where
table2.fk_table1=table1.id) as nr where table1.id<>10"

The essence here is that "select table2.nr from table2 where
table2.fk_table1=table1.id" returns either the integer in table2.nr, or
NULL if there isnt a match. The whole sentence runs EXTREMELY slow...3-4
sec.


3-4 extremely slow? Hah! You should see some of the queries, I have
been running lately. :-) (Sorry, just couldn't resist.)

Anyway, without knowledge about your table, their indexes and how
much data they have I cannot give any good answer. You might use
the option Show Execution Plan in Query Analyzer to get an idea.

This query might give the same result. I have no idea if it will
perform any better:

Select t1.id, t1.txt, table2.nr
from table1 t1
left join table2 t2 ON t1.id = t2.fk_table1
where t1.id<>10

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Erland Sommarskog wrote:
3-4 extremely slow? Hah! You should see some of the queries, I have
been running lately. :-) (Sorry, just couldn't resist.)


:)

Well, I thought of using a left join instead as you recommend. Afaik joins
is very fast on SQL2000.
But I think its more of a problem wioth the indexes or perhaps an error
(code?) that makes SQL compile the SP every time its called....? Dunno.

I'll try with a left join tomorrow and post the result.
Thx,

--
PipHans
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.518 / Virus Database: 316 - Release Date: 11-09-2003
Jul 20 '05 #3
PipHans (pi*****@hotmail.com) writes:
Well, I thought of using a left join instead as you recommend. Afaik joins
is very fast on SQL2000.
Depends on your indexes. :-)
But I think its more of a problem wioth the indexes or perhaps an error
(code?) that makes SQL compile the SP every time its called....? Dunno.


There might be a recompilation problem, but to say anything about that
I need to see the code for the entire procedure.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by N. Shamsundar | last post: by
3 posts views Thread by Jennyfer J Barco | last post: by
12 posts views Thread by Jason Huang | last post: by
reply views Thread by leo001 | last post: by

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.