Feeds:
Posts
Comments

Archive for the ‘SQL View’ Category

My very good friend, Jacob Sebastian, is running a month-long SQL Quiz Series where the best-of-the-best experts from around the globe would be the quiz masters. They will ask one question every day, and users are expected to answer them correctly. The winning prizes include cool gadgets like iPAD, Kindle and many more.

I am one of the quiz masters, and my question is published here: The View, The Table and The Clustered Index Confusion. I have asked there three questions.

Q1. Does the table use an index created on itself?

Q2. Does the view use an index created on itself?

Q3. Do both queries use the same index? If yes, why? If no, why not?

Today is the last day to participate in the SQL Quiz.

I have already written the answer as a hint over here: SQL SERVER – Indexed View always Use Index on Index

Additionally, if above problem interest you, I suggest you read my article series on limitations of the view SQL SERVER – The Limitations of the Views – Eleven and more…

Reference: Pinal Dave (http://blog.SQLAuthority.com)

About these ads

Read Full Post »

My earlier article SQL SERVER – The Limitations of the Views – Eleven and more… has lots of popularity and I have been asked many questions on the view. Many emails I received suggesting that they have hundreds of the view and now have no clue what is going on and how many of them have indexes and how many does not have index. Some even asked me if there is any way they can get a list of the views with the property of Index along with it.

Here is the quick script which does exactly the same. You can also include many other columns from the same view.

SELECT SCHEMA_NAME(schema_id) AS schema_name
,name AS view_name
,OBJECTPROPERTYEX(OBJECT_ID,'IsIndexed') AS IsIndexed
,OBJECTPROPERTYEX(OBJECT_ID,'IsIndexable') AS IsIndexable
--,*
FROM sys.views;
GO

You can run above query to see if view is indexed or created with SCHEAMABINDING.

Do you use Views, if yes, you need to read SQL SERVER – The Limitations of the Views – Eleven and more…

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Read Full Post »

This blog post is written in response to T-SQL Tuesday hosted by Shankar Reddy.

I have been recently writing about Views and their Limitations. While writing this article series, I got inspired to write about SQL Server Quiz Questions. You can view the Quiz Question posted over here.

In SQL Server 2005, a single table can have maximum 249 non clustered indexes and 1 clustered index. In SQL Server 2008, a single table can have maximum 999 non clustered indexes and 1 clustered index. It is widely believed that a table can have only 1 clustered index, and this belief is true. I have some questions for all of you. Let us assume that I am creating view from the table itself and then create a clustered index on it. In my view, I am selecting the complete table itself.

USE tempdb
GO
-- Create sample Table
CREATE TABLE mySampleTable(ID1 INT, ID2 INT, SomeData VARCHAR(100))
INSERT INTO mySampleTable(ID1,ID2,SomeData)
SELECT TOP 1000 ROW_NUMBER()OVER (ORDER BY o1.name),
ROW_NUMBER()OVER (ORDER BY o1.name DESC),
o1.name
FROM sys.all_objects o1
CROSS JOIN sys.all_objects o2
GO
-- Create Index on Table
CREATE UNIQUE CLUSTERED INDEX [IX_SampleView] ON mySampleTable
(
ID1 ASC
)
GO
-- Create sample View
CREATE VIEW SampleView
WITH SCHEMABINDING
AS
SELECT
ID1,ID2,SomeData
FROM dbo.mySampleTable
GO
-- Create Index on View
CREATE UNIQUE CLUSTERED INDEX [IX_ViewSample] ON [dbo].[SampleView]
(
ID2 ASC
)
GO
-- Enable Execution Plan using CTRL + M
SELECT ID1,ID2,SomeData
FROM mySampleTable
GO
SELECT ID1,ID2,SomeData
FROM SampleView
GO
-- Clean up
DROP VIEW SampleView
DROP TABLE mySampleTable
GO

Now run the following script and answer these questions:

Q1. Does the table use an index created on itself?
Q2. Does the view use an index created on itself?
Q3. Do both the queries use the same index? If yes, why? If no, why not?

The answers are very clear.

The answers are very clear.

A1: Yes
A2: No
A3: Read the rest of the blog! or visit BeyondRelationa.com (where you can win iPAD as well).

Usually, the assumption is that Index on table will use Index on table and Index on view will be used by view. However, that is the misconception. It does not happen this way. In fact, if you notice the image, you will find the both of them (table and view) use both the index created on the table. The index created on the view is not used. The reason for the same as listed in BOL.

The cost of using the indexed view may exceed the cost of getting the data from the base tables, or the query is so simple that a query against the base tables is fast and easy to find. This often happens when the indexed view is defined on small tables. You can use the NOEXPAND hint if you want to force the query processor to use the indexed view. This may require you to rewrite your query if you don’t initially reference the view explicitly. You can get the actual cost of the query with NOEXPAND and compare it to the actual cost of the query plan that doesn’t reference the view. If they are close, this may give you the confidence that the decision of whether or not to use the indexed view doesn’t matter.

Various answers to this question are given over here.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Read Full Post »

My very good friend, Jacob Sebastian, is running a month-long SQL Quiz Series where the best-of-the-best experts from around the globe would be the quiz masters. They will ask one question every day, and users are expected to answer them correctly. The winning prizes include cool gadgets like iPAD, Kindle and many more.

I am one of the quiz masters, and my question is published here: The View, The Table and The Clustered Index Confusion. I have asked there three questions. However, the real important question is:

Bonus Question: Does this mean that my table has two effective clustered indexes now?

The best answer to this question will be featured on this blog with due credit. If you have not answered the question yet, here is a hint for you:

In the SQL Server 2008 enterprise version, there is an additional feature where the Index View somehow started to become very helpful to parents object (table enclosed in the View).

However, the question can be answered evaluated in any version of SQL Server. The winner will be judged by the version number and answer. I have carefully picked the question so those who have different versions will have different answers.

What’s your answer then?

If you are interested in reading more about Views here is my quick blog post which discusses my recent articles on limitations of the views.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Read Full Post »

Since I started the monthly round up of the blog post, I have received many positive feedback. I plan to continue doing this month refresher every month now. This rounds ups are my mirror and informs me what I have been doing whole month. Here is quick look at the last month.

The month started very interesting with my daughter’s birthday SQLAuthority News – Fathers and Daughters. As this was very first birthday it was very special for me. I had great time enjoying with her quality time and it was all fun. I am an MVP and I am one proud one. I think MVP demonstrates community dedication and extreme passionate for community, I explained my understanding in the article SQLAuthority News – What is an MVP? – How to become an MVP?.

There was very interesting conversation on the subject of Soft-Delete SQL SERVER – Soft Delete – IsDelete Column – Your Opinion and really think community participated very well on the subject. I am also involved in training very unique fast track data ware house course and I have made few notes over here SQL SERVER – Few Notes on Fast Track Data Warehouse.

Not everything was as great as I expected during the whole month, I was really abused by Indian Airlines Spicejet I finally wrote about their harassment over SQLAuthority News – Spicejet Complaint – Update – No Outcome.If you want to read complete story, you can read my earlier article on the same subject SQLAuthority News – Spicejet Complain – Do Not Fly with Spicejet.

All DBA face common problems when their log file grow too big, I tried to answer the question in article SQL SERVER – How to Stop Growing Log File Too Big.

How can I not write on my favorite subject of indexing in any month.

I have also written series of limitation of the view. Here is the complete series.

I would like to request your feedback on the limitation of the view blog post series.

Reference: Pinal Dave (http://blog.SQLAuthority.com)

Read Full Post »

« Newer Posts - Older Posts »