减少往返次数 - linq-to-sql

发布于 2024-10-17 00:23:22 字数 1917 浏览 2 评论 0原文

对于以下特定情况和一般情况,是否可以减少在执行 ASP.NET MVC 2 操作期间到数据库的往返次数? 我正在使用 linq-to-sql。 以下代码会产生类似 60 个选择的结果,这需要 60 次往返数据库。如何减少往返次数?

如果需要更多我的代码,我会发布它。

我的视图模型:

public class ArticlesContainerViewModel
{
    public ArticlesContainerViewModel()
    {
    }

    public Article CategoryArticle { get; set; }

    public IList<ArticlesNode> CategoryNodes { get; set; }
}

public class ArticlesNode
{
    public Article NodeArticle { get; set; }

    public IQueryable<Article> NodeItems { get; set; }
}

视图:

<ul class="tabs">
        <% foreach (var category in Model)
           { %>
        <li><a href="#" class="s">
            <%= Html.Encode(category.CategoryArticle.AbbreviatedTitle) %></a></li>
        <% } %>
    </ul>
    <!-- tab "panes" -->
    <div class="panes">
        <% foreach (var category in Model)
           {
        %>
        <div>
            <table style="width: 100%;" cellpadding="0" cellspacing="0">
                <% int counter = 0; foreach (var node in category.CategoryNodes)
                   {
                %>
                <%if (counter % 2 == 0)
                  { %>
                <tr>
                    <%} %>
                    <td class="content-container">
                        <div class="index-node-title">
                            <%= Html.Encode(node.NodeArticle.ArticleTitle)%>&nbsp;<span class="small1 darkGrey2">(<%= Html.Encode(node.NodeItems.Count().ToString())%>)</span>
                        </div>
                        <div class="index-node-content">
                            <ul class="index-node-content-list">
                                <% foreach (var item in node.NodeItems.Take(2))

Is it possible to reduce the number of round trips to the database during the execution of an asp.net mvc 2 action for the following specific case and in general?
I'm using linq-to-sql.
The following code results in something like 60 selects, which take 60 round trips to the database. How can I reduce the number of round trips ?

If more of my code is needed, I will post it.

My view model :

public class ArticlesContainerViewModel
{
    public ArticlesContainerViewModel()
    {
    }

    public Article CategoryArticle { get; set; }

    public IList<ArticlesNode> CategoryNodes { get; set; }
}

public class ArticlesNode
{
    public Article NodeArticle { get; set; }

    public IQueryable<Article> NodeItems { get; set; }
}

The view:

<ul class="tabs">
        <% foreach (var category in Model)
           { %>
        <li><a href="#" class="s">
            <%= Html.Encode(category.CategoryArticle.AbbreviatedTitle) %></a></li>
        <% } %>
    </ul>
    <!-- tab "panes" -->
    <div class="panes">
        <% foreach (var category in Model)
           {
        %>
        <div>
            <table style="width: 100%;" cellpadding="0" cellspacing="0">
                <% int counter = 0; foreach (var node in category.CategoryNodes)
                   {
                %>
                <%if (counter % 2 == 0)
                  { %>
                <tr>
                    <%} %>
                    <td class="content-container">
                        <div class="index-node-title">
                            <%= Html.Encode(node.NodeArticle.ArticleTitle)%> <span class="small1 darkGrey2">(<%= Html.Encode(node.NodeItems.Count().ToString())%>)</span>
                        </div>
                        <div class="index-node-content">
                            <ul class="index-node-content-list">
                                <% foreach (var item in node.NodeItems.Take(2))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

季末如歌 2024-10-24 00:23:22

如果你问我,代码有点混乱,我猜 Model 的类型是 IList 类型或类似的类型。您的问题是您在应用程序中使用 IQueryable 这么“高”。我想要的结构是:

1. First select all categories and put that in your `ViewModel` using a single query. 
2. From all your categories select all your articles or what else from the db in a single query, put the result in a dictionary where category is the key.

这样您应该只能获得两个查询,而不是像现在这样的 m*(n+1)

The code is a little bit messy if you ask me, and I guess that the type of Model is of type IList<ArticlesContainerViewModel> or something similar. Your problem is that you are using IQueryable this "high" up in the application. The structure I would go for is:

1. First select all categories and put that in your `ViewModel` using a single query. 
2. From all your categories select all your articles or what else from the db in a single query, put the result in a dictionary where category is the key.

That way you should be able to get only two queries instead of m*(n+1) as you are having now.

已下线请稍等 2024-10-24 00:23:22

看看 PLINQO。它将优化您的查询。 www.plinqo.com

Take a look at PLINQO. It will optimize your queries. www.plinqo.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文