Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 limiting the results of the first table - TRIVIAL?

Author  Topic 

sdiwi
Starting Member

27 Posts

Posted - 2005-09-02 : 08:52:19
hi there.

i ran into this problem for several times now, and until now i always found an acceptable workaround or didnt care for the speed loss...

let's assume following query:

SELECT a.id, a.name, b.id
FROM tablea a
LEFT JOIN tableb b ON
(......)

got it? ok. let's go on...


now i don't need most of the rows of tablea. because tablea really has a huge amount of data stored the query is kinda costly and slow.
however limiting after the left join does not really improve things.
what i need to do, is to limit the results of tablea BEFORE joining left.
something like this would be fine for me:

SELECT a.id, a.name, b.id
FROM tablea a
WHERE a.id NOT IN (....)
LEFT JOIN tableb b ON
(......)

Of course this does not work. As this is anyway code inside of an udf, i could use a tablevar or a temp table and initialize it with the rows i need from tablea.

but this is clumsy. ain't there an easier way?


thanks for your help,
peace,

sdiwi.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-02 : 08:58:38
Not sure whether this helps you


SELECT a.id, a.name, b.id
FROM (Select top 100 * from tableA) a
LEFT JOIN tableb b ON
(......)

Otherwise post some sample data and the result you want

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-09-02 : 09:08:24
Put condition in a where clause that limits rows in table a and have usefull index in plays. Also condition should be:

<table a column> <operator> <expression>

where <operator> is in (<, <=, >, >=, =)...

Have statistics updated and sometimes force execution plan.
Go to Top of Page

sdiwi
Starting Member

27 Posts

Posted - 2005-09-02 : 09:17:37
@madhivanan
omg. ya. this does help me...
ouch....

dam. why didn't i try that one?
thank you.

i think that was too obvious. i didn't really search for that simple solutions :P...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-09-02 : 09:27:57
But make sure you get proper result (most desired result) at end
You may use tableA Order by to get relevent data

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -