Azure SQL数据库->添加索引->对象'/.rem'已断开连接或在服务器上不存在

时间:2018-11-17 22:26:25

标签: c# sql-server entity-framework azure azure-sql-database

我的数据库中有一个很大的表,当前大小为2 852,305 MB。我可以在开发人员机器上使用localdb实例来快速查询该数据库,大约100毫秒。

enter image description here

但是,使用完全相同的查询和数据,要在具有20个DTU(S1)的Azure SQL数据库上花费 70秒

enter image description here

我正在阅读 TL; DR :使用索引进行查询可以解决问题。

http://capesean.co.za/fixing-slow-performance-with-azure-sql-database/

我使用的是Entity Framework,因此我像往常一样添加了Index并尝试从Update-Database运行PMC

https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations#multiple-column-indexes

但是未添加索引,但我不断收到错误Object '/.rem' has been disconnected or does not exist at the server.。我已经阅读了下面的大约相同的错误代码,但是鉴于这种情况,我无法控制对象的生存期。

"Object has been disconnected or does not exist at the server" exception

该如何解决?

迁移和ApplicationDbContext的超时都可能不是超时问题。

internal sealed class Configuration : DbMigrationsConfiguration<ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        CommandTimeout = 10000; // migration timeout
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        //Log to output
        Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

        //Prevent timeouts
        Database.CommandTimeout = 1800;
    }
    ...

完整的堆栈跟踪:

  

System.Runtime.Remoting.RemotingException:对象   '/2f49096e_0c2e_49d6_a7a0_7ffbe823cf87/yxyu+vuywlowzb7myk8fhrw4_7932.rem'   已断开连接或在服务器上不存在。在   System.Data.Entity.Migrations.Design.ToolingFacade.ToolLogger.Verbose(字符串   sql)   System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(MigrationStatement   migrationStatement,DbConnection连接,DbTransaction   交易,DbInterceptionContext拦截上下文)   System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable 1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable 1   migrationStatements,DbTransaction事务,DbInterceptionContext   截取上下文)   System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable 1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable 1   migrationStatements,DbConnection连接,DbInterceptionContext   截取上下文)   System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable 1 migrationStatements, DbConnection connection) at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass32.<ExecuteStatements>b__30() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func 1   操作)   System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(操作   操作)   System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable 1 migrationStatements, DbTransaction existingTransaction) at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable 1   migrationStatements)位于   System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable 1 migrationStatements) at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable 1个操作,   IEnumerable 1 systemOperations, Boolean downgrading, Boolean auto)
at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration) at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable
1   endingMigrations,字符串targetMigrationId,字符串lastMigrationId)
  在   System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1   endingMigrations,字符串targetMigrationId,字符串lastMigrationId)
  在System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String   targetMigration)   System.Data.Entity.Migrations.DbMigrator。<> c__DisplayClasse.b__d()   在   System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(动作   mustSucceedToKeepDatabase)   System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(操作   mustSucceedToKeepDatabase)   System.Data.Entity.Migrations.DbMigrator.Update(字符串   targetMigration)   System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String   targetMigration)   System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()   在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()   宾语   '/2f49096e_0c2e_49d6_a7a0_7ffbe823cf87/yxyu+vuywlowzb7myk8fhrw4_7932.rem'   已断开连接或在服务器上不存在。

1 个答案:

答案 0 :(得分:0)

失败的迁移代码如下:

    public override void Up()
    {
        CreateIndex("dbo.MyTable", new[] { "Column1", "Column2", "Column3" }, name: "IX_MyIndex");
    }

为防止Visual Studio中的错误,我运行了命令Update-Database -script,复制了脚本并使用SQL Management Studio直接针对Azure数据库运行了该脚本。创建的索引如下所示:

CREATE INDEX [IX_MyIndex] ON [dbo].[MyTable]([Column1], [Column2], [Column3])

这成功了!该查询花费了大约8分钟的时间来运行。现在的效果类似于localdb

enter image description here

Update-Database -script的好处是您可以为__MigrationHistory获得正确的值。我本可以从localdb复制这些文件,但我认为这更干净。

如果不匹配,运行任何数据库代码都会给您此错误:

  

{“ Message”:“发生错误。”,“ ExceptionMessage”:“模型   自从开始以来,“ ApplicationDbContext”上下文的支持已更改   数据库已创建。考虑使用代码优先迁移进行更新   数据库(http://go.microsoft.com/fwlink/?LinkId=238269

将此问题发送给Microsoft,以便我希望他们对此进行修复。

https://twitter.com/Ogglas/status/1063927246245822464