sql azure查询耗时

标签 sql sql-server azure

我在 Sql Azure 中有一个表包含大约 6M 行。 我想为它创建一个新索引。 cmd 是这样的:

CREATE NONCLUSTERED INDEX [INDEX1] ON [dbo].Table1
(
    [Column1] ASC,
    [Column2] ASC,
    [Column3] ASC,
    [Column4] ASC
)
INCLUDE ( [Column5],[Column6]) 

大约15分钟后,出现错误

"Msg 10054, Level 20, State 0, Line 0

A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)"

试了好几次,都是同样的错误。 但我执行了其他耗时的查询,例如:

Insert into table1(Col1,Col2,Col3) select Col1,Col2,Col3 from table2

花了20分钟,成功返回。

查询在同一个 Sql Azure DB 中执行。我不知道这里发生了什么事。有人可以帮忙吗?谢谢!

最佳答案

我在包含 100M 行的表中遇到了同样的问题,并联系了 Microsoft 支持。这是我得到的回复:

The reason why you can’t create the index on your table is that you are facing a limitation on the platform that prevents to have transactions larger than 2GB.

The creation of an index is a transactional operation that relies on the transaction log to execute the move of the table pages. More rows in a table means more pages to put in the T-Log. Since your table contains 100 million of records (which is quite a big number), it is easy for you to hit this limit.

In order to create the index we need to change the approach. Basically we are going to use a temporary(staging) table to store the data while you create the index on the source table, that you would have previously cleared from data.

Action Plan:

  1. Create a staging table identical to the original table but without any index (this makes the staging table a heap)
  2. move the data from the original table to a staging table (the insert is faster because the staging table is a heap)
  3. empty the original table
  4. create the index on the original table (this time the transaction should be almost empty)
  5. move back data from staging table to original table (this would take some time, as the table contains indexes)
  6. delete the staging table

他们建议使用BCP在临时表和原始表之间移动数据。

查看 event_log 表时...

select * from sys.event_log 
where database_name ='<DBName>'
and event_type <> 'connection_successful'
order by start_time desc

..我发现了这个错误消息:

The session has been terminated because of excessive transaction log space usage. Try modifying fewer rows in a single transaction.

关于sql azure查询耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233410/

相关文章:

sql - 使用来自交叉实体的复合外键作为主键?

php - 如何按特定月份/年份选择记录

java.sql.SQL警告: [Microsoft][SQLServer 2000 Driver for JDBC]Database changed to X

azure - 如何从 Powershell Runbook 访问虚拟机中的脚本文件

azure - 逻辑应用连接器策略引发 "Property Immutable"错误

SQLException - 事务开始/提交不匹配

mysql 日期时间比较查询语法对我不起作用

sql-server - SQL Server 存储过程返回表,每个用户的唯一性计数

sql - 如何处理 char(1) 来代替 bool 值和三态字段?

azure - 将设备到云消息发送到 IoTHub 时,是否可以重用 Azure Functions 上的连接?