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

时间:2017-03-15 14:52:04

标签: sql-server query-performance

我是asp.net mvc5的新手。

我有一个模型Shipping,我的dbo.Shippings.sql显示在这里:

CREATE TABLE [dbo].[Shippings] 
(
    [Id]                INT             IDENTITY (1, 1) NOT NULL,
    [TrackingNumber]    NVARCHAR (MAX)  NOT NULL,
    [FromCompany]       NVARCHAR (MAX)  NULL,
    [FromContactName]   NVARCHAR (MAX)  NULL,
    [From]              NVARCHAR (MAX)  NOT NULL,
    [FromCity]          NVARCHAR (MAX)  NOT NULL,
    [FromState]         NVARCHAR (MAX)  NOT NULL,
    [FromZipCode]       NVARCHAR (MAX)  NOT NULL,
    [FromCountry]       NVARCHAR (MAX)  NOT NULL,
    [FromPhoneNumber]   NVARCHAR (MAX)  NULL,
    [FromFaxNumber]     NVARCHAR (MAX)  NULL,
    [Commodity]         NVARCHAR (MAX)  NOT NULL,
    [OrderDateTime]     DATETIME        NOT NULL,
    [OrderByName]       NVARCHAR (MAX)  NOT NULL,
    [PickUpDateTime]    DATETIME        NOT NULL,
    [PickUpRefNum]      NVARCHAR (MAX)  NOT NULL,
    [DestCompany]       NVARCHAR (MAX)  NULL,
    [DestContactName]   NVARCHAR (MAX)  NULL,
    [Destination]       NVARCHAR (MAX)  NOT NULL,
    [DestCity]          NVARCHAR (MAX)  NOT NULL,
    [DestState]         NVARCHAR (MAX)  NOT NULL,
    [DestZipCode]       NVARCHAR (MAX)  NOT NULL,
    [DestCountry]       NVARCHAR (MAX)  NOT NULL,
    [DestPhoneNumber]   NVARCHAR (MAX)  NULL,
    [DestFaxNumber]     NVARCHAR (MAX)  NULL,
    [DeliveryDateTime]  DATETIME        NOT NULL,
    [Price]             FLOAT (53)      NOT NULL,
    [Category]          NVARCHAR (MAX)  NOT NULL,
    [FreightClass]      INT             NOT NULL,
    [Pkgs]              INT             NOT NULL,
    [Weight]            REAL            NOT NULL,
    [Length]            REAL            NOT NULL,
    [Width]             REAL            NOT NULL,
    [Height]            REAL            NOT NULL,
    [IsDelivered]       BIT             NOT NULL,
    [ApplicationUserId] NVARCHAR (128)  NOT NULL,
    [AcceptOffer]       BIT             DEFAULT ((0)) NOT NULL,
    [TruckerId]         NVARCHAR (MAX)  NULL,
    [Description]       NVARCHAR (MAX)  NULL,
    [FileName]          NVARCHAR (255)  NULL,
    [ContentType]       NVARCHAR (100)  NULL,
    [Content]           VARBINARY (MAX) NULL,

    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
);
GO

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;

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

请帮忙!

1 个答案:

答案 0 :(得分:3)

显然,您需要检查数据类型。您实际上需要那么多VARCHAR(MAX)列是不可行的。所以首先要在这个意义上重塑数据库。我强烈建议您查看Joe Celko关于SQL Server Central的Stairway to Data系列文章,它将为您提供数据类型及其正确用法的清晰图片。

另一方面,如果你想只使用LINQ获得一些字段,你可以使用以下内容:

using (var ctx = new MyDataContext())
{
     var theDataINeed = (from myData in ctx.Shipping
                        where myData.TruckerId == null
                        select new {
                           myData.FromCity,
                           myData.FromState,
                           myData.FromZipCode,
                           myData.PickUpDateTime
                        });
}

然后你将它传递给List<>左右,你只会得到你需要的东西。