Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COUNT_BIG in Indexed View

Tags:

sql-server

CREATE TABLE test2 ( id INTEGER, name VARCHAR(10), family VARCHAR(10), amount INTEGER)  CREATE VIEW dbo.test2_v WITH SCHEMABINDING  AS SELECT id, SUM(amount) as amount -- , COUNT_BIG(*) as tmp FROM dbo.test2  GROUP BY id  CREATE UNIQUE CLUSTERED INDEX vIdx ON test2_v(id) 

I have error with this code:

Cannot create index on view 'test.dbo.test2_v' because its select list does not include a proper use of COUNT_BIG. Consider adding COUNT_BIG(*) to select list.

I can create view like this:

CREATE VIEW dbo.test2_v WITH SCHEMABINDING      AS     SELECT id, SUM(amount) as amount, COUNT_BIG(*) as tmp     FROM dbo.test2      GROUP BY id 

But I'm just wondering what is purpose of this column in this case?

like image 883
ceth Avatar asked May 17 '11 11:05

ceth


People also ask

Can we create non clustered index on view?

Also, it is possible to create non-clustered indexes on a view, providing more possibilities to enhance the queries calling the view.

Can we do indexing on views?

Indexes can only be created on views which have the same owner as the referenced table or tables. This is also called an intact ownership-chain between the view and the table(s). Typically, when table and view reside within the same schema, the same schema-owner applies to all objects within the schema.

What is the difference between view and indexed view?

An indexed view has a unique clustered index. The unique clustered index is stored in SQL Server and updated like any other clustered index. An indexed view is more significant compared to standard views that involve complex processing of large numbers of rows, such as aggregating lots of data, or joining many rows.

What is indexed view explain in short?

What does Indexed view mean? This is also a simple view which has a unique clustered index defined on it. When a clustered index is created on a view, the result set is stored in the database just like a table with a clustered index.


1 Answers

You need COUNT_BIG in this case because of the fact you are using GROUP BY.

This is one of many limitations of Indexed Views and because of these restrictions, Indexed Views can't be used in many places or the usage of it is NOT as effective as it could have been. Unfortunately, it is how it works currently. Sucks, it narrows the scope of usage.

http://technet.microsoft.com/en-us/library/cc917715.aspx

like image 156
Sankar Reddy Avatar answered Sep 22 '22 04:09

Sankar Reddy