|
首先,我想提一下,我已经检查了所有其他问题,没有一个和我相似,所以我认为这不是重复的。
4 q2 a- ?4 N% |$ F9 K5 q到目前为止,我有两个表, Article_tbl ArticleZone_tbl行数几乎相同。
9 x9 ^1 u. \, M) a& Q% j/ V“ Article_tbl”包含一个身份主键“ ArticleID”。“ ArticleZone_tbl包含一个由三列组成的主键:4 R" V) L$ p1 Z* P" {7 W2 `
ArticleID”,“ ChannelID”,“ ZoneID其中 ArticleID Article_tbl”的外键0 E$ {( Z. |7 |7 e! O
在列表中创建非聚集索引进行排序。+ }7 _; j0 u8 G7 }0 `( @
SQL查询:& C V% V( M) S9 M. }, K1 I
WITH OrderedOrders AS(Select ROW_NUMBER() Over(Order by LastEditDate desc,ArticleOrder Asc,LastEditDateTime desc) as RowNum,dbo.Article_tbl.*,ArticleZone_tbl.ChannelID,ArticleZone_tbl.ZoneID,ArticleZone_tbl.ArticleOrderFrom Article_tbl INNER JOIN ArticleZone_tbl ON dbo.Article_tbl.ArticleID = dbo.ArticleZone_tbl.ArticleIDWhere ChannelID=1 And ZoneID=0)SELECT * FROM OrderedOrders Where RowNum Between 1 And 10以上查询需要2秒左右才能完成,有没有优化此查询的方法?/ n) T8 {# }& f2 s2 h/ `0 T9 ~
更多信息:操作系统:Windows WebServer 2008R2 SQL Server:2008R2 RAM:32GB HDD:160GB SSD
+ g5 _# D$ h' q# l( x提前致谢。5 [, w7 w; Y- ^" F3 `
最好的问候,McHaimech6 h8 w+ d8 Y. E; f' e V2 w
: P( O! O4 j+ r8 F 解决方案: 1 O; [* h1 y* W$ ]( T
您可以尝试在两个表上创建索引视图:$ `; h2 \# A+ _8 H+ y( z
CREATE VIEW dbo.YourIndexedViewWITH SCHEMABINDING AS SELECT az.ArticleID, az.ChannnelID, az.ZoneID, a.LastEditDate, a.LastEditDateTime, az.ArticleOrder FROM dbo.Article_tbl a INNER JOIN dbo.ArticleZone_tbl az ON a.ArticleID = az.AtricleID;GOCREATE UNIQUE CLUSTERED INDEX UQ_YourIndexView_ArticleID_ChannelID_ZoneID ON dbo.YourIndexedView (ArticleID,ChannelID,ZoneID);一旦有了聚簇索引,就可以创建非聚簇索引,这将有助于排序:
' ~' g b1 a/ U- |+ e7 W% P6 y! MCREATE NONCLUSTERED INDEX IX_YourIndexedView_LastEditDate_ArticleOrder_LastEditDateTime ON dbo.YourIndexedView (LastEditDate DESC,ArticleOrder ASC,LastEditDateTime DESC);然后,您可以在查询中引用它:
! b5 F! M9 C5 EWITH OrderedOrders AS( SELECT RowNum = ROW_NUMBER() OVER(ORDER BY LastEditDate DESC,ArticleOrder ASC,LastEditDateTime DESC), ArticleID, ChannelID, ZoneID, LastEditDateTime, ArticleOrder FROM dbo.YourIndexedView WITH (NOEXPAND) WHERE ChannelID = 1 AND ZoneID = 0)SELECT *FROM OrderedOrdersWHERE RowNum BETWEEN 1 AND 10;注:我可能错过了你文章表中的一些列,但我无法从问题中推断出它们+ \' @/ Z7 N% ^$ ]
此外,如果您的查询总是有相同的区域和通道,您可以过滤视图,那么您的收集索引列将成为ArticleID:
+ N6 p2 A# _9 n4 T p) L5 S1 R/ {CREATE VIEW dbo.YourIndexedViewWITH SCHEMABINDING AS SELECT az.ArticleID, az.ChannnelID, az.ZoneID, a.LastEditDate, a.LastEditDateTime, az.ArticleOrder FROM Article_tbl a INNER JOIN ArticleZone_tbl az ON a.ArticleID = az.AtricleID WHERE az.ChannelID = 1 AND Az.ZoneID = 1;GOCREATE UNIQUE CLUSTERED INDEX UQ_YourIndexView_ArticleID ON dbo.YourIndexedView (ArticleID);这意味着你的索引会更小更快。 |
|