在 Sql Server 2008 中为视图建立索引实际上会复制原始数据吗?

发布于 2024-09-18 04:23:47 字数 89 浏览 10 评论 0原文

如果我创建索引视图(在 Sql Server 2008 中),这是否意味着我将所有必需的数据从源表复制到单独的新表中?或者只保存一些微小的指针/索引来表示这个视图?

If i create an Indexed View (in Sql Server 2008), does this mean i copy all required the data from the source tables into a separate new table? Or are only some tiny pointers/indexes saved, to represent this view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分开我的手 2024-09-25 04:23:47

是的,数据是单独复制和存储的,因此如果您修改基础表,您的索引视图将自动更新。 这会导致大量的锁争用。此外,索引视图可能会变得比基础表更大并变得适得其反。

Yes, the data is copied and stored separately, so if you modify the underlying table, your indexed view will update automatically. This causes a lot of lock contention. Also the indexed view may grow larger than the underlying tables and become counterproductive.

变身佩奇 2024-09-25 04:23:47

是的,数据被复制了。其他数据库平台(例如 Oracle)将此称为物化视图,因为数据将物质化为物理形式。

Yes, the data is copied. Other database platforms such as Oracle refer to this as a Materialized View because the data will materialize into a physical form.

小巷里的女流氓 2024-09-25 04:23:47

当在视图上创建唯一聚集索引时,结果集将存储在数据库中,就像存储具有聚集索引的表一样。

当对基表中的数据进行修改时,数据修改也会反映出来在索引视图中存储的数据

~来自msdn

When a unique clustered index is created on a view, the result set is stored in the database just like a table with a clustered index is stored.

As modifications are made to the data in the base tables, the data modifications are reflected in the data stored in the indexed view

~ from msdn

苍白女子 2024-09-25 04:23:47

您可以使用(未​​记录但广泛使用的)DBCC PAGE 命令来准确查看存储的内容。下面将创建一个索引视图并打印第一个数据页的内容。

SET NOCOUNT ON
IF OBJECT_ID('tempdb..#dbcc_ind') IS NOT NULL
    TRUNCATE TABLE #dbcc_ind
ELSE
    CREATE TABLE #dbcc_ind
    (PageFID  TINYINT, 
    PagePID INT,   
    IAMFID   TINYINT, 
    IAMPID  INT, 
    ObjectID  INT,
    IndexID  TINYINT,
    PartitionNumber TINYINT,
    PartitionID BIGINT,
    iam_chain_type  VARCHAR(30),    
    PageType  TINYINT, 
    IndexLevel  TINYINT,
    NextPageFID  TINYINT,
    NextPagePID  INT,
    PrevPageFID  TINYINT,
    PrevPagePID INT, 
    PRIMARY KEY (PageFID, PagePID));

IF OBJECT_ID('dbo.vtest') IS NULL
CREATE TABLE dbo.vtest (
i INT IDENTITY(1,1)  NOT NULL PRIMARY KEY,
c1 CHAR(500) NOT NULL DEFAULT REPLICATE('x',500), 
c2 CHAR(500) NOT NULL DEFAULT REPLICATE('y',500) 
)
GO

INSERT INTO dbo.vtest DEFAULT VALUES
GO 10

IF OBJECT_ID('dbo.ixViewTest') IS NULL
BEGIN
EXEC('CREATE VIEW dbo.ixViewTest
WITH SCHEMABINDING
AS
SELECT i,c1,c2
FROM dbo.vtest;')
EXEC('CREATE UNIQUE CLUSTERED INDEX [cix] ON [dbo].[ixViewTest] ([i] ASC)')
END
GO


DECLARE @command VARCHAR(1000)
SET @command = 'DBCC IND(' + QUOTENAME(DB_NAME()) + ', ixViewTest,1) WITH NO_INFOMSGS;'

INSERT INTO #dbcc_ind
    EXEC ( @command );


SELECT @command= 'DBCC PAGE (' + QUOTENAME(DB_NAME()) + ',' + CAST(PageFID AS VARCHAR(5)) + ',' + CAST(PagePID AS VARCHAR(10)) + ',1) ;'
FROM #dbcc_ind
WHERE PageType=1
 AND PrevPagePID=0


DBCC TRACEON(3604)
EXEC ( @command )
DBCC TRACEOFF(3604)

You can use the (undocumented but widely used) DBCC PAGE command to see exactly what is stored. The following will create an indexed view and print the contents of the first data page.

SET NOCOUNT ON
IF OBJECT_ID('tempdb..#dbcc_ind') IS NOT NULL
    TRUNCATE TABLE #dbcc_ind
ELSE
    CREATE TABLE #dbcc_ind
    (PageFID  TINYINT, 
    PagePID INT,   
    IAMFID   TINYINT, 
    IAMPID  INT, 
    ObjectID  INT,
    IndexID  TINYINT,
    PartitionNumber TINYINT,
    PartitionID BIGINT,
    iam_chain_type  VARCHAR(30),    
    PageType  TINYINT, 
    IndexLevel  TINYINT,
    NextPageFID  TINYINT,
    NextPagePID  INT,
    PrevPageFID  TINYINT,
    PrevPagePID INT, 
    PRIMARY KEY (PageFID, PagePID));

IF OBJECT_ID('dbo.vtest') IS NULL
CREATE TABLE dbo.vtest (
i INT IDENTITY(1,1)  NOT NULL PRIMARY KEY,
c1 CHAR(500) NOT NULL DEFAULT REPLICATE('x',500), 
c2 CHAR(500) NOT NULL DEFAULT REPLICATE('y',500) 
)
GO

INSERT INTO dbo.vtest DEFAULT VALUES
GO 10

IF OBJECT_ID('dbo.ixViewTest') IS NULL
BEGIN
EXEC('CREATE VIEW dbo.ixViewTest
WITH SCHEMABINDING
AS
SELECT i,c1,c2
FROM dbo.vtest;')
EXEC('CREATE UNIQUE CLUSTERED INDEX [cix] ON [dbo].[ixViewTest] ([i] ASC)')
END
GO


DECLARE @command VARCHAR(1000)
SET @command = 'DBCC IND(' + QUOTENAME(DB_NAME()) + ', ixViewTest,1) WITH NO_INFOMSGS;'

INSERT INTO #dbcc_ind
    EXEC ( @command );


SELECT @command= 'DBCC PAGE (' + QUOTENAME(DB_NAME()) + ',' + CAST(PageFID AS VARCHAR(5)) + ',' + CAST(PagePID AS VARCHAR(10)) + ',1) ;'
FROM #dbcc_ind
WHERE PageType=1
 AND PrevPagePID=0


DBCC TRACEON(3604)
EXEC ( @command )
DBCC TRACEOFF(3604)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文