Home All Groups Group Topic Archive Search About

Select from a diffrent database in a Stored procedure.

Author
7 Sep 2006 12:11 PM
Karsten Lundsgaard
Hi I have a Stored Procedure in Database DB_A, where i'm getting data from
Database DB_B, both on the same SQL 2000 server S. Like.
   Select *
   from
    DB_B.dbo.Table_A A,
    DB_B.dbo.Table_B B
   Where
      A.A_Id = B.A_Id

And it works fine, but it is very slow, compared to running the SQL on the
DB_B.

What can I do to improve the speed?
I have tried with triggers ect.

Regards

Karsten Lundsgaard

Author
7 Sep 2006 12:49 PM
Aaron Bertrand [SQL Server MVP]
Accessing a different database should not be any different performance-wise
from having both objects in the same database.

Have you compared the execution plans of the two queries?

Are there indexes on A_Id and B_Id?

Have you considered using proper joins instead of this deprecated variety,
and not using SELECT *?

A


Show quote
"Karsten Lundsgaard" <KarstenLundsga***@discussions.microsoft.com> wrote in
message news:A125B4DA-B293-41C4-9329-8846FE6AF85B@microsoft.com...
> Hi I have a Stored Procedure in Database DB_A, where i'm getting data from
> Database DB_B, both on the same SQL 2000 server S. Like.
>   Select *
>   from
> DB_B.dbo.Table_A A,
> DB_B.dbo.Table_B B
>   Where
>      A.A_Id = B.A_Id
>
> And it works fine, but it is very slow, compared to running the SQL on the
> DB_B.
>
> What can I do to improve the speed?
> I have tried with triggers ect.
>
> Regards
>
> Karsten Lundsgaard
>
>
>
>
>
Author
7 Sep 2006 1:02 PM
Tracy McKibben
Karsten Lundsgaard wrote:
Show quote
> Hi I have a Stored Procedure in Database DB_A, where i'm getting data from
> Database DB_B, both on the same SQL 2000 server S. Like.
>    Select *
>    from
>     DB_B.dbo.Table_A A,
>     DB_B.dbo.Table_B B
>    Where
>       A.A_Id = B.A_Id
>
> And it works fine, but it is very slow, compared to running the SQL on the
> DB_B.
>
> What can I do to improve the speed?
> I have tried with triggers ect.
>
> Regards
>
> Karsten Lundsgaard
>
>
>
>
>

There should be no performance difference cause by a cross-database
query on the same server.  Look at the execution plan, see where the
bottleneck is.


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
7 Sep 2006 1:04 PM
John Bell
Hi

You should not use * in production code, only specify the columns you
require. Try:

SELECT A.A_Id, B.A_Id
FROM DB_B.dbo.Table_A A,
JOIN DB_B.dbo.Table_B B ON A.A_Id = B.A_Id

You could also create the procedure on database B and call it

USE DB_B
GO

CREATE PROCEDURE ProcB
AS
SELECT A.A_Id, B.A_Id
FROM dbo.Table_A A,
JOIN dbo.Table_B B ON A.A_Id = B.A_Id
GO
USE DB_A
GO
EXEC DB_B.dbo.ProcB


John

Show quote
"Karsten Lundsgaard" <KarstenLundsga***@discussions.microsoft.com> wrote in
message news:A125B4DA-B293-41C4-9329-8846FE6AF85B@microsoft.com...
> Hi I have a Stored Procedure in Database DB_A, where i'm getting data from
> Database DB_B, both on the same SQL 2000 server S. Like.
>   Select *
>   from
> DB_B.dbo.Table_A A,
> DB_B.dbo.Table_B B
>   Where
>      A.A_Id = B.A_Id
>
> And it works fine, but it is very slow, compared to running the SQL on the
> DB_B.
>
> What can I do to improve the speed?
> I have tried with triggers ect.
>
> Regards
>
> Karsten Lundsgaard
>
>
>
>
>
Author
8 Sep 2006 6:57 AM
Karsten Lundsgaard
Thanks for the answers Aaron, Tracy and John.

I know that I shouldn't use "Select *", the SQL-statement is just an
example, the real SQL-statement is complex.

John, I cannot use the "Use DB_B" in a Stored Procedure.

Thanks for your help, I will study the ExecutionsPlans some more.

Karsten Lundsgaard.
Author
8 Sep 2006 9:43 AM
John Bell
Hi

The USE statement was because he procedures were being created in one
database and to run it you had to be in the other, you would not need it in
your actual procedure.

John

Show quote
"Karsten Lundsgaard" <KarstenLundsga***@discussions.microsoft.com> wrote in
message news:3F3581A5-B7F9-4487-893E-05182AB6F396@microsoft.com...
> Thanks for the answers Aaron, Tracy and John.
>
> I know that I shouldn't use "Select *", the SQL-statement is just an
> example, the real SQL-statement is complex.
>
> John, I cannot use the "Use DB_B" in a Stored Procedure.
>
> Thanks for your help, I will study the ExecutionsPlans some more.
>
> Karsten Lundsgaard.

AddThis Social Bookmark Button