I have two quite large tables, let’s call them ‘authors’ and ‘articles’.
Every row in the ‘articles’ table is associated with a single author represented by the value in the ‘AuthorID’ field.
We have a page where we show a set of writers that match a certain criteria, and the first article that is associated with every single writer, ordered by the value of the ‘DatePublished’ field in the article.
This is currently achieved by executing multiple queries which turns out to be really slow especially when executed for a large set of authors. I’m not the expert on the subject but I’m pretty sure there is a way to get this data with a single statement which will be much more optimized than the current mess.
To put it in another way: (Think every function call as a single SQL query)
What we have now:
$authors = get_some_authors($criteria);
for each($author in $authors) {
$author->lastArticle = get_latest_article_by_author($author_id);
}
What we need:
$authors_with_last_articles = get_some_authors_and_their_latest_articles($criteria);
We’re using MS SQL Server 9.0.
Hope I could explain the problem correctly. Thanks in advance for your help.
You could use an
outer applyto implementfor-eachbehavior: