从视图中删除子查询以使其成为索引视图

remove subquery from view to make it Indexed view(从视图中删除子查询以使其成为索引视图)
本文介绍了从视图中删除子查询以使其成为索引视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想为全文搜索创建一个索引视图.

i want to create a Index View for full text search.

我面临子查询的唯一问题,因为索引视图不允许子查询.

the only problem i,m facing with subquery, because index views does not allow subquery.

下面是我的查询

ALTER VIEW [dbo].[Demo] with SCHEMABINDING  AS
select distinct a.ID,a.Title, a.Description ,b.Name as Recipe, c.Name as Taste , d.Name as CuisineType,
STUFF((SELECT ',' + Name FROM dbo.Ingredients where ID in (select IngredientID from dbo.listingIngredients 
where listingid = a.ID ) FOR XML PATH('')), 1, 1, '') as Ingredients
from dbo.Listing as a 
inner join dbo.RecipeType b on a.RecipeTypeID = b.ID
inner join dbo.taste c on a.tasteID = c.ID
inner join dbo.CuisineType d on a.CuisineTypeID = d.ID
inner join dbo.listingIngredients e on a.ID = e.listingID
GO

我使用子查询从使用 STUFF 的成分表中获取成分作为连接字符串.

I,m using subquery to get ingredients as concatenate string from Ingredients table using STUFF.

有人可以让我知道如何删除此子查询并将成分作为内容字符串.

can some one please let me know how can i remove this subquery and have ingredients as contented string.

请告诉我

问候男子气概

推荐答案

查询的 XML 部分会导致问题,即使您确实设法删除了子选择.

The XML part of the query will cause problems, even if you did manage to remove the sub-selected.

然而,一切都没有丢失.您可以将视图重写为可以编入索引的部分和另一个更便宜但不能编入索引的部分.例如,您可以这样写:

However, all is not lost. You could rewrite the view into a part that can be indexed and another part that is cheaper, but can't. For example, you could write:

ALTER VIEW [dbo].[Demo_Part] with SCHEMABINDING  AS
select a.ID,a.Title
, a.Description 
, b.Name as Recipe
, c.Name as Taste 
, d.Name as CuisineType
, e.name
from dbo.Listing as a 
inner join dbo.RecipeType b on a.RecipeTypeID = b.ID
inner join dbo.taste c on a.tasteID = c.ID
inner join dbo.CuisineType d on a.CuisineTypeID = d.ID
inner join dbo.listingIngredients e on a.ID = e.listingID
GROUP BY a.ID,a.Title
, a.Description 
, b.Name as Recipe
, c.Name as Taste 
, d.Name as CuisineType
, e.name

根据您的数据模型,您甚至可能不需要 group by.此视图可以编入索引

Depending on your data model, you may not even need the group by. This view can be indexed

然后编写另一个未编入索引但替换原始视图的视图

And then write another view that is not indexed, but which replaces your original view

CREATE VIEW [dbo].[Demo]
SELECT ...
 STUFF (...)
FROM [dbo].[Demo_Part]

作为一个元答案,我想补充一点,如果您需要索引这样的视图(并使用 DISTINCT),很可能您的数据建模者在数据模型上犯了很大的错误,或者您的数据访问代码是非常低效.关于这一切的一切都像是在努力解决糟糕的编码和建模实践.

As a meta-answer I would add that if you need to index a view like this (and use the DISTINCT), chances are that your data modeller made a pretty big mistake with the data model or that your data access code is very inefficient. Everything about this smells like you are trying to work around poor coding and modelling practices.

这篇关于从视图中删除子查询以使其成为索引视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending parameters to stored procedures vb.net(将参数发送到存储过程 vb.net)
Insert multiple rows, count based on another table columns(插入多行,根据另一个表列计数)
How to hold the location of an image in a SQL Server database?(如何在 SQL Server 数据库中保存图像的位置?)
How to send to email (outlook) the selected items in SQL Server database using vb.net(如何使用 vb.net 将 SQL Server 数据库中的选定项目发送到电子邮件(Outlook))
datagrid checkbox writes Null instead of 0 (false) into database(datagrid 复选框将 Null 而不是 0 (false) 写入数据库)
DBCC CheckDb-any ways to detect errors vb.net?(DBCC CheckDb-vb.net 有什么检测错误的方法吗?)