我正在尝试对以下表优化我的内连接语句:
文章组包含大约700行
products包含大约150.000行
对于products中的每个产品,products_category_mapping包含1到3行
下面是我当前的查询:
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上添加了一个聚集索引,因为我认为最后一列可能具有最不同的结果,并且需要最快的速度。不过,我不确定我现在所做的是否正确。
发布于 2015-09-18 13:33:42
ARTICLEGROUPS只有700个rows.This,这是一个很小的表,您也可以尝试不对其进行索引。这里使用的列是GA.ID, GA.CATLEVEL。也许你可以试试下面的索引。
Create Index IX_id on ARTICLEGROUPS (id) Include (CATLEVEL asc);PRODUCTS有150000行,使用的列为GP.ID.如果LABEL和PAGE不是来自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 <行。列使用PCM.ARTICLEGROUP_ID和PCM.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)添加索引后查看查询的执行计划。我在答案部分添加了这一点,因为我在评论部分写这篇文章时发现它非常笨拙,希望这能对你有所帮助。
https://stackoverflow.com/questions/32643306
复制相似问题