如何提高ASP.NET MVC中从SQL Server数据库获取数据的速度

时间:2017-03-15 16:38:42

标签: c# sql asp.net sql-server azure

我是asp.net mvc5的新手。我有一个模型Shipping,我的dbo.Shippings.sql是这样的:

CREATE TABLE [dbo].[Shippings] 
(
    [Id]                INT             IDENTITY (1, 1) NOT NULL,
    [TruckerId]         NVARCHAR (MAX)  NULL,
.........more there...

    CONSTRAINT [PK_dbo.Shippings] 
        PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_dbo.Shippings_dbo.AspNetUsers_ApplicationUserId] 
        FOREIGN KEY ([ApplicationUserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);

CREATE NONCLUSTERED INDEX [IX_ApplicationUserId]
    ON [dbo].[Shippings]([ApplicationUserId] ASC);

我的第一页必须显示未售出的所有送货(if shipping.truckerId == null表示尚未售出)

我的ShippingController我在下面:

// GET: Shipping
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
        //below is sorting
        ViewBag.CurrentSort = sortOrder;
        ViewBag.FromSortParm = String.IsNullOrEmpty(sortOrder) ? "from_desc" : "";
        ViewBag.DestSortParm = sortOrder == "Destination" ? "dest_desc" : "Destination";

        if (searchString != null)
        {
            page = 1;
        }
        else
        {
            searchString = currentFilter;
        }

        ViewBag.CurrentFilter = searchString;

        //below is searching state and zipcode
        var shippings = from s in db.Shippings
                        where s.TruckerId == null
                        select s;

        if (!String.IsNullOrEmpty(searchString))
        {
            shippings = shippings.Where(s => s.FromState.Contains(searchString)//from state
                                   || s.DestState.Contains(searchString)//dest state
                                   || s.FromZipCode.Contains(searchString)//from zipcode
                                   || s.DestZipCode.Contains(searchString));//dest zipcode
        }

        switch (sortOrder)
        {
            case "from_desc":
                shippings = shippings.OrderByDescending(s => s.From);
                break;
            case "Destination":
                shippings = shippings.OrderBy(s => s.Destination); //not working
                break;
            case "dest_desc":
                shippings = shippings.OrderByDescending(s => s.Destination); //not working
                break;
            default:
                shippings = shippings.OrderByDescending(s => s.OrderDateTime);
                break;
        }

        int pageSize = 10;
        int pageNumber = (page ?? 1);
        return View(shippings.ToPagedList(pageNumber, pageSize));
        //return View(shippings.ToList());

}

我想:

var shippings = from s in db.Shippings
                where s.TruckerId == null
                select s;

并不是一种聪明的方式来获取信息,如果数据包变大,它就会非常慢......

我只需要显示它的FromCity,FromState,FromZipCode,PickUpDateTime,DestCity,DestState,DestZipCode,Price,Content和TruckerId,它们告诉它是否已售出。

请帮忙!

已修改并更新: 当我提出这个问题时,我对Web开发和数据库还是比较陌生的。 通过为db创建索引,我能够获得更好的性能。所以快速查找可能。 我还制作了网络应用程序的架构。 我还创建了一个未售出的运输视图。因此,如果网络从数据库点击视图,它就不必花时间过滤掉。

1 个答案:

答案 0 :(得分:1)

没有声誉可以评论,我会删除它,或者重拍以回答!!!

怎么样:

  1. 添加一些枚举状态(已售出,有效,有租,......)或仅在不需要更多状态时仅为卖出的bool值
  2. 或者某些优化(比如将NVARCHAR(MAX)减少到可能的最长值,例如属性名称为NVARCHAR(20)
  3. P.S。 :我认为,这种选择不会放慢很多......