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 2012 Forums
 Transact-SQL (2012)
 Fastest way to select rows using the primary keys

Author  Topic 

sth_Weird
Starting Member

38 Posts

Posted - 2014-09-15 : 04:31:33
hi,
I am looking for the best way to select rows from a table, using primary keys.
This is what I have:
Table "A" has about 1000 entries, one column contains a foreign key (unique) that points to an entry in another table "B". Table "B" has a lot of rows, let's say 100000+, up to 500000. I want to select all rows from table "B" that are refered to in table "A". Now, the number of entries in table "B" is a lot larger than the 1000 entries in table "A". Does that matter when it comes to picking the best sql query to select the rows? Should I use a table join, or use SELECT * FROM B WHERE PrimaryKey in (SELECT ForeignKey FROM A)? Or maybe even use WHERE EXISTS (...)? By the way I only need the columns of table "B" in my result.

thx
sth_Weird

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-09-15 : 08:07:33
Just join and select the resulting columns that come from b
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-09-15 : 08:35:20
Do you have a referential constraint? If so, use join.
If not, consider EXISTS.



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Lokanatha
Starting Member

1 Post

Posted - 2014-09-17 : 21:53:49
Better use Joins for more details on [url]<a href="http://skillgun.com/sql/interview-questions-and-answers">SQL Questions</a>[/url] in http://skillgun.com
Go to Top of Page

sth_Weird
Starting Member

38 Posts

Posted - 2014-09-26 : 05:57:12
thank you!
Go to Top of Page
   

- Advertisement -