我有两个表:FreeText的统计查询是超级慢
**Product**
ID
Name
SKU
**Brand**
ID
Name
产品表中有大约120K记录 品牌表有30K记录
我需要找到的所有的产品数量与名称和品牌匹配一个特定的关键字。
我用自由文本 '包含' 这样的:
SELECT count(*)
FROM Product
inner join Brand
on Product.BrandID = Brand.ID
WHERE (contains(Product.Name, 'pants')
or
contains(Brand.Name, 'pants'))
这个查询大约需要17秒。 我在运行此查询之前重建了FreeText索引。
如果我只检查Product.Name。他们的查询少于1秒。同样,如果我只检查品牌名称。如果我使用OR条件,则会发生该问题。
如果我切换查询中使用,如:
SELECT count(*)
FROM Product
inner join Brand
on Product.BrandID = Brand.ID
WHERE Product.Name LIKE '%pants%'
or
Brand.Name LIKE '%pants%'
这需要1秒。
我看MSDN上说:http://msdn.microsoft.com/en-us/library/ms187787.aspx
To search on multiple tables, use a joined table in your FROM clause to search on a result set that is the product of two or more tables.
所以我增加了一个INNER连接表来自:
SELECT count(*)
FROM (select Product.Name ProductName, Product.SKU ProductSKU, Brand.Name as BrandName FROM Product
inner join Brand
on product.BrandID = Brand.ID) as TempTable
WHERE
contains(TempTable.ProductName, 'pants')
or
contains(TempTable.BrandName, 'pants')
这将导致错误: 不能使用CONTAINS或FREETEXT谓词列'ProductName',因为它没有全文索引。
所以问题是 - 为什么OR条件可能导致诸如查询缓慢?
你对Product.BrandID的指数? – 2009-11-30 15:28:11
是的,在Product.BrandID上有一个索引。 – 2009-12-02 09:05:57