Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > microsoft.public.sqlserver.programming > #31348

Indexing for View Containing UNION

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Michael Cole <micheal.cole@invalid.com>
Newsgroups microsoft.public.sqlserver.programming
Subject Indexing for View Containing UNION
Date Mon, 24 Jun 2019 18:05:40 +1000
Organization A noiseless patient Spider
Lines 88
Message-ID <qeq08n$q9s$1@dont-email.me> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset="iso-8859-15"; format=flowed
Content-Transfer-Encoding 8bit
Injection-Date Mon, 24 Jun 2019 08:05:43 -0000 (UTC)
Injection-Info reader02.eternal-september.org; posting-host="a6431ee7848a4b59dfc88ef10c5f0a7e"; logging-data="26940"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/bSn7kaS8rHgZdlavfZIw"
Cancel-Lock sha1:huKKKr0/2ucZZrnK8aQ27M/YM8s=
X-Newsreader MesNews/1.08.06.00-gb
X-ICQ 1701145376
Xref csiph.com microsoft.public.sqlserver.programming:31348

Show key headers only | View raw


Basic outline is that I wish to ensure uniqueness of data within a 
table where some of the data can be considered as "shared".  Sample 
data and code: -

[code]

CREATE TABLE [dbo].[TestSplitOwnership](
	[Item] [int] NOT NULL,
	[Owner] [char](1) NOT NULL,
 CONSTRAINT [PK_TestSplitOwnership] PRIMARY KEY CLUSTERED
(
	[Item] ASC,
	[Owner] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = 
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (1, N'C')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (2, N'C')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (3, N'C')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (4, N'C')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (11, N'1')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (11, N'2')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (11, N'3')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (12, N'1')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (12, N'2')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (13, N'1')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (14, N'2')
GO
INSERT [dbo].[TestSplitOwnership] ([Item], [Owner]) VALUES (15, N'3')
GO

DROP VIEW [dbo].[vwTestSplitOwnership];
GO

CREATE VIEW [dbo].[vwTestSplitOwnership]
WITH SCHEMABINDING
AS
SELECT [TestSplitOwnership].[Item],
       [TestSplitOwnership].[Owner]
FROM   [dbo].[TestSplitOwnership]
WHERE  [Owner] <> 'C'
UNION ALL
SELECT [TestSplitOwnership].[Item],
       A.[Owner]
FROM   [dbo].[TestSplitOwnership]
       CROSS JOIN (
                      SELECT DISTINCT [Owner]
                      FROM   [dbo].[TestSplitOwnership]
                      WHERE  [Owner] <> 'C'
                  ) AS A
WHERE  [TestSplitOwnership].[Owner] = 'C';
GO

CREATE UNIQUE CLUSTERED INDEX [PK_TestSplitOwnership]
ON [dbo].[vwTestSplitOwnership] (
                              [Item] ASC,
                              [Owner] ASC
                          )
WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = 
OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON )
ON [PRIMARY];
GO


[/code]



The intent is "C" is not an actual owner - that all the records with 
"C" as their owner are effectively shared for all other owners, and 
this we need to ensure uniqueness of ownership based on the query in 
the view, e.g., any "owned" items can not be the same as a "shared" 
item.  Issue is that I cannot put a PK on the view as it contains the 
UNION.I cannot change the data structure, but any suggestions as to how 
I could implement such a constraint would be welcome.

Back to microsoft.public.sqlserver.programming | Previous | NextNext in thread | Find similar


Thread

Indexing for View Containing UNION Michael Cole <micheal.cole@invalid.com> - 2019-06-24 18:05 +1000
  Re: Indexing for View Containing UNION Erland Sommarskog <esquel@sommarskog.se> - 2019-06-25 21:05 +0200

csiph-web