如何加快这个简单的查询

时间:2015-09-23 23:18:46

标签: sql sql-server

Bad_Phrase具有> 15MM记录和Update [SourceTable] Set Bad_Count = (Select count(*) from Bad_Phrase where [SourceTable].Name like '%'+Bad_Phrase.PHRASE+'%') 有> 3K记录,以下查询在SQL Server 2005 SP4上运行将近10个小时。

Bad_Phrase

在英语中,此查询计算[Name]中列出的任何短语是SourceTable中列Bad_Count的子字符串的次数,然后将该结果放入列中let self = this //saving the context here export default Ember.controller.extend({ init() { this._super(...arguments); self = this; }, settings: { crud: { read: { enabled: true, default() { return self.get('blah.blah'); //Need to access the controller context } } } } });

我想了解一些如何让这个查询运行得更快的建议。

3 个答案:

答案 0 :(得分:1)

由于缺乏更好的想法,这里有一个:

我不知道SQL Server本身是否支持并行化UPDATE语句,但您可以尝试通过对需要完成的工作进行分区来手动完成。

例如,举个例子,如果您可以手动并通过编写小应用程序并行运行以下2个update语句,我很想知道您是否可以降低总数处理时间。

Update [SourceTable] 
Set Bad_Count=(
    Select count(*) 
    from Bad_Phrase 
    where [SourceTable].Name like '%'+Bad_Phrase.PHRASE+'%'
)
where Name < 'm'

Update [SourceTable] 
Set Bad_Count=(
    Select count(*) 
    from Bad_Phrase 
    where [SourceTable].Name like '%'+Bad_Phrase.PHRASE+'%'
)
where Name >= 'm'

因此,第一个update语句负责更新名称以字母a-l开头的所有行,第二个查询负责o-z

这只是一个想法,您可以尝试将其拆分为更小的块和更多并行的update语句,具体取决于SQL Server计算机的容量。

答案 1 :(得分:0)

听起来您的查询正在扫描整个表格。您的表是否有适当的索引。在where子句中出现的列上放置索引是一个很好的起点。您还可以尝试在Sql server管理工作室中获取查询的成本(显示估计的执行成本),或者如果您愿意等待(显示实际执行成本)都是查询窗口中的按钮。这笔费用将提供有关永远采取行动的见解,并可能引导您更快地查询。

答案 2 :(得分:0)

您正在使用具有相同表的子查询更新表,每行更新将扫描整个表,这可能会导致执行时间过长。我认为如果您首先在public int getDay(){ return day;} public int getMonth(){ return month;} public int getYear(){ return year;} 表中插入所有数据然后在更新语句中使用#temp表,则会更好。或者你也可以#temp Source表和Temp表。