优化加载和搜索速度.净c#



我对ASP完全陌生。. NET,然而,经过一些基本的斗争,感谢谷歌,我设法建立了一个页面,以满足我的需求。

Web应用程序应该从我与本地主机上的另一个Windows应用程序生成的XML文件中加载数据,显示该数据并允许用户搜索它。

这个XML文件超过50 MB,超过120,000个条目。

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

问题是:

  • 当我第一次加载页面时,它可能需要30秒
  • 当我搜索数据加载可能需要超过10秒

我该如何解决这个问题?我尝试过状态视图,但是这会导致"内存不足"异常。我做了一些研究,似乎我可以将这个数据集保存在服务器缓存中,这将允许所有用户立即访问它,而不需要每次为每个用户重新加载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+through+XML)。并且,尽可能最大化地使用缓存。这里有一些指针(在ASP.Net中的数据缓存)。

最新更新