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 2005 Forums
 Transact-SQL (2005)
 Query using a large list

Author  Topic 

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2009-12-04 : 14:38:15
I have a table that has around 30 million records in it, the field is an 18 character char field. My users would like to ability to query up to 500 items against this list. Trying to figure out the best way query such a large # of items against such a large table without losing performance either server or user side. Any help is appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-04 : 14:39:44
Make sure it is indexed.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2009-12-04 : 14:41:11
Thanks.....any examples how how to loop through that? I was thinking this should probably be a stored procedure?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-04 : 14:47:42
SELECT TOP 500 SomeField
FROM SomeTable
WHERE...
ORDER BY ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2009-12-04 : 15:51:57
Ok....I didn't explain the issue correctly.....

My users have around 500 items in a list and now they query each item against the table one at a time which is very time consuming. They want to be able to take the entire list of 500 items and see which items exist in the table of 30 million records using one query. Basically instead of performing the query 500 times they want to query once.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-04 : 15:53:58
You're going to need to CSV the list together and use IN. It probably won't be efficient though. Sometimes the business requirements just mean you'll have a slow query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

ddobbertin
Yak Posting Veteran

51 Posts

Posted - 2009-12-04 : 16:03:33
I've used IN many times before....however never with a CSV list? Could you help out with an example? I appreciate all the help!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-07 : 03:09:35
quote:
Originally posted by ddobbertin

I've used IN many times before....however never with a CSV list? Could you help out with an example? I appreciate all the help!


Make sure you read all the options specified
http://www.sommarskog.se/arrays-in-sql-2005.html

Madhivanan

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

- Advertisement -