|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, August 06, 2012 11:00 PM
Points: 219,
Visits: 82
|
|
Hi All
We need to create a clustered index by implementing schema binding in the view. As you know it throws error. Could anyone possibly tell me to achieve in alternate way?
Thanks in advance Vin
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 12:00 PM
Points: 2,988,
Visits: 4,412
|
|
vinothraj (9/23/2010) We need to create a clustered index by implementing schema binding in the view. As you know it throws error. How could I know? ... post shows nothing. Would you mind in posting a test case showing the issue?
_____________________________________ Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, August 06, 2012 11:00 PM
Points: 219,
Visits: 82
|
|
Thanks for the reply...
For example i've two databases called "dbFirst" and "dbSecond".
In dbSecond i've a table called "EmployeeTable".
I'm going to create a view called "EmployeeView" in dbFirst database with
Select EmployeeNumber, EmployeeName From dbSecond.dbo.EmployeeTable
I want to create a index (either clustered or non-clustered) for this view as my view containing millions of records and i dont want to wait for the retrieval. If i want to create index for this view which is located in dbFirst referring dbSecond, i'm unable to do. Could you suggest me whether there is any alternate way to achieve this?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 12:00 PM
Points: 2,988,
Visits: 4,412
|
|
vinothraj (9/23/2010) Select EmployeeNumber, EmployeeName From dbSecond.dbo.EmployeeTable
I want to create a index (either clustered or non-clustered) for this view as my view containing millions of records and i dont want to wait for the retrieval. If i want to create index for this view which is located in dbFirst referring dbSecond, i'm unable to do. Could you suggest me whether there is any alternate way to achieve this?
First, a view is an abstraction not a physical object meaning a view is just a stored query therefore it can contain nothing.
SQL Server engine takes advantage of the indexes pointing to the base table/s of the view.
You can see it by yourself doing this simple test.
1- Go to Northwind and create a view pointing to Pubs database like...
USE Northwind go SET ANSI_NULLS,QUOTED_IDENTIFIER ON go CREATE VIEW dbo.MyEmployeeView AS SELECT * FROM pubs.dbo.employee WHERE emp_id = 'JYL26161F' 2- Check the execution plan of query below...
USE Northwind go select * from dbo.MyEmployeeView ... which should show something like...
StmtText StmtId NodeId Parent PhysicalOp LogicalOp Argument DefinedValues EstimateRows EstimateIO EstimateCPU AvgRowSize TotalSubtreeCost OutputList Warnings Type Parallel EstimateExecutions select * from dbo.MyEmployeeView 1 1 0 [NULL] [NULL] 1 [NULL] 1 [NULL] [NULL] [NULL] 0.0126592 [NULL] [NULL] SELECT 0 [NULL] |--Bookmark Lookup(BOOKMARK [Bmk1000]), OBJECT [pubs].[dbo].[employee])) 1 3 1 Bookmark Lookup Bookmark Lookup BOOKMARK [Bmk1000]), OBJECT [pubs].[dbo].[employee]) [employee].[hire_date], [employee].[pub_id], [employee].[job_lvl], [employee].[job_id], [employee].[lname], [employee].[minit], [employee].[fname], [employee].[emp_id] 1 0.00625 1.1e-006 49 0.0126592 [employee].[hire_date], [employee].[pub_id], [employee].[job_lvl], [employee].[job_id], [employee].[lname], [employee].[minit], [employee].[fname], [employee].[emp_id] [NULL] PLAN_ROW 0 1 |--Index Seek(OBJECT [pubs].[dbo].[employee].[PK_emp_id]), SEEK [employee].[emp_id]='JYL26161F') ORDERED FORWARD) 1 4 3 Index Seek Index Seek OBJECT [pubs].[dbo].[employee].[PK_emp_id]), SEEK [employee].[emp_id]='JYL26161F') ORDERED FORWARD [Bmk1000] 1 0.0063285 7.96e-005 53 0.0064081 [Bmk1000] [NULL] PLAN_ROW 0 1
... it does takes advantage of existing base table indexes, doesn't it?
_____________________________________ Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, August 06, 2012 11:00 PM
Points: 219,
Visits: 82
|
|
| I've already used like that, but i need it across databases.
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 11:20 AM
Points: 31,437,
Visits: 13,752
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 12:00 PM
Points: 2,988,
Visits: 4,412
|
|
vinothraj (9/29/2010) I've already used like that, but i need it across databases.
Please note that my example has query and view in database "A" while base tables of the view are in database "B".
Would you mind in elaborating about "across databases"?
_____________________________________ Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Monday, August 06, 2012 11:00 PM
Points: 219,
Visits: 82
|
|
| thanks buddy. you are correct its across databases, where as i need schema binding in view not in table which is my original query.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 12:00 PM
Points: 2,988,
Visits: 4,412
|
|
vinothraj (9/23/2010) We need to create a clustered index by implementing schema binding in the view. Lets start over?
Post is kind of confusing.
1- Clustered indexes have their leaf level inside the target table therefore it is hard to understand how "clustered index" and "view" got into the same phrase.
2- schemabinding option creates a schema - schema as in DDL - dependency in between the new view and the base tables. Again, not sure how "clustered index" made it to this phrase.
_____________________________________ Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 2:44 PM
Points: 88,
Visits: 171
|
|
While reading this post, I believe I have a similar question. There are tables that exist in dbA and dbB. I wish to create an fast view in dbC, as in the example below:
USE [Onyx_support] GO
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[vIncident_Audit_Log]')) DROP VIEW [dbo].[vIncident_Audit_Log] GO
SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
CREATE VIEW [dbo].[vIncident_Audit_Log] WITH SCHEMABINDING AS SELECT incident_audit_log_id, site_id, incident_type_did, incident_category_did, incident_product_code, incident_id, secondary_id, assigned_to, labor, time, primary_contact_id, primary_contact_type_enum, assigned_id, tracking_code_did, serial_number, desc1, desc2, source_did, status_did, resolution_did1, resolution_did2, resolution_did3, resolution_did4, total_time, total_labor, image, read_only_access, private_access, reminder_date, user1, user2, user3, user4, user5, user6, user7, user8, user9, user10, insert_by, insert_date, update_by, update_date, onyx_timestamp, delete_status, central_office, cs_source_description, cs_proposal_date, cs_projected_close_month, cs_contract_expire_date, cs_current_provider, cs_competitor, cs_competitor_description, cs_pots_ale, cs_t1_ale, cs_mrr, cs_nrr, cs_hi_cap_ale, cs_phone_system_type, cs_bts_nrr FROM onyx.dbo.incident_audit_log WHERE (insert_date > DATEADD(m, - 2, GETDATE()))
when I try to run the above code, I get the error below:
Cannot schema bind view 'dbo.vIncident_Audit_Log' because name 'onyx..incident_audit_log' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.
Is there a way for me to create an indexable view on a column that is not indexed in the Onyx db? They are proprietary database tables that are running too slow in a query.
TIA Andre
|
|
|
|