简体   繁体   English

SQL Server更新查询花费的时间太长

[英]SQL Server Update Queries Taking Too Long

I have a SQL query that builds analysis tables using SELECT INTO statements from populated staging tables. 我有一个SQL查询,该查询使用已填充的临时表中的SELECT INTO语句构建分析表。 This part goes pretty smooth, but when I try to append fields to the analysis tables, from each other or from the original staging tables, the UPDATE queries run for HOURS. 这部分进行得很顺利,但是当我尝试将字段彼此之间或从原始暂存表中添加到分析表时,UPDATE查询的运行时间为HOURS。

I feel this run time is unwarranted because the most records per table is 200K. 我觉得这个运行时间是不必要的,因为每个表的最多记录为200K。 I have a feeling that this is an issue with my log file management or the fact that I have no indexes on any of these tables. 我感觉这是我的日志文件管理问题,或者我在任何这些表上都没有索引。 I have been reluctant to build indexes because I know if hastily done, they can actually cause more problems. 我一直不愿意建立索引,因为我知道如果仓促完成,它们实际上会引起更多问题。 There are no triggers on any of these tables. 这些表中没有任何触发器。

I am trying to find an approach so that this query runs consistently. 我正在尝试找到一种方法,以便该查询始终运行。 In the past, it has run under 1 hour (which is still probably too long) but today for instance it has taken 3+ hours. 过去,它的运行时间不到1小时(可能仍然太长),但是今天,它花费了3个多小时。

I've already modified the log file to autogrow by 50% rather than 10%, to have an unlimited MAXSIZE, I've set the DB recovery mode to SIMPLE and I have shrunk the log file manually. 我已经将日志文件修改为自动增长50%(而不是10%),以具有无限的MAXSIZE,我将数据库恢复模式设置为SIMPLE,并且手动缩小了日志文件。 None of this is helping. 这些都没有帮助。

I know that TABLE SCANS are a source of lag in large queries, so is the solution here to create indexes for all of the fields I use in my LEFT JOINs depicted below? 我知道TABLE SCANS是大型查询中滞后的根源, 那么这里的解决方案是否为我在下面所示的LEFT JOIN中使用的所有字段创建索引的解决方案? These snippets are exact examples of where the query is lagging. 这些片段是查询滞后的确切示例。 I have transparency into this because I am following the execution of the script in the LIVE QUERY STATISTICS window. 我对此具有透明性,因为我正在“实时查询统计”窗口中跟踪脚本的执行。

UPDATE a
SET a.[Account ID] = b.[Account ID]
FROM SFAX.dbo.Leads as a
left join SFAX.dbo.Accts as b on a.[Company   Account] = b.[Account Name]
WHERE a.[Account ID] IS NULL;

-- Link by website2
UPDATE a
SET a.[Account ID] = b.[Account ID]
FROM SFAX.dbo.Leads as a
left join SFAX.dbo.Accts as b on a.url2 = b.Website2
WHERE a.[Account ID] IS NULL;
--
UPDATE a
SET a.[SFDC Account Name] = b.[Account Name]
FROM SFAX.dbo.Leads as a
left join SFAX.dbo.Accts as b on
a.[Account ID] = b.[Account ID]
WHERE a.[Account ID] IS NOT NULL;

UPDATE a
SET a.ConvertedFlag = b.[Lead ID]
FROM SFAX.dbo.WhoAnalysis1 as a 
LEFT JOIN SFAX.dbo.Contacts as b ON a.WhoId = b.[Contact ID];

Note: In the comments above you express doubt. 注意:在上面的评论中,您表示怀疑。 This is easy to test -- break your script in half prior to updates -- run part one -- then run one update and time it. 这很容易测试-在更新之前将脚本分成两半-运行第一部分-然后运行一个更新并安排时间。 Then make indexes for that one update. 然后为该更新创建索引。 Run it again. 再次运行。 How many orders of magnitude faster is it? 它快几个数量级?

Don't be scared of indexes if you work with SQL -- love your indexes. 如果您使用SQL,请不要害怕索引-喜欢索引。 Create indexes for 为创建索引

SFAX.dbo.Leads.[Company   Account], [Account ID] 
SFAX.dbo.Accts.[Account Name]

SFAX.dbo.Leads.url2, [Account ID]
SFAX.dbo.Accts.Website2

SFAX.dbo.Leads.[Account ID]
SFAX.dbo.Accts.[Account ID]

SFAX.dbo.WhoAnalysis1.WhoID
SFAX.dbo.Contacts.[Contact ID] 

But remember -- if you are changing a lot of records in your database this could be constrained by the speed of your I/O 但是请记住-如果您要更改数据库中的许多记录,这可能会受到I / O速度的限制

Below has a lot of nonsense work to do - so don't do it. 下面有很多废话要做的事-所以不要这样做。

UPDATE a
SET a.[Account ID] = b.[Account ID]
FROM SFAX.dbo.Leads as a
left join SFAX.dbo.Accts as b on a.[Company   Account] = b.[Account Name]
WHERE a.[Account ID] IS NULL;

If there is no match in b, you will set [a.Account ID] to null. 如果b中没有匹配项,则将[a.Account ID]设置为null。 But you are only selecting those rows in a where the column is NULL. 但是,您只选择列为NULL的a中的那些行。 SO DON'T DO THAT - your left join is pointless extra work. 所以不要这样做 -您的左加入是毫无意义的额外工作。 Change it to an inner join. 将其更改为内部联接。 Will that help significantly? 这会大大帮助吗? Can't tell without knowing the characteristics of your data. 在不知道数据特征的情况下无法分辨。

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

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