简体   繁体   English

在SQL Server中如何在左联接表上全文搜索多个条件?

[英]How do you full-text search multiple criteria on left-joined tables in SQL Server?

I have a query that originally looks like this: 我有一个最初看起来像这样的查询:

select c.Id, c.Name, c.CountryCode, c.CustomerNumber, cacc.AccountNumber, ca.Line1, ca.CityName, ca.PostalCode
from dbo.Customer as c
left join dbo.CustomerAddress as ca on ca.CustomerId = c.Id
left join dbo.CustomerAccount as cacc on cacc.CustomerId = c.Id
where  c.CountryCode = 'XX' and (cacc.AccountNumber like '%C17%' or c.Name like '%op%'       
or ca.Line1 like '%ae%' or ca.CityName like '%ab%' or ca.PostalCode like '%10%')

On a database with 90,000 records this query takes around 7 seconds to execute (obviously all the joins and likes are taxing). 在具有90,000条记录的数据库上,此查询需要大约7秒钟的时间来执行(显然,所有联接和喜欢都在费劲)。

I have been trying to find a way to bring the query execution time down with full-text search on the columns concerned. 我一直在尝试找到一种方法,通过对相关列进行全文搜索来缩短查询执行时间。 However, I haven't seen an example of a full-text search that has three table joins like this, especially since my join condition is not part of the search term. 但是,我还没有看到这样的具有三个表联接的全文本搜索示例,特别是因为我的联接条件不是搜索项的一部分。

Is there a way to do this in full-text search? 有没有办法在全文搜索中做到这一点?


@David @大卫

Yep, there are indexes on the Ids. 是的,在Ids上有索引。

I've tried adding indexes on the CustomerAddress stuff (CityName, PostalCode, etc.) and it brought down the query to 3 seconds, but I still find that too slow for something like this. 我尝试在CustomerAddress内容(CityName,PostalCode等)上添加索引,这将查询降低到3秒,但是对于这样的事情,我仍然觉得太慢了。

Note that all of the text fields (with the exception of the ids) are nvarchars, and Line1 is an nvarchar 1000, so that might affect the speed, but still. 请注意,所有文本字段(ids除外)均为nvarchars,而Line1为nvarchar 1000,因此可能会影响速度,但仍会影响速度。

NOTE: This isn't really an answer, just an attempt to clarify what might actually be causing the performance problem(s). 注意:这并不是真正的答案,仅是试图弄清楚实际上可能导致性能问题的原因。

90,000 records is really a fairly small data set and the query is relatively simple with just two join. 90,000条记录实际上是一个很小的数据集,并且只有两个联接,查询相对简单。 Do you have indexes on CustomerAddress.CustomerId and CustomerAccount.CustomerId? 您在CustomerAddress.CustomerId和CustomerAccount.CustomerId上有索引吗? That seems more likely to be causing performance issues than the where condition LIKE predicates. 与LIKE判断的条件相比,这似乎更可能导致性能问题。 Are you typically searching to find a match on all of those columns at the same time? 您是否通常会同时搜索所有这些列的匹配项?

I would echo David's suggestion. 我会赞同戴维的建议。 You'd probably want to examine how the RDBMS is executing your query (eg, via table scans or using indexes). 您可能想要检查RDBMS如何执行查询(例如,通过表扫描或使用索引)。

One quick check would be to time just the part of the query involving the text search. 一种快速检查是仅计时与文本搜索有关的查询部分。 Something like this: 像这样:

SELECT  ca.Line1, ca.CityName, ca.PostalCode
FROM    CustomerAddress as ca
WHERE   ca.CustomerId = <some id number>
AND     (ca.Line1 LIKE '%ae%' OR ca.CityName LIKE '%ab%' OR ca.PostalCode LIKE '%10%');

If that takes a long time, then the LIKE s are the issue (remove one expression at a time from the OR ed line to see if just one of those columns is causing the slowdown). 如果这需要花费很长时间,则可能是LIKE的(每次从OR ed行中删除一个表达式,以查看是否只有这些列之一引起了速度下降)。 If it's quick, then the joins are suspect. 如果很快,则怀疑是联接。

You could write a similar query for the CustomerAccount table as well. 您也可以为CustomerAccount表编写类似的查询。

Run it through the query analyzer and see what the query plan is. 通过查询分析器运行它,然后查看查询计划是什么。 My guess would be that the double root (ie. %ae%) searches are causing it do do a table scan when looking for the matching rows. 我的猜测是,双根(即%ae%)搜索导致它在寻找匹配的行时确​​实进行了表扫描。 Double root searches are inherently slow, as you can't use any kind of index to match them usually. 双根搜索本质上是缓慢的,因为通常不能使用任何类型的索引来匹配它们。

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

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