0

I have a view that calls on four different partial views (.ascx) to populate a portion of the view via RenderAction call. Each of the partial views use the same view model, but each returns a different set of data via their own EF query in the underlying model. As you would assume from the sharing of the viewmodel, the partial views all return pretty much the same type of information -- the difference is in the filtering. E.g. "New Products" vs. "Popular Products" vs. "Recommended Products", etc.

I'm getting the data that I want, but I need to address the structure because my performance is pretty poor. The performance of each individual query doesn't seem too bad (I've used LinqPad and tested the generated SQL in SQL Server and the performance is great). However, altogether, the page load time is pretty poor as I switch categories and reload the page.

Rather than calling 4 queries against the SQL server, can I call one that pulls everything (for all 4) and then filter the results into the individual partial views? Would this be better?

Many thanks for your suggestions/advice.

Justin Johnson
  • 30,978
  • 7
  • 65
  • 89
karman
  • 141
  • 1
  • 1
  • 8

1 Answers1

0

Yes. Doing one query and filtering would be much better.

List<Widgets> widgetsFromQuery = (from w in db.Widgets 
where w.Name.EndsWith("-sprocket") || 
w.Name.EndsWith("-seat") || 
w.Name == "toaster"
select c).ToList();

Calling ToList() at the end, forces Linq to perform the query immediately.

Then you use widgetsFromQuery as your Model, and in each view, filter like this:

var filteredModel = Model.Select(w => w.Name.EndsWith("-sprocket"));
var filteredModel = Model.Select(w => w.Name.EndsWith("-seat"));
var filteredModel = Model.Select(w => w.Name == "toaster"));

Further performance enhancements would be something like:

1) Cache the output of the query in the session (if it's user specific) or application cache if not.

2) Make each view tied to an action, load with AJAX, and use output cache attributes on the actions.

Josh Pearce
  • 3,399
  • 1
  • 23
  • 24
  • Thank you for your answer. I'm not asking for sample code, but can you explain how/where I would filter the original query into the individual partial views? When I asked my original question, I was going on the premise that it be possible, but I don't know how I would go about doing something like that. As for your (1) and (2) -- thank you. Those are over my head...will need to research/read how to execute. Thanks again. – karman Mar 09 '11 at 15:01
  • karman, I've edited my answer to include an example. P.S. Caching really isn't that hard, and you should learn AJAX someday, it's great. – Josh Pearce Mar 10 '11 at 14:07