(问:tuning in sql server - views。抱歉,我需要查看我的帖子。)
我需要在连接中添加提示(在进行连接时强制使用ID上的索引), 并在select(强制使用where子句的名称上的索引),作为本文中提到的最后一个查询。 我不知道正确连接的正确语法是什么(用于调优),以及force index的正确语法是什么,where子句将使用它,而我从视图中选择。
我在sql server 2012中创建了一个视图,例如:
create myview as
select mytable2.name
from mytable1 t1
join myTable2 t2
on t1.id = t2.id
我希望连接table1和table2将使用正确的索引(id),但是当我这样做时:
select * from myview
where name = 'abcd'
我希望最后一次选择将使用列' name'。
的索引正如我所描述的那样,带有提示(调优)的sql server中的正确语法是什么,它运行得最好?
我想强制使用索引仅用于连接目的(column = id),并强制执行索引名称:
select name from myview
where name = 'abcd'.
像
这样的东西create myview as
select mytable2.name
/* index hint name on column name */
from mytable1 t1
join myTable2 t2
/* index hint name on column id - just for join */
on t1.id = t2.id
我不想在执行视图时强制使用视图添加提示的最终用户 - 只需使用正确的索引提示将视图作为他的视图。 (或者,如果不可能 - 我该怎么做)。
需要样品。
我有一个如此简单的视图(无论基于什么表......它都是我正在为我所工作的公司指定的。这是Microsoft Dynamics AX 2012+的一些额外表格。)
create view [dbo].[splSqlAltItemsView]
as
select i_orig.dataareaid, id_orig.inventsiteid, i_orig.itemid, i_alt.itemid altItemId,
orig_item.minTollerance, orig_item.maxTollerance
from
(
select orig_item.recid,
c_grade.value as grade,
c_materialFamily.value as MaterialFamily,
c_shape.value as shape,
c_thickness.value as thickness,
convert(real, isnull(c_minTollerance.value, 0.00)) as minTollerance,
convert(real, isnull(c_maxTollerance.value, 0.00)) as maxTollerance
from
(select e0.recid
from ECORESPRODUCT e0
where e0.recid in
(
select productid
from CMAPRODUCTATTRIBUTEVALUES c0
where name in ('Grade', 'Material Family', 'Shape', 'Thickness')
and productid > 0
group by productid
having count(1) = 4
)) orig_item
join CMAPRODUCTATTRIBUTEVALUES c_grade
on c_grade.PRODUCTID = orig_item.RECID
and c_grade.name = 'Grade'
join CMAPRODUCTATTRIBUTEVALUES c_materialFamily
on c_materialFamily.PRODUCTID = orig_item.RECID
and c_materialFamily.name = 'Material Family'
join CMAPRODUCTATTRIBUTEVALUES c_shape
on c_shape.PRODUCTID = orig_item.RECID
and c_shape.name = 'Shape'
join CMAPRODUCTATTRIBUTEVALUES c_thickness
on c_thickness.PRODUCTID = orig_item.RECID
and c_thickness.name = 'Thickness'
left join CMAPRODUCTATTRIBUTEVALUES c_minTollerance
on c_minTollerance.PRODUCTID = orig_item.RECID
and c_minTollerance.name = 'Min Tollerance'
left join CMAPRODUCTATTRIBUTEVALUES c_maxTollerance
on c_minTollerance.PRODUCTID = orig_item.RECID
and c_minTollerance.name = 'Max Tollerance'
) orig_item
join
(select alt_item.recid,
c_grade.value as grade,
c_materialFamily.value as MaterialFamily,
c_shape.value as shape,
c_thickness.value as thickness,
convert(real, isnull(c_minTollerance.value, 0.00)) as minTollerance,
convert(real, isnull(c_maxTollerance.value, 0.00)) as maxTollerance
from
(select e0.recid
from ECORESPRODUCT e0
where e0.recid in
(
select productid
from CMAPRODUCTATTRIBUTEVALUES c0
where name in ('Grade', 'Material Family', 'Shape', 'Thickness')
and productid > 0
group by productid
having count(1) = 4
)) alt_item
join CMAPRODUCTATTRIBUTEVALUES c_grade
on c_grade.PRODUCTID = alt_item.RECID
and c_grade.name = 'Grade'
join CMAPRODUCTATTRIBUTEVALUES c_materialFamily
on c_materialFamily.PRODUCTID = alt_item.RECID
and c_materialFamily.name = 'Material Family'
join CMAPRODUCTATTRIBUTEVALUES c_shape
on c_shape.PRODUCTID = alt_item.RECID
and c_shape.name = 'Shape'
join CMAPRODUCTATTRIBUTEVALUES c_thickness
on c_thickness.PRODUCTID = alt_item.RECID
and c_thickness.name = 'Thickness'
left join CMAPRODUCTATTRIBUTEVALUES c_minTollerance
on c_minTollerance.PRODUCTID = alt_item.RECID
and c_minTollerance.name = 'Min Tollerance'
left join CMAPRODUCTATTRIBUTEVALUES c_maxTollerance
on c_minTollerance.PRODUCTID = alt_item.RECID
and c_minTollerance.name = 'Max Tollerance'
) alt_item
on alt_item.grade = orig_item.grade
and alt_item.MaterialFamily = orig_item.MaterialFamily
and alt_item.shape = orig_item.shape
and (alt_item.thickness between orig_item.thickness - orig_item.minTollerance and orig_item.thickness + orig_item.maxTollerance or
orig_item.thickness between alt_item.thickness - alt_item.minTollerance and alt_item.thickness + alt_item.maxTollerance)
join inventtable i_orig
on i_orig.product = orig_item.recid
join InventItemInventSetup is_orig
on is_orig.dataareaid = i_orig.dataareaid
and is_orig.itemid = i_orig.itemid
and is_orig.inventdimid = 'AllBlank'
join InventDim id_orig
on id_orig.DATAAREAID = is_orig.DATAAREAID
and id_orig.inventdimid = is_orig.inventdimiddefault
join inventtable i_alt
on i_alt.product = alt_item.recid
and i_alt.DATAAREAID = i_orig.DATAAREAID
join InventItemInventSetup is_alt
on is_alt.dataareaid = i_alt.dataareaid
and is_alt.itemid = i_alt.itemid
and is_alt.inventdimid = 'AllBlank'
join InventDim id_alt
on id_alt.DATAAREAID = is_alt.DATAAREAID
and id_alt.inventdimid = is_alt.inventdimiddefault
and id_alt.inventsiteid = id_orig.inventsiteid
当我这样做时:
select * from splSqlAltItemsView
where itemid = '12345'
有很多时间,甚至inventTable都有一个关于itemid的索引。
当我这样做时:
select * from splSqlAltItemsView
视图运行了几秒钟。
答案 0 :(得分:2)
我相信你知道使用提示通常是一个坏主意,它应该只在非常特殊的情况下使用,但是......
create myview as
select mytable2.name
from mytable1 t1 WITH (INDEX(index_name1))
join myTable2 t2
WITH (INDEX(index_name2))
on t1.id = t2.id