【问题标题】:For which column index needs to be created需要为哪一列创建索引
【发布时间】:2015-09-15 12:54:04
【问题描述】:
Select distinct c.classID, co.fCourseName as CourseName, StreetAddress + ', ' + l.City as LocationAddress, s.SessionName, sh.fShift as shift, StartTime, EndTime, c.classname, s.SessionID,
    c.StartDate,c.enddate 
    From dbo.vw_Class c 
    Inner Join dbo.lk_Session s 
    On (s.SessionID = c.sessionID) 
    Inner Join dbo.lk_Course co 
    On (co.CourseID = c.CourseID )
    Inner Join dbo.vw_Location l 
    On (l.locationid = c.locationid) 
    Inner Join lk_District d
    On (d.districtID = c.districtId) 
    Inner Join lk_Province p 
    On (p.provik = d.provik) 
    Inner Join lk_Shift sh 
    On (c.shiftid = sh.shiftid)
   where 
      c.DistrictID       =  case  when @Districtid is null   then c.DistrictID   else  @Districtid  end 
     and c.LocationID    =  case  when @locationid is null   then c.LocationID   else  @locationid  end 
     and s.SessionID     =  case  when @sessionid is null    then s.SessionID    else  @sessionid   end 
     and c.CourseID      =  case  when @levelid  is null     then c.CourseID     else  @levelid     end 
     and c.ShiftID       =  case  when @shiftid   is null    then c.ShiftID      else  @shiftid     end 
     and c.StartDate    >=  case  when @startdate is null    then c.StartDate    else  @startdate   end
     and c.EndDate      <=  case when  @enddate is null      then c.EndDate      else  @enddate     end
     and convert(time,c.StartTime) >= case when @starttime is null then convert(time,c.StartTime) else convert(time,@starttime) end
     and convert(time,c.endtime)   <= case when @endtime is null then convert(time,c.endtime) else convert(time,@endtime) end
     and c.Monday    = case  when @day1 = 'N' then c.monday     else  @day1  end 
     and c.Tuesday   = case  when @day2 = 'N' then c.Tuesday        else  @day2  end 
     and c.Wednesday = case  when @day3 = 'N' then c.Wednesday  else  @day3  end 
     and c.Thursday  = case  when @day4 = 'N' then c.Thursday       else  @day4  end 
     and c.Friday    = case  when @day5 = 'N' then c.Friday     else  @day5  end 
     and c.Saturday  = case  when @day6 = 'N'then c.Saturday        else  @day6  end 
     and c.Sunday    = case  when @day7 = 'N' then c.Sunday     else  @day7  end 
     and c.RowStatus    = 'A' 
     ORDER BY co.fCourseName, s.SessionID ,c.ClassName

在上面的代码中,我需要为哪些列创建索引。(此查询中使用的所有表都没有创建主键或索引)

【问题讨论】:

  • "(此查询中使用的所有表都没有主键" - 为什么不呢?每个表都应该有一个主键。
  • 你确定它们都是表吗 vw_ 通常表示一个视图
  • 我认为您首先在此查询中使用的每个查找表上创建主键。然后在每个连接条件中创建一个非聚集的。
  • @ArunGairola 不要将主键与聚集索引混淆。主键只需要是唯一索引,它不必是集群索引(尽管这是默认值,通常是最好的选择......)
  • 嗨@ZoharPeled 我同意你的观点,但如果是“Baiju”语句(此查询中使用的表都没有主键”,我建议他在表上创建主键。

标签: sql sql-server indexing database-performance


【解决方案1】:

首先,这是您的带有格式的 sql 查询,在我的公司中确定。 我将您的过滤器从 case (not surgable) 替换为“OR”。

SELECT DISTINCT
    [c].[classID]
, [co].[fCourseName] as [CourseName]
, [StreetAddress] + ', ' + [l].[City] as [LocationAddress]
, [s].[SessionName]
, [sh].[fShift] as [shift]
, [StartTime]
, [EndTime]
, [c].[classname]
, [s].[SessionID]
, [c].[StartDate]
, [c].[enddate]
FROM
    [dbo].[vw_Class] as [c]
INNER JOIN
    [dbo].[lk_Session] as [s] ON [s].[SessionID] = [c].[sessionID]
INNER JOIN
    [dbo].[lk_Course] as [co] ON [co].[CourseID] = [c].[CourseID]
INNER JOIN
    [dbo].[vw_Location] as [l] ON [l].[locationid] = [c].[locationid]
INNER JOIN
    [lk_District] as [d] ON [d].[districtID] = [c].[districtId]
INNER JOIN
    [lk_Province] as [p] ON [p].[provik] = [d].[provik]
INNER JOIN
    [lk_Shift] as [sh] ON [c].[shiftid] = [sh].[shiftid]
WHERE
(
        [c].[DistrictID] = @Districtid
    OR
        @Districtid IS NULL
)
AND
(
        [c].[LocationID] = @locationid
    OR
        @locationid IS NULL
)
AND
(
        [s].[SessionID] = @sessionid
    OR
        @sessionid IS NULL
)
AND
(
        [c].[CourseID] = @levelid
    OR
        @levelid IS NULL
)
AND
(
        [c].[ShiftID] = @shiftid
    OR
        @shiftid IS NULL
)
AND
(
        [c].[StartDate] >= @startdate
    OR
        @startdate IS NULL
)
AND
(
        [c].[EndDate] <= @enddate
    OR
        @enddate IS NULL
)
AND
(
        convert(time, [c].[StartTime]) >= @starttime
    OR
        @starttime IS NULL
)
AND
(
        convert(time, [c].[endtime]) <= @endtime
    OR
        @endtime IS NULL
)
AND
(
        convert(time, [c].[endtime]) <= @endtime
    OR
        @endtime IS NULL
)
AND
(
        [c].[Monday] = @day1
    OR
        @day1 = 'N'
)
AND
(
        [c].[Tuesday] = @day2
    OR
        @day2 = 'N'
)
AND
(
        [c].[Wednesday] = @day3
    OR
        @day3 = 'N'
)
AND
(
        [c].[Thursday] = @day4
    OR
        @day4 = 'N'
)
AND
(
        [c].[Friday] = @day5
    OR
        @day5 = 'N'
)
AND
(
        [c].[Saturday] = @day6
    OR
        @day6 = 'N'
)
AND
(
        [c].[Sunday] = @day7
    OR
        @day7 = 'N'
)
AND
    [c].[RowStatus] = 'A' 
ORDER BY
    [co].[fCourseName]
, [s].[SessionID]
, [c].[ClassName]

在回答您的问题时,您需要所有字段的索引,其中一个用于连接(如 [sessionID]、[CourseID] 等)。

您不能总是在视图上使用索引,但如果可以,请根据重复值在字段上创建它。我使用了下一条规则 - “如果某些列中的值重复超过 10%,我们可以在过滤器中使用它 - 创建索引”。

【讨论】:

    猜你喜欢
    • 2017-02-28
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多