CREATE TABLE Storage
(
ID INT IDENTITY(1,1) PRIMARY KEY,
data XML NOT NULL
)
GO
INSERT INTO Storage(data)
VALUES('
<footballteams>
<team manager="Benitez">
<name>Liverpool</name>
<ground>Anfield</ground>
</team>
<team manager="Mourinho">
<name>Chelsea</name>
<ground>Stamford Bridge</ground>
</team>
<team manager="Wenger">
<name>Arsenal</name>
<ground>Highbury</ground>
</team>
</footballteams>');
----------------------------------------
GO
CREATE VIEW FootballView WITH SCHEMABINDING AS
(
SELECT
TeamName = Team.TeamNode.value('(name)[1]', 'varchar(100)'),
Manager = Team.TeamNode.value('(@manager)', 'varchar(100)'),
Ground = Team.TeamNode.value('(ground)[1]', 'varchar(100)')
FROM
dbo.Storage S
CROSS APPLY DATA.nodes('/footballteams') AS Teams(TeamsNode)
CROSS APPLY data.nodes('/footballteams/team') AS Team(TeamNode)
)
GO
CREATE UNIQUE CLUSTERED INDEX TeamNameInd ON FootballView(TeamName)
-
Error Message: Cannot create index on view "CF.dbo.FootballView" because it contains an APPLY. Consider not indexing the view, or removing APPLY.
我意识到,事实上,不能为此视图创建索引,因为使用了 CROSS APPLY。任何人都可以为此提出解决方法吗?因为在处理大量 XML 数据时,使用这种没有索引的视图太慢了。
编辑:
有什么办法可以索引 XML 本身吗?