vista indizada y T-SQL que dan los mismos resultados a diferencia de SQL Server 2008 R2
-
11-10-2019 - |
Pregunta
Quiero crear vista indizada MyView con tal de T-SQL
Select
o.Theme_ID as Theme_ID,
DATEADD(day, DATEDIFF(day, 0, o.Object_CreationDate), 0) as Objext_CreationDate,
Count_BIG(*) as ObjectCount, o.Object_Tonality from [dbo].Object o
inner join [dbo].Theme t on o.Theme_ID = t.Theme_ID
inner join [dbo].[ThemeWorkplace] tw on t.Theme_ID = tw.Theme_ID
inner join [dbo].Workplace w on w.Workplace_ID = tw.Workplace_ID
... where t.Theme_DeletedMark = 0
AND (w.Workplace_AccessType = 1 OR w.Workplace_AccessType = 8)
AND Object_DeletedMark = 0 ...
Group BY o.Theme_ID,o.Object_Tonality, DATEADD(day, DATEDIFF(day, 0, o.Object_CreationDate), 0)
Este T-SQL funciona bien y permite establecer un índice agrupado en MyView .
El problema es que la tabla ThemeWorkplace
contiene varios registros con Theme_ID
misma.
Y aunque yo uso GROUP BY
- me pongo en valor Object_Count
que
es igual a: (valor Object_Count
real) * recuento (Theme_ID
en ThemeWorkplace
).
No se puede utilizar la palabra DISTINCT
en T-SQL, ya que en este caso es imposible crear el índice a la vista.
¿Qué es una sugerencia para obtener resultados correctos en mi opinión?
Solución
As you've noted, there are significant restrictions on creating an indexed view. The techniques that might help you here, such as distinct or a subquery are prohibited. I think you'll need to sacrifice materializing the view in this particular case.