如何创建索引以加快数据检索

时间:2019-05-08 03:22:59

标签: sql-server indexing

我在下面有这个查询,它从服务器上获取6000多行项目,下载的数据仅为317KB,但将近25秒,否则IIS服务器会超时。

-- Region Parameters
DECLARE @p0 Int = 0
DECLARE @p1 NVarChar(1000) = 'sample.user'
DECLARE @p2 Int = 0
DECLARE @p3 NVarChar(1000) = 'sample.user'
-- EndRegion
SELECT [t0].[Id], [t0].[DocNumber], [t0].[DocStatus], [t0].[Title], [t0].[SubmittedDate], [t0].[CreatedBy], [t0].[ApprovedByStep], [t0].[RoutedFor], 
    (CASE 
        WHEN [t2].[test] IS NULL THEN NULL
        ELSE [t2].[ApprovedBy]
     END) AS [HistApprovedBy]
FROM [TESTDB].[dbo].[SampleTable] AS [t0]
LEFT OUTER JOIN (
    SELECT 1 AS [test], [t1].[RequestId], [t1].[DocType], [t1].[ApprovedBy]
    FROM [TESTDB].[dbo].[SampleHistories] AS [t1]
    ) AS [t2] ON ([t0].[Id] = [t2].[RequestId]) AND (@p0 = [t2].[DocType])
WHERE ([t0].[CreatedBy] = @p1) OR (([t0].[DocStatus] <> @p2) AND ([t0].[CreatedBy] <> @p3))
ORDER BY [t0].[DocNumber]

我了解到索引可以加快数据检索的速度,如何在上面的sql查询中创建索引?以及关于如何加快速度的任何建议。我会很感激的。

点击here以制定行动计划

供参考以下是我的LINQ

var vm = (from splTbl in SampleTable
      join splsplHist in SamplesplHistories on
      new
      {
          Key1 = splTbl.Id,
          Key2 = 0
      }
      equals
      new
      {
          Key1 = splHist.RequestId,
          Key2 = splHist.DocType
      }
      into subsplHists
      from subsplHist in subsplHists.DefaultIfEmpty()
      where (splTbl.CreatedBy == "sample.user")
            || (splTbl.DocStatus != 0 && splTbl.CreatedBy != "sample.user")
      orderby splTbl.DocNumber
      select new
      {
          splTbl.Id,
          splTbl.DocNumber,
          splTbl.DocStatus,
          splTbl.Title,
          splTbl.SubmittedDate,
          splTbl.CreatedBy,
          splTbl.ApprovedByStep,
          splTbl.RoutedFor,
          splHistApprovedBy = (subsplHist == null ? null : subsplHist.ApprovedBy)
      }
    ).ToList()
    .GroupBy(x => new {
        x.Id,
        x.DocNumber,
        x.DocStatus,
        x.Title,
        x.SubmittedDate,
        x.CreatedBy,
        x.ApprovedByStep,
        x.RoutedFor
    }).Select(x => new
    {
        x.Key.Id,
        x.Key.DocNumber,
        x.Key.DocStatus,
        x.Key.Title,
        x.Key.SubmittedDate,
        x.Key.CreatedBy,
        x.Key.ApprovedByStep,
        x.Key.RoutedFor,
        splHistApprovedBy = string.Join(",", x.Where(i => i.splHistApprovedBy != null).Select(i => i.splHistApprovedBy).Distinct())
    }).ToList();

1 个答案:

答案 0 :(得分:0)

@Dale Burelle的建议

我创建了行动计划建议的以下索引。

androidx.appcompat.app