繁体   English   中英

优化负载和搜索速度ASP.NET C#

[英]Optimizing load and search speeds ASP.NET C#

我是ASP.NET的新手,但是经过一些基础知识的努力,感谢谷歌,我设法建立了一个适合我需求的页面。

Web应用程序应该从我使用localhost上的另一个Windows应用程序生成的XML文件加载数据,显示此数据并允许用户搜索它。

此XML文件具有超过50 MB和超过120.000个条目。

我正在将这个XML文件读入数据集,然后我将其绑定到gridView。

问题是:

  • 当我第一次加载页面时,最多可能需要30秒
  • 当我搜索数据加载时可能需要10秒以上

我怎么解决这个问题? 我尝试过StateView,但是会导致“Ran out of Memory”异常。 我做了一些研究,似乎我可以将这个数据集保存在服务器缓存中,这样可以让所有用户立即访问它,而不需要每次为每个用户重新加载XML?

这是我目前的代码,如果有什么不好,请告诉我,因为我不知道ASP.NET。 谢谢。

 public DataSet ds = new DataSet();
    public DataSet resultDS = new DataSet();
    public bool searchListActive = false;
    string _sortDirection = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
        if (!IsPostBack)
        {
            gridView_IndexData.PageSize = Convert.ToInt32(ddList_DataCount.SelectedItem.Value);
            ViewState["searchListActive"] = false;
            BindCB();
        }

        gridView_IndexData.PageSize = Convert.ToInt32(ddList_DataCount.SelectedItem.Value);
    }



    void BindGrid()
    {
        ds.ReadXml(Server.MapPath("~/lstData.xml"));
        gridView_IndexData.DataSource = ds;
        gridView_IndexData.DataBind();
    }

    void BindCB()
    {
        DataTable dt = ds.Tables[0].DefaultView.ToTable(true, "forumName");
        var DR = dt.NewRow();
        DR["forumName"] = "All forums";
        dt.Rows.InsertAt(DR, 0);
        dt.AcceptChanges();
        ddList_Forum.DataSource = dt;
        ddList_Forum.DataTextField = "forumName";
        ddList_Forum.DataBind();
    }



    protected void btnSearchQuery_Click(object sender, EventArgs e)
    {
        resultDS = ds.Clone();

        string searchQuery = "";

        searchQuery = "TopicTitle LIKE '%" + tbSearchInput.Text + "%'";

        if (tbSearchByUsername.Text.Length > 0)
        {
            searchQuery += "AND UserName ='" + tbSearchByUsername.Text + "'";
        }

        if (ddList_Type.Text != "")
        {
            searchQuery += "AND Type ='" + ddList_Type.Text + ":'";
        }

        if (ddList_Forum.Text != "All forums")
        {
            searchQuery += "AND forumName ='" + ddList_Forum.Text + "'";
        }


        var results = ds.Tables[0].Select(searchQuery);
        resultDS.Tables.Add();

        foreach (DataRow dr in results)
        {
            resultDS.Tables[0].ImportRow(dr);
        }

        resultDS.AcceptChanges();
        gridView_IndexData.DataSource = resultDS.Tables[0];
        ViewState["searchListActive"] = true;
        ViewState["resultDS"] = resultDS;
        gridView_IndexData.DataBind();

    }

    protected void gridView_IndexData_Sorting(object sender, GridViewSortEventArgs e)
    {
        SetSortDirection(e.SortDirection.ToString());
        ds.Tables[0].DefaultView.Sort = e.SortExpression + " " + _sortDirection;
        gridView_IndexData.DataSource = ds.Tables[0];
        gridView_IndexData.DataBind();
    }

    void SetSortDirection(string sortDirection)
    {
        if (sortDirection == "Descending")
        {
            _sortDirection = "DESC";
        }
        else
        {
            _sortDirection = "ASC";
        }
    }

    protected void gridView_IndexData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridView_IndexData.PageIndex = e.NewPageIndex;
        if ((bool)ViewState["searchListActive"] == true)
        {
            gridView_IndexData.DataSource = (DataSet)ViewState["resultDS"];
        }
        gridView_IndexData.DataBind();
    }

1)如果您将共享相同的数据集实例,它将在并发用户同时搜索时给您错误

2)优化搜索速度将您的结构转换为LINQ而不是数据集查询

我会使用适当的xml处理组合,例如使用xmlreader。 这是一篇指向该文章的文章( http://forums.asp.net/t/1939295.aspx?Most+efficient+way+to+iterate++++XML )。 并且,尽可能最大限度地利用缓存。 这里有一些指示( ASP.Net中的数据缓存 )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM