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)
 Speed in Stored Procedure

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-04-29 : 10:06:44
Hello,

I have two tables: Articles and Comments

I need to Select some articles (no more than 20) and for each article I need to select its Comments.

I am populating a .Net class with the data.

As far as I can see, I can do this in two ways:

1. Use Inner Join in my SQL stored procedure (This is how I have it now)

2. Load the 20 articles from my .NET code and then for each Article and given the ArticleId execute the procedure "GetCommentsForArticle" which would make me execute this procedure a maximum of 20 times.

(1) is probably faster. But (2) is easier in terms of my .NET code.

My question is, for 20 records will I have a huge difference in time?

The reason why I am asking this is because of the upcoming LINQ.
In LINQ I will be able to create the Queries (including Joins) from my VB.NET / C# code.

So if I do this using (2) in a few months I will easily be able to adapt my code to LINQ. But if I use (1) it will take much more time.

Could someone advice me on the time difference of both approachs?

Thanks,
Miguel

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-04-29 : 10:38:07
well 1 will be faster.

LINQ: queries in the C# yes but you'll still have to access the db 20 times or 1 time based on your programming.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-04-29 : 12:40:37
How about:

1) Get IDs of Articles into Temporary table
2) Return Articles rows matching the IDs
3) Return Comments matching the Article IDs - order by Article.

Then presumably your Application can display the data from each of the two result sets?

Kristen
Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2007-04-29 : 18:11:57
Hi Kristen,

I am a little bit lost about your suggestion.
My problem here is to populate my classes and make this work well with my Stored Procedure.

As tables go, for each article I can have many comments.

So when I call 20 articles I would like to retrieve all comments for each article.

Can you point me to some examples of this?
Is something like this or what you suggested is usually done?

As far as .NET classes this would be the best way to do so.

Thanks,
Miguel


Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-04-30 : 04:12:38
What I am proposing is that your Stored Procedure returns two resultsets:

1) All the articles [matching whatever criteria are appropriate]

2) All the comments on those articles, sorted by Article ID / date / whatever.

So you can store all the articles in a Class. Then loop round the second Result Set storing all the comments.

This sound to me to be very similar to getting all the Articles, and then calling SQL for each article in turn to get the comments - which would be very expensive in terms of the handshaking and round-trips etc.

Kristen
Go to Top of Page

jshepler
Yak Posting Veteran

60 Posts

Posted - 2007-04-30 : 14:20:32
Here's an example of what Kristen is talking about

DECLARE @idTable TABLE (ID int)
INSERT INTO @idTable
SELECT ArticleID
FROM Articles
WHERE <whatever criteria you have>

-- recordset 1 (dataSet.Tables[0]) lists all the articles
SELECT a.ArticleID, a.Headline, a.Body
FROM @idTable t
INNER JOIN Articles a ON t.ID = a.ArticleID

-- recordset 2 (dataSet.Tables[1]) lists all the comments for the above articles
SELECT c.ArticleID, c.CommentID, c.CommentText
FROM @idTable t
INNER JOIN Comments c ON t.ID = c.ArticleID

in your .net code, use a dataview on the 2nd recordset (dataSet.Tables[1]) where you set the .RowFilter property for each article ID

DataView dv = new DataView(dataSet.Tables[1]);

foreach(DataRow row in dataSet.Tables[0].Rows)
{
article = new Article();
article.ArticleID = (int)row["ArticleID"];
article.Headline = (string)row["Headline"];
article.Body = (string)row["Body"];

dv.RowFilter = string.Format("ArticleID = {0}", article.ArticleID);
foreach(DataRowView comment in dv)
article.Comments.Add(new Comment((int)comment["CommentID"], (string)comment["CommentText"]));

listOfArticles.Add(article);
}



/jeff
Go to Top of Page
   

- Advertisement -