在不同表中具有翻译的实体的索引策略

时间:2013-12-11 12:29:35

标签: sql-server sql-server-2008-r2 indexing

我有以下数据结构:

Article: IdArticle, IdParentArticle, IdWebsite, Author, DateUpdated, Status, ...
ArticleTran: IdArticle, IdLanguage, Title, Content, ...

我需要在文章上构建不同的查询,同时考虑到: - 总是有一个当前的网站(参见列IdWebsite)和一个语言ID(参见列IdLanguage)来过滤 - 在第I条中可以有存根文章(即文章从父母那里继承数据 - 请参阅IdParentArticle列) - 如果标题或内容不可用,则应回退到默认语言

现在我在考虑选择数据库设计和索引策略:

1 /创建一个视图vwArticles ,返回包含完整信息的文章列表,关于文章是否为存根

SELECT A.IdArticle, A.IdParentArticle, A.IdWebsite, ... 
FROM Article A      
WHERE A.IdParentArticle IS NULL    
UNION      
SELECT Stub.IdArticle, Stub.IdParentArticle, A.IdWebsite, ...   
FROM Article Stub
    INNER JOIN Article A ON A.IdArticle = Stub.IdParentArticle
WHERE Stub.IdParentArticle IS NOT NULL      

2 /创建包含文章信息+翻译信息的视图vwArticlesTran

SELECT
    A.IdArticle, A.IdParentArticle, A.IdWebsite,
    COALESCE(L.Title, LD.Title),
    COALESCE(L.Content, LD.Content),...
FROM
    vwArticles A
    LEFT JOIN ArticleTran L ON A.IdArticle = L.IdArticle
    -- fallback for default language
    LEFT JOIN ArticleTran LD ON A.IdArticle = LD.IdArticle AND LD.IdLanguage = 1

3 /主键:

PK Article: on column IdArticle, non-clustered
PK ArticleTran: on columns IdArticle + IdLanguage, clustered

4 /创建以下索引:

IX_ArticleTran_IdArticle => for JOIN in vwArticles
IX_ArticleTran_Title => for search after title
IX_Article_DateUpdated - clustered => articles are usually ordered by DateUpdated DESC

您对我的提案有任何其他建议吗?有什么可以改进的吗? 是否值得在ArticleLang上创建覆盖索引?

0 个答案:

没有答案