AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 327969
Accepted
Francesco Mantovani
Francesco Mantovani
Asked: 2023-06-07 04:07:25 +0800 CST2023-06-07 04:07:25 +0800 CST 2023-06-07 04:07:25 +0800 CST

SQL 弹性池:重建索引时“信号量超时期限已过期”

  • 772

我正在缩小 SQL 弹性池上已有的数据库。

目标是在一个独特的弹性池中容纳尽可能多的数据库。

为了做到这一点,我偶然发现了一个博客,建议运行这个查询,它基本上做了三件事:

  1. -- 步骤1:重建所有索引
  2. -- 第 2 步:收缩数据库
  3. -- 步骤3:重建所有索引

查询来了:

create or alter procedure [#ForEachTable](@inputStmt nvarchar(max))
as
begin
    set nocount, xact_abort on;
    drop table if exists [#Tables];

    select concat(quotename([S].[name]), N'.', quotename([T].[name])) as [table]
    into [#Tables]
    from [sys].[schemas] as [S]
        inner join [sys].[tables] as [T]
            on [S].[schema_id] = [T].[schema_id]
    where [T].[is_ms_shipped] = 0;

    declare tables cursor local fast_forward for select [table] from [#Tables];
    open tables;

    declare @table nvarchar(max);
    fetch next from tables into @table;

    declare @total integer = (select count(*) from [#Tables]);
    declare @space integer = len(cast(@total as nvarchar(max)));
    declare @current integer = 1;
    while @@fetch_status = 0
    begin
        declare @stmt nvarchar(max) = replace(@inputStmt, N'?', @table);
        
        declare @msg nvarchar(max) = concat(
            sysutcdatetime(), N' - ',
            N'[', right(concat(N'000', @current), @space), N'/', @total, N']: ',
            N'Executing command: "', @stmt, N'".'
        );
        raiserror(@msg, 10, 1) with nowait;

        execute [sys].[sp_executesql] @stmt = @stmt;
    
        fetch next from tables into @table;
        set @current += 1;
    end;

    close tables;
    deallocate tables;
end;
go

-- Step 1: Rebuild all the indexes
raiserror(N'First rebuild...', 10, 1) with nowait;
execute [#ForEachTable] N'alter index all on ? rebuild with (online = on);';
go

-- Step 2: Shrink the database
raiserror(N'Shrink...', 10, 1) with nowait;
declare @stmt nvarchar(max) = concat(N'dbcc shrinkdatabase (', db_id(), N')');
execute [sys].[sp_executesql] @stmt = @stmt;
go

-- Step 3: Rebuild all the indexes
raiserror(N'Final rebuild...', 10, 1) with nowait;
execute [#ForEachTable] N'alter index all on ? rebuild with (online = on);';

但几个小时后我收到此警告消息,然后 SSMS 关闭:

First rebuild...
2023-06-06 07:39:50.2128129 - [0001/1467]: Executing command: "alter index all on [dbo].[xhisto_2018] rebuild with (online = on);".
2023-06-06 07:39:50.2440440 - [0002/1467]: Executing command: "alter index all on [dbo].[Affiliate] rebuild with (online = on);".
2023-06-06 07:39:50.2596849 - [0003/1467]: Executing command: "alter index all on [dbo].[template_step] rebuild with (online = on);".
2023-06-06 07:39:50.2753131 - [0004/1467]: Executing command: "alter index all on [dbo].[mapping_monthly] rebuild with (online = on);".
2023-06-06 07:39:50.7596876 - [0005/1467]: Executing command: "alter index all on [dbo].[statement] rebuild with (online = on);".
2023-06-06 07:39:50.7753120 - [0006/1467]: Executing command: "alter index all on [dbo].[history] rebuild with (online = on);".
2023-06-06 07:39:50.7753120 - [0007/1467]: Executing command: "alter index all on [dbo].[formula_items] rebuild with (online = on);".
2023-06-06 07:39:50.7909373 - [0008/1467]: Executing command: "alter index all on [dbo].[monthly_staging] rebuild with (online = on);".
2023-06-06 07:39:50.8221873 - [0009/1467]: Executing command: "alter index all on [dbo].[sales] rebuild with (online = on);".
Msg 121, Level 20, State 0, Line 82
A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The semaphore timeout period has expired.)

Completion time: 2023-06-06T15:21:35.2092836+02:00

我正在从安装在 Azure 上的 VM 上的 SSMS 运行查询。非常肯定这不可能是连接问题。这种情况发生二分之一。

azure-sql-database
  • 1 1 个回答
  • 37 Views

1 个回答

  • Voted
  1. Best Answer
    Francesco Mantovani
    2023-06-26T17:25:34+08:002023-06-26T17:25:34+08:00

    信号量问题是由 VDI(我在其上有 SSMS)和 Azure SQL 数据库的连接中断引起的。

    重新发明轮子是没有意义的。微软为Azure SQL数据库提出了一个开箱即用的解决方案:自动收缩

    -- Enable auto-shrink for the current database.
    ALTER DATABASE CURRENT SET AUTO_SHRINK ON;
    

    在此输入图像描述

    这就成功了。

    如果您想再次重建索引,则没有必要重新发明轮子。微软建议使用 Azure 自动化重建 SQL 数据库索引

    • 0

相关问题

  • 使用限制和偏移的单选与多选

  • 是否可以在 SQL Azure 中创建数百个数据库而不是一个大数据库并冒死锁的风险

  • 在 SQL Azure 中实现加密表

  • 将服务器链接到 SQL Azure 非常慢

  • 如何更改 SQL Azure 上的现有主键?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve