Гость
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Можно ли выбраться из этого заколдованного круга? / 4 сообщений из 4, страница 1 из 1
15.05.2021, 00:59
    #40070490
Можно ли выбраться из этого заколдованного круга?
Согласно библии,

авторIf GROUP BY is present, the VIEW definition must contain COUNT_BIG(*) and must not contain HAVING.
These GROUP BY restrictions are applicable only to the indexed view definition.
A query can use an indexed view in its execution plan even if it does not satisfy these GROUP BY restrictions.

If the SELECT statement in the view definition specifies a GROUP BY clause, the key of the unique clustered index can reference only columns specified in the GROUP BY clause.

If the view definition contains a GROUP BY clause, the key of the unique clustered index can reference only the columns specified in the GROUP BY clause.

Поэтому, разумеется, вот это не прокатит:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
create table t (t bit)
go
create view v with schemabinding as select t, '' as l from dbo.t where t = 0 group by t
go
create unique clustered index idxv on v (t)
go
/*
Msg 10138, Level 16, State 1, Line 1
Cannot create index on view 'db.dbo.v' because its select list does not include a proper use of COUNT_BIG. Consider adding COUNT_BIG(*) to select list.
*/



Но не прокатывает и когда делаешь, как там выше требуется:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
drop view v
go
create view v with schemabinding as select t, '' as l, count_big(*) as c from dbo.t where t = 0 group by t
go
/*
Msg 8668, Level 16, State 0, Line 1
Cannot create the clustered index 'idxv' on view 'db.dbo.v' because the select list of the view contains an expression on result of aggregate function or grouping column. Consider removing expression on result of aggregate function or grouping column from select list.
*/



Что именно нельзя, из этого сообщения непонятно. Мне нужно поле t и константа l.
Индекс я создаю на t, и по нему же group by. Что я упустил?
Объясните, пожалуйста!
...
Рейтинг: 0 / 0
15.05.2021, 02:22
    #40070495
felix_ff
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Можно ли выбраться из этого заколдованного круга?
гомодиализ
Согласно библии,

авторIf GROUP BY is present, the VIEW definition must contain COUNT_BIG(*) and must not contain HAVING.
These GROUP BY restrictions are applicable only to the indexed view definition.
A query can use an indexed view in its execution plan even if it does not satisfy these GROUP BY restrictions.

If the SELECT statement in the view definition specifies a GROUP BY clause, the key of the unique clustered index can reference only columns specified in the GROUP BY clause.

If the view definition contains a GROUP BY clause, the key of the unique clustered index can reference only the columns specified in the GROUP BY clause.


Поэтому, разумеется, вот это не прокатит:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
create table t (t bit)
go
create view v with schemabinding as select t, '' as l from dbo.t where t = 0 group by t
go
create unique clustered index idxv on v (t)
go
/*
Msg 10138, Level 16, State 1, Line 1
Cannot create index on view 'db.dbo.v' because its select list does not include a proper use of COUNT_BIG. Consider adding COUNT_BIG(*) to select list.
*/



Но не прокатывает и когда делаешь, как там выше требуется:

Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
drop view v
go
create view v with schemabinding as select t, '' as l, count_big(*) as c from dbo.t where t = 0 group by t
go
/*
Msg 8668, Level 16, State 0, Line 1
Cannot create the clustered index 'idxv' on view 'db.dbo.v' because the select list of the view contains an expression on result of aggregate function or grouping column. Consider removing expression on result of aggregate function or grouping column from select list.
*/



Что именно нельзя, из этого сообщения непонятно. Мне нужно поле t и константа l.
Индекс я создаю на t, и по нему же group by. Что я упустил?
Объясните, пожалуйста!

у вас мешается строковый литерал поскольку он считается выражением.
а данное выражение в контексте наличия group by косвенно влияет на результат аггрегата.

уберите
Код: sql
1.
'' as l

и тогда индекс построится.
вообще у индексированых вьюх слишком дофига ограничений, вы уверены что оно вам надо?
...
Рейтинг: 0 / 0
15.05.2021, 02:33
    #40070496
felix_ff
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Можно ли выбраться из этого заколдованного круга?
ну или на крайняк если вам уж совсем нужна строковая константа делайте так:
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
create table [dbo].[tbl] (
[t] bit not null,
[c] as '' persisted
);
go

create or alter view [vw] with schemabinding as
select [t], [c], count_big(*) as [cnt] from [dbo].[tbl] where [t] = 0 group by [t], [c];
go

create unique clustered index [ix] on [dbo].[vw] ([t], [c]);
...
Рейтинг: 0 / 0
15.05.2021, 10:12
    #40070504
invm
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Можно ли выбраться из этого заколдованного круга?
гомодиализ,

Можно так
Код: sql
1.
2.
3.
4.
5.
6.
7.
8.
create table t (t bit)
go
create view v__internal with schemabinding as select t, count_big(*) as c from dbo.t where t = 0 group by t
go
create unique clustered index idxv__internal on v__internal (t)
go
create view v as select t, '' as l, c from dbo.v_internal with (noexpand)
go
...
Рейтинг: 0 / 0
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Можно ли выбраться из этого заколдованного круга? / 4 сообщений из 4, страница 1 из 1
Целевая тема:
Создать новую тему:
Автор:
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]