2010-09-03 44 views
1

SO真的有兩個部分的問題:全文索引+連接+ linq:如何正確執行此操作?

1)如何使用CONTAINSTABLE()與一堆內部連接?

假設有兩個表:

Item (ItemID, CategoryID, Name) 
Category (CategoryID, ParentCategoryID, Name) 

使用 「啞巴」 like搜索這看起來是這樣的:

create view [db].[vItem] 
as 
select i.*, c1.Name as SubCategory, c2.Name as Category, c3.Name as Department 
from Item i 
inner join category c1 on(c1.CategoryID=i.CategoryID) 
inner join category c2 on(c1.ParentCategoryID=c2.CategoryID) 
inner join category c3 on(c2.ParentCategoryID=c3.CategoryID) 

然後執行:

select * 
from vItem 
where (Name like '%[word 1]%' or SubCategory like '%[word 1]%' or Category like '%[word 1]%' or Department like '%[word 1]%') [and ... word n] 

我可能下降的觀點內部連接CONTAINSTABLE() 4次,但那是a)fugly b)不一樣的東西(e ach關鍵表將需要具有所有搜索關鍵詞以獲得任何結果)

2)如何在查詢語言中使用上述所有內容。 再次,假設我只使用like方法 - 它非常簡單。

from i in db.VItem 
where i.Name.Contains("word 1") ... 

如何用全文搜索做到這一點?

回答

1

對於第1部分):創建視圖作爲indexed view

create unique clustered index vItem_ItemID 
    on db.vItem(ItemID) 

然後在該視圖上,而不是基表的列create the fulltext index

create fulltext index on db.vItem 
    (Name, SubCategory, Category, Department) 
    key index vItem_ItemID 

一旦這樣創建的,您可以:

select v.* 
    from containstable(db.vItem, *, '"word 1"') ct 
     inner join db.vItem v 
      on ct.[key] = v.ItemID 

我將不得不離開別人第2部分),因爲我沒有什麼經驗的LINQ。

+0

所以我不能使用已經在Item和Category表上的全文索引? – 2010-09-03 19:29:06

+0

你可以但它會是你所說的「fugly」技術。我認爲從可讀性和性能的角度來看,這是最好的。 – 2010-09-03 19:30:31