通过列索引优化INNER JOIN语句

时间:2015-09-18 03:20:53

标签: sql-server join query-optimization sql-server-2014-express

我试图在下表中优化我的INNER JOIN语句:

[articlegroups]包含~700行
[产品]包含~150.000行
[products_category_mapping]为[products]中的每个产品包含1行最多3行(因此在150.000和450.000行之间的任何位置)

这是我当前的疑问:

SELECT ga.label_sp,ga.label_en,ga.slug_sp,ga.slug_en,ga.pagetitle_sp,ga.pagetitle_en,ga.image_sp,ga.image_en,ga.description_sp,ga.description_en,ga.metadescription_sp,ga.metadescription_en
FROM articlegroups ga WITH (NOLOCK)
INNER JOIN products_category_mapping pcm on pcm.articlegroup_id=ga.id
INNER JOIN products gp on gp.id=pcm.artikelid
WHERE gp.id=<PRODUCTID> AND ga.catlevel=0

我在这里阅读http://www.sql-server-performance.com/2006/tuning-joins/我可以做的事情就是将索引添加到表所连接的列上。

现在我想知道什么会带来最佳表现: 向products_category_mapping.artikelid和/或products_category_mapping.articlegroup_id添加索引以及索引是什么类型的?我应该为两列添加索引吗?我应该将其中一个聚集在一起,如果是这样的话? 我现在已经在products_category_mapping.artikelid上添加了两列和一个聚簇索引的索引,因为我虽然最后一列可能有最不同的结果并且需要最快的速度。我不确定我现在所做的事情是否正确。

1 个答案:

答案 0 :(得分:0)

ARTICLEGROUPS只有700行。这是一个小表,您也可以尝试不对其进行索引。这里使用的列是GA.ID, GA.CATLEVEL。也许你可以试试下面的索引。

Create Index IX_id on ARTICLEGROUPS (id) Include (CATLEVEL asc);

PRODUCTS有150000行,使用的列为GP.ID.如果LABELPAGE不是来自PRODUCTS,请尝试

Create Clustered Index IX_id on PRODUCTS (id);

其他创建

Create Index IX_id on PRODUCTS (id) Include (..); -- Pls fill include part.

PRODUCTS_CATEGORY_MAPPING有150000&lt;行。列使用了PCM.ARTICLEGROUP_IDPCM.ARTIKELID。试试下面看看。

Create Index IX_agid on PRODUCTS_CATEGORY_MAPPING (ARTICLEGROUP_ID) Include (..) --If LABEL or PAGE is from this table add thosem coumns in the include part)

Create Index IX_aid on PRODUCTS_CATEGORY_MAPPING (ARTIKELID) Include (..) --If LABEL or PAGE is from this table add those columns in the include part)

添加索引后查看查询的执行计划。我在答案部分添加了这个,因为我发现在评论部分写这篇文章时非常笨拙,希望这对你有帮助。

相关问题