yanka24
yanka24

Reputation: 9

Querying system tables runs very slow on some servers

I have a SQL Server 2019 Enterprise and Quest LiteSpeed for SQL Server installed on all servers.
On some of the servers the connection from LiteSpeed to SQL Server in GUI times out.
After talking to Quest support, I was given a query that LiteSpeed runs when it tries to connect to the server.
On some servers this query runs for about 7-10 sec in Management Studio and those servers don't have any issues with LiteSpeed. But some servers run the same query for about 1.5 min!!!!! and those servers do not connect in LiteSpeed GUI. I am not sure what I have to look for and fix to make this work. Any help will be really appreciated.

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
    select
      db.[name],
      0 as [IsDetached],
      0 as [IsShutdown],
      case db.state when 4 then 1 else 0 end  as [IsSuspect],
      case db.state when 6 then 1 else 0 end  as [IsOffline],
      case db.state when 1 then 1 else 0 end  as [IsInLoad],
      case db.state when 2 then 1 else 0 end  as [IsInRecovery],
      case db.state when 3 then 1 else 0 end  as [IsNotRecovered],
      case db.state when 5 then 1 else 0 end  as [IsEmergencyMode],
      cast(db.is_in_standby as int) as [IsInStandBy],
      cast(db.is_read_only as int) as [IsReadOnly],
      case db.user_access when 1 then 1 else 0 end  as [IsSingleUser],
      case db.user_access when 2 then 1 else 0 end  as [IsDboOnly],
      isnull(ag.ReplicaRole, 0) as [ReplicaRole],
      isnull(ag.ReplicaGroup, '') as [ReplicaGroup],
      isnull(ag.syncState, -1) as [ReplicaState],
      case when db.[name] in (N'master', N'model', N'msdb', N'distribution') then 1 else 0 end as IsSystem,
      db.recovery_model as [RecoveryModel],
      db.compatibility_level as [CompatibilityLevel],
      isnull(DATABASEPROPERTYEX(db.[name], N'Collation'), 'N/A') AS Collation,
      CAST(db.is_remote_data_archive_enabled AS bit) AS [RemoteDataArchiveEnabled],
      isnull(cast(mr.mirroring_state as int), -1) as [MirrorState],
      isnull(cast(mr.mirroring_role as int), 0) as [MirrorRole] 
    from master.sys.databases db
      left outer join master.sys.database_mirroring mr on db.database_id = mr.database_id
      left outer join (
        select 
          db.name as [ReplicaDBName], 
          ag.name as [ReplicaGroup], 
          cast(ars.[role] as int) as [ReplicaRole], 
          cast(drs.synchronization_state as int) as [syncState]
        from master.sys.availability_groups ag 
          inner join master.sys.availability_replicas ar on ar.group_id = ag.group_id 
          inner join master.sys.dm_hadr_availability_replica_states ars on ars.replica_id = ar.replica_id
          inner join master.sys.databases db on db.replica_id = ars.replica_id
          inner join master.sys.dm_hadr_database_replica_states drs on drs.group_database_id = db.group_database_id 
        where ar.replica_server_name = SERVERPROPERTY('ServerName') AND drs.is_local = 1
        ) ag on ag.ReplicaDBName = db.[name]
    where db.[name] <> N'tempdb' and cast(isnull(db.source_database_id, 0) as bit) <> 1 
    ORDER BY db.[name]

Upvotes: 0

Views: 41

Answers (0)

Related Questions