小编典典

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

sql

我是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创建索引,我可以提高性能。如此快速的查找成为可能。另外,我重新制作了Web应用程序的体系结构。我还创建了一个未售出的运输视图。因此,始终只要Web从db中访问视图,就不必花费时间进行过滤。


阅读 159

收藏
2021-04-15

共1个答案

小编典典

没有足够的声誉来发表评论,我将其删除,或重新制作以回答!!!

关于什么 :

  1. 添加一些枚举状态(出售,活动,租金等),或者仅在不需要更多状态时才使bool值变高
  2. 或进行一些 优化 (例如将NVARCHAR(MAX)减小为可能的最长值,例如,属性名称为NVARCHAR(20)

PS:我认为,此选择不会使速度减慢很多。

2021-04-15