简体   繁体   English

使用FULLTEXT的SQL Server 2012性能问题

[英]SQL Server 2012 Performance issue using FULLTEXT

I'm using SQL Server 2012 Standard and I have some issue using the CONTAINS clause on a query. 我正在使用SQL Server 2012 Standard,但在查询上使用CONTAINS子句时遇到一些问题。

My query: 我的查询:

select * 
from 
    calles as c 
INNER JOIN 
    colonias as col ON c.ID_Colonia = col.ID_Colonia
where 
    CONTAINS(c.Nombre,@Busqueda) OR CONTAINS(col.Nombre,@Busqueda)

If I use only one contains the time of the search is about 200 ms but if I use both it is about 10s (that's a lot of time). 如果仅使用一个包含内容,则搜索时间约为200毫秒,但是如果同时使用两者,则搜索时间约为10秒(这是很多时间)。 I try a workaround to do it using UNION like this: 我尝试使用UNION这样解决此问题:

select * 
from 
    calles as c 
INNER JOIN 
    colonias as col ON c.ID_Colonia = col.ID_Colonia
where 
    CONTAINS(c.Nombre,@Busqueda) 

UNION 

select * 
from 
    calles as c 
INNER JOIN 
    colonias as col ON c.ID_Colonia = col.ID_Colonia
where 
    CONTAINS(col.Nombre,@Busqueda)

And the query time is about 200ms again. 并且查询时间又是大约200ms。 But I think that the second code is clumsy. 但是我认为第二个代码很笨拙。 Do I have some error? 我有什么错误吗?

FULLTEXT index in SQL Server is a service which is (kinda) external to the RDBMS engine. SQL Server中的FULLTEXT索引是RDBMS引擎外部的某种服务。

It accepts the search string and returns a list of key values from the table (which need then to be joined with the table itself to be sure they're still there). 它接受搜索字符串并从表中返回一个键值列表(然后需要将这些键值与表本身连接起来以确保它们仍然存在)。

So in fact you are joining two more tables in your query and apply an OR condition to the result of the join. 因此,实际上您正在联接查询中的另外两个表,并将OR条件应用于OR的结果。

SQL Server's optimizer is not especially smart when it comes to constructs like this. 当涉及到这样的构造时,SQL Server的优化器并不是特别聪明。

Replacing an OR condition with a UNION is a legitimate and commonly used optimization technique. UNION替换OR条件是一种合法且常用的优化技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM