使用多个局部视图(或代替)的最佳结构是什么?
我有一个视图,它调用四个不同的部分视图 (.ascx) 以通过 RenderAction 调用填充视图的一部分。每个部分视图都使用相同的视图模型,但每个部分视图都通过底层模型中自己的 EF 查询返回一组不同的数据。正如您从视图模型共享中假设的那样,部分视图都返回几乎相同类型的信息 - 区别在于过滤。例如“新产品”与“热门产品”与“推荐产品”等。
我得到了我想要的数据,但我需要解决结构问题,因为我的性能相当差。每个单独查询的性能看起来并不算太差(我已经使用 LinqPad 并在 SQL Server 中测试了生成的 SQL,性能非常好)。然而,总的来说,当我切换类别并重新加载页面时,页面加载时间非常短。
我是否可以调用一个提取所有内容(所有 4 个)的查询,然后将结果过滤到各个部分视图中,而不是针对 SQL Server 调用 4 个查询?这样会更好吗?
非常感谢您的建议/意见。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。进行一次查询和过滤会好得多。
最后调用 ToList() 会强制 Linq 立即执行查询。
然后,您使用 widgetsFromQuery 作为您的模型,并在每个视图中,像这样进行过滤:
进一步的性能增强将类似于:
1)在会话中缓存查询的输出(如果它是用户特定的)或应用程序缓存(如果不是)。
2) 将每个视图绑定到一个操作,使用 AJAX 加载,并在操作上使用输出缓存属性。
Yes. Doing one query and filtering would be much better.
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:
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.