私はasp.net mvc5を初めて使用しています。ASP.NET MVC(Dupe)でSQL Serverデータベースからデータを取得する速度を向上させる方法
私はモデル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);
そして、私の最初のページの必見は販売されていないすべてのshippings(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;
shippingsを取得するための賢い方法ではありませんし、DBが大きくなった場合、それは非常に遅く..です
助けてください!
多くの 'nvarchar(max)'列があります...なぜですか? – SqlZim
@ SqlZim私は 'string'を設定していないので、制限はありません。私は彼らに最大の価値を与えるか? –
あなたの本当の問題はあなたが提案したコードにあるとは思わない。私はそれらが含まれていると思います。それはそれらの価値をすべて分析するための実際のコストです。 1つの質問:ビュー内のすべての列を実際に表示していますか? –