简体   繁体   English

MS SQL 日志大小不够或“由于 'active_transaction',数据库的事务日志已满”。

[英]MS SQL log size is not enough or "the transaction log for database is full due to 'active_transaction'."

I am trying to update the table with a result from select query.我正在尝试使用 select 查询的结果更新表。 The problem is that query takes too long and i get an error that log file is not enough.问题是查询花费的时间太长,并且我收到一个错误,即日志文件不够。 For example:例如:

With cte as (
select t1.key, t1.col1, t2.col2, t3.col3
from t1
left join t2 on t1.key = t2.key
left join t3 on t1.ket = t3.key
)

UPDATE t4
SET t4.col1 = cte.col1
    t4.col2 = cte.col2
    t4.col3 = cte.col3
from cte
where
    t4.key = cte.key

Right now i am updating the table by parts (just change the end of the query to现在我正在按部分更新表格(只需将查询的结尾更改为

where
    t4.key < 10000000
    t4.key = cte.key

). )。 Is there any way to optimize it and make queries faster?有什么办法可以优化它并使查询更快?

Is there any way to optimize it and make queries faster?有什么办法可以优化它并使查询更快?

You're doing most of it already.你已经做了大部分。 Doing your update in range-limited batches (with key < 10000000 in your case) is the way to do this kind of mass update to a table.在范围有限的批次中进行更新(在您的情况下, key < 10000000 )是对表进行这种大规模更新的方法。 More chunks and fewer rows in each chunk will perform even better.每个块中更多的块和更少的行将执行得更好。 When I do this kind of thing I use lots of batches, usually with 500 rows in each.当我做这种事情时,我会使用很多批次,通常每个批次有 500 行。

There's one other thing you can do: refrain from updating rows which already have the correct values.您还可以做另一件事:不要更新已经具有正确值的行。 Like this:像这样:

...
UPDATE t4
SET t4.col1 = cte.col1
    t4.col2 = cte.col2
    t4.col3 = cte.col3
FROM cte

WHERE t4.key = cte.key
  AND t4.col1 <> cte.col1
  AND t4.col2 <> cte.col2
  AND t4.col3 <> cte.col3

The point here is to have each UPDATE statement touch a limited number of rows.这里的重点是让每个 UPDATE 语句接触有限数量的行。 SQL server works by writing the whole update into its transaction log. SQL 服务器通过将整个更新写入其事务日志来工作。 Then, when it knows the whole transaction will succeed, it changes the tables with the data in the transaction log.然后,当它知道整个事务将成功时,它会根据事务日志中的数据更改表。 All that is to make it possible for other queries to see the old values in the table until your UPDATE statement finishes and lands in the table all at once rather than trickling in. It's all part of SQL's ACID -- atomicity, consistency, isolation, durability -- functionality.所有这一切都是为了让其他查询能够看到表中的值,直到您的 UPDATE 语句完成并一次全部进入表中,而不是慢慢进入。这都是 SQL 的ACID的一部分——原子性、一致性、隔离性,耐用性——功能性。

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

相关问题 由于“ACTIVE_TRANSACTION”,数据库“tempdb”的事务日志已满 - The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION' 由于“ ACTIVE_TRANSACTION”,数据库“ tempdb”的事务日志已满 - the transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION 由于“ACTIVE_TRANSACTION”,数据库“tempdb”的事务日志已满 - Transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION 由于活动事务,SQL Server 日志已满 - SQL Server Log full due to active transaction &#39;由于&#39;活动事务&#39;,数据库的事务日志已满 - 无法备份、放大、截断或收缩 - 'The Transaction Log for database is full due to 'Active Transaction' - Unable to Backup, Enlarge, Truncate, or Shrink 由于“CHECKPOINT”,数据库“master”的事务日志已满 - The transaction log for database 'master' is full due to 'CHECKPOINT' “由于&#39;LOG_BACKUP&#39;,数据库的事务日志已满”在共享主机中 - “The transaction log for database is full due to 'LOG_BACKUP'” in a shared host 由于“LOG_BACKUP”,数据库的事务日志已满 - The transaction log for database is full due to 'LOG_BACKUP' 数据库的事务日志已满 - The transaction log for the database is full MS SQL Server 数据库选项 - 事务日志 - MS SQL Server Database Option - Transaction Log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM