Q1 - Why is the simple ID1 clustered index (PK in Test1T) & regurgitated in the view (dbo.vw_TestIndexView) not indexable?
Q2 - A Non-Persisted Calc'd Col (in tbl) needs meet criteria below in order to be indexable as a non-clustered index; assuming true for the Total Calc'd Col:
a - Deterministic - meets criteria
b - Precise - it fails; despite being cast to decimal (38,26) everywhere. To avoid overflow on multiplaction the result of the calculation is cast to (38,26) & nested in a round to 36dp. Regardless if I was using different scaled data-types this would be taken care of in casting the calc'd column.
___________________________________________________________________________________________________________________________________________________
Trying to use the priniciples of a Computed Col In Tbl Redgate Tut by Robert Sheldon to an Indexed View (basic db attached containing only items mentioned) & creation code at very bottom to produce Calc'd Col (Total).
Objects
• dbo.Test1T
• dbo.Test2T
• dbo.vwTestIndexView
Indexed View Criteria
Results
___________________________________________________________________________________________________________________________________________________
Creation Code: Below Irrelevant If Opening Db
Create Tables
Create View
Create Indexes On View
Test Column Criteria To Meet Indexable View Criteria
Q2 - A Non-Persisted Calc'd Col (in tbl) needs meet criteria below in order to be indexable as a non-clustered index; assuming true for the Total Calc'd Col:
a - Deterministic - meets criteria
b - Precise - it fails; despite being cast to decimal (38,26) everywhere. To avoid overflow on multiplaction the result of the calculation is cast to (38,26) & nested in a round to 36dp. Regardless if I was using different scaled data-types this would be taken care of in casting the calc'd column.
___________________________________________________________________________________________________________________________________________________
Trying to use the priniciples of a Computed Col In Tbl Redgate Tut by Robert Sheldon to an Indexed View (basic db attached containing only items mentioned) & creation code at very bottom to produce Calc'd Col (Total).
Objects
• dbo.Test1T
• dbo.Test2T
• dbo.vwTestIndexView
Indexed View Criteria
Results
___________________________________________________________________________________________________________________________________________________
Creation Code: Below Irrelevant If Opening Db
Create Tables
Code:
USE Test
-- Create 1st Tbl
IF OBJECT_ID('dbo.Test1T','Table') IS NOT NULL
DROP TABLE dbo.Test1T
GO
CREATE TABLE dbo.Test1T (
ID1 int NOT NULL PRIMARY KEY
,NrOrdered DECIMAL (38,26)
);
GO
INSERT INTO dbo.Test1T (ID1, NrOrdered)
VALUES (1,5),
(2,9);
-- Create 2nd Tbl
GO
IF OBJECT_ID('dbo.Test2T','Table') IS NOT NULL
DROP TABLE dbo.Test2T
GO
CREATE TABLE dbo.Test2T (
ID2 int NOT NULL PRIMARY KEY
,ID1_FK int FOREIGN KEY REFERENCES dbo.Test1T(ID1)
,Price DECIMAL (38,26)
);
GO
INSERT INTO dbo.Test2T (ID2, ID1_FK, Price)
VALUES (1,1,10),
(2,2,3);
Create View
Code:
USE [Test]
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_PADDING,
ANSI_WARNINGS,
CONCAT_NULL_YIELDS_NULL,
ARITHABORT,
QUOTED_IDENTIFIER,
ANSI_NULLS ON;
GO
-- Del view if exists
IF OBJECT_ID('dbo.vw_TestIndexView', 'view') IS NOT NULL
DROP VIEW dbo.vw_TestIndexView;
--Create view with calc'd col
GO
CREATE VIEW dbo.vw_TestIndexView
WITH SCHEMABINDING
AS
SELECT
dbo.Test1T.ID1
,dbo.Test1T.NrOrdered
,dbo.Test2T.Price
,ROUND(CAST(dbo.Test1T.NrOrdered * dbo.Test2T.Price AS DECIMAL(38,26)),38) AS TOTAL
FROM dbo.Test1T
INNER JOIN dbo.Test2T ON dbo.Test1T.ID1 = dbo.Test2T.ID1_FK;
Create Indexes On View
Code:
CREATE UNIQUE CLUSTERED INDEX idx_TestID1 ON dbo.vw_TestIndexView (
ID1
);
GO
CREATE NONCLUSTERED INDEX idx_Total ON dbo.vw_TestIndexView (
Total
);
Test Column Criteria To Meet Indexable View Criteria
Code:
DECLARE @id int = OBJECT_ID('dbo.vw_TestIndexView')
SELECT
COLUMNPROPERTY(@id, 'ID1', 'Indexable') AS 'ID1-Indexable',
COLUMNPROPERTY(@id, 'ID1', 'Precise') AS 'ID1-Precise',
COLUMNPROPERTY(@id, 'NrOrdered', 'Precise') AS 'NrOrdered-Precise',
COLUMNPROPERTY(@id, 'Total', 'IsDeterministic') AS 'Total-Deterministic',
COLUMNPROPERTY(@id, 'Total', 'Precise') AS 'Total-Precise';