Wednesday, 30 August 2023

SQLServer : Script to check and create missing multi-column stats on a table

 

Statistics are critical for SQL Server performance. By default, SQL Server creates statistics for columns  However, SQL Server isn’t yet quite smart enough to automatically create multi-column stats objects that might be helpful. 

So the below script compares the details provided by the missing index DMVs against the existing sys.stats and sys.stats_columns views to provide a list of potential new stats.

;WITH suggested_indexes AS

(

    SELECT ObjectName = QUOTENAME(s.name) + N'.' + QUOTENAME(o.name)

        , Columns = STUFF((

            SELECT N', ' + QUOTENAME(mic.column_name)

            FROM sys.dm_db_missing_index_columns(mig.index_handle) mic 

            WHERE mic.column_usage = N'EQUALITY' 

                OR mic.column_usage = N'INEQUALITY' 

            ORDER BY mic.column_name FOR XML PATH(N'')

            ), 1, 2, '')

    FROM sys.dm_db_missing_index_groups mig

        INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle

        INNER JOIN sys.objects o ON mid.object_id = o.object_id

        INNER JOIN sys.schemas s ON o.schema_id = s.schema_id

    WHERE mid.database_id = DB_ID()    

)

, existing_stats AS

(

SELECT ObjectName = QUOTENAME(sch.name) + N'.' + QUOTENAME(o.name)

    , StatsName = s.name

    , AutoCreated = s.auto_created

    , UserCreated = s.user_created

    , StatsColumns = STUFF((SELECT N', ' + QUOTENAME(c.name)

        FROM sys.stats_columns sc 

            INNER JOIN sys.columns c ON sc.object_id = c.object_id AND sc.column_id = c.column_id

        WHERE s.object_id = sc.object_id AND s.stats_id = sc.stats_id 

        ORDER BY sc.stats_column_id

        FOR XML PATH(N'')

        ), 1, 2, N'')

    , FilterDefinition = CASE WHEN s.has_filter = 1 THEN s.filter_definition ELSE N'' END

FROM sys.stats s

    INNER JOIN sys.objects o ON s.object_id = o.object_id

    INNER JOIN sys.schemas sch ON o.schema_id = sch.schema_id

WHERE o.is_ms_shipped = 0

)

SELECT *

    , CreateStmt = N'CREATE STATISTICS [stats_name_' 

       + CONVERT(nvarchar(30), CONVERT(bigint, CRYPT_GEN_RANDOM(4))) 

       + N'] ON ' + si.ObjectName + N'(' + si.Columns + N') WITH FULLSCAN;'

FROM suggested_indexes si

WHERE NOT EXISTS (SELECT 1

    FROM existing_stats es

    WHERE es.ObjectName = si.ObjectName

        AND es.StatsColumns = si.Columns

    )

GROUP BY si.ObjectName

    , si.Columns

ORDER BY si.ObjectName

    , si.Columns;

Wednesday, 16 August 2023

SQLServer: Query Plan with missing index details

 

Run the following query to identify queries that cause high CPU usage and that contain at least one missing index in the query plan

SELECT

    qs_cpu.total_worker_time / 1000 AS total_cpu_time_ms,

    q.[text],

    p.query_plan,

    qs_cpu.execution_count,

    q.dbid,

    q.objectid,

    q.encrypted AS text_encrypted

FROM

    (SELECT TOP 500 qs.plan_handle,

     qs.total_worker_time,

     qs.execution_count FROM sys.dm_exec_query_stats qs ORDER BY qs.total_worker_time DESC) AS qs_cpu

CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS q

CROSS APPLY sys.dm_exec_query_plan(plan_handle) p

WHERE p.query_plan.exist('declare namespace 

        qplan = "http://schemas.microsoft.com/sqlserver/2004/07/showplan";

        //qplan:MissingIndexes')=1

SQLServer: Find Top CPU and IO intensive queries


SELECT TOP 50

[Avg. MultiCore/CPU time(sec)] = qs.total_worker_time / 1000000 / qs.execution_count,

[Total MultiCore/CPU time(sec)] = qs.total_worker_time / 1000000,

[Avg. Elapsed Time(sec)] = qs.total_elapsed_time / 1000000 / qs.execution_count,

[Total Elapsed Time(sec)] = qs.total_elapsed_time / 1000000,

qs.execution_count,

[Avg. I/O] = (total_logical_reads + total_logical_writes) / qs.execution_count,

[Total I/O] = total_logical_reads + total_logical_writes,

Query = SUBSTRING(qt.[text], (qs.statement_start_offset / 2) + 1,

(

(

CASE qs.statement_end_offset

WHEN -1 THEN DATALENGTH(qt.[text])

ELSE qs.statement_end_offset

END - qs.statement_start_offset

) / 2

) + 1

),

Batch = qt.[text],

[DB] = DB_NAME(qt.[dbid]),

qs.last_execution_time,

qp.query_plan

FROM sys.dm_exec_query_stats AS qs

CROSS APPLY sys.dm_exec_sql_text(qs.[sql_handle]) AS qt

CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp

where qs.execution_count > 5 --more than 5 occurences

ORDER BY [Total MultiCore/CPU time(sec)] DESC

SQLServer -: Find Top CPU Intensive Queries

Queries that are currently in cache that are consuming more CPU

;WITH eqs

AS (

    SELECT 

         [execution_count]

        ,[total_worker_time]/1000  AS [TotalCPUTime_ms]

        ,[total_elapsed_time]/1000  AS [TotalDuration_ms]

        ,query_hash

        ,plan_handle

        ,[sql_handle]

    FROM sys.dm_exec_query_stats

    )

SELECT TOP 10 est.[text], eqp.query_plan AS SQLStatement

    ,eqs.*

FROM eqs

OUTER APPLY sys.dm_exec_query_plan(eqs.plan_handle) eqp

OUTER APPLY sys.dm_exec_sql_text(eqs.sql_handle) AS est

ORDER BY [TotalCPUTime_ms] DESC

Queries with average CPU usage 

select query_stats.query_hash,

SUM(query_stats.total_worker_time) / SUM(query_stats.execution_count) as avgCPU_USAGE,

min(query_stats.statement_text) as QUERY

from (

select qs.*,

SUBSTRING(st.text,(qs.statement_start_offset/2)+1,

((case statement_end_offset

when -1 then DATALENGTH(st.text)

else qs.statement_end_offset end

- qs.statement_start_offset)/2) +1) as statement_text

from sys.dm_exec_query_stats as qs

cross apply sys.dm_exec_sql_text(qs.sql_handle) as st 

) as query_stats

group by query_stats.query_hash

order by 2 desc;






Sunday, 13 August 2023

SQLServer - Always On Availability Group TCP Port and Endpoint Conflicts

 

              Always On Availability Group and SQL Server FCI can have below port conflicts 


Having an availability group with a listener that listen on the same TCP port than the standalone instance on the same process will not result to a TCP port conflict.

Having an availability group with a listener that listen on the same TCP port than the standalone instance on a different process will result to a TCP port conflict. In this case each SQL Server process will attempt to open a socket on the same TCP port and on the same address IP.

Having Multiple availability group  running on the same TCP endpoint port is not a problem because they can open a socket on their distinct IP address. However you can face port conflicts in the case you have multiple  instances on the same node using the same endpoint port ,So in that case you need to change the TCP endpoint port .

Having several SQL Server FCI that listen on the same port is not a problem because they can open a socket on their distinct IP address. However you can face port conflicts in the case you have also a standalone instance installed on one of the cluster node

Always On Secondary Replica in a disconnected state where we have Multiple Availability Groups on the same SQLServer Instance

Always On Secondary Replica is showing as disconnected state where we have multiple Availability group  on the same SQL Server instance .

Scenario:-

we have 2 Availability Group running on  2 servers. First AG is working fine as expected . 

But secondary replica on the 2nd AG is showing as disconnected state  and databases are not in sync .

Resolution:-

when we checked  and understand  that the endpoint URL of the secondary AG replica  is configured with wrong TCP port , 

TCP://DBSERVER1.manufacturing.Adventure-Works.com:5022

TCP://DBSERVER2.manufacturing.Adventure-Works.com:5023

After updating the correct port on the endpoint URL , secondary replica came online and starts synchronizing 

It should be as below ,

TCP://DBSERVER1.manufacturing.Adventure-Works.com:5022

TCP://DBSERVER2.manufacturing.Adventure-Works.com:5022

Cause:-

1) We can not have multiple endpoint on the SQL Server Instance 

2) Each port number must be associated with only one endpoint, and each endpoint is associated with a single server instance; thus, different server instances on the same server listen on different endpoints with different ports. Therefore, the port you specify in the endpoint URL when you specify an availability replica will always direct incoming messages to the server instance whose endpoint is associated with that port.

3)We can use the same port on multiple AG's  on the same server since each listener is associated with an IP address and it will use the same TCP socket 

4) If you are using multiple instance on the same server then we need to have different endpoint created with different port  .Hence the endpoint URL also will change for the named instance .









Monday, 24 December 2018

Install SQL Server 2019 preview CTP on Ubuntu Server 18.04

Pre-requisites

  • Ubuntu Server 18.04
  • Internet Connection to update the Ubuntu and Microsoft Packages 
  • If you are going for an offline install , you can download the packages from the link https://packages.microsoft.com/
  • Admin/root access to the user
I am using an Ubuntu Server version 18.04 with latest update patches and my host name is mssql2

Install and Configure SQLServer 2019

To configure SQL Server on Ubuntu, run the following commands in a terminal/putty to install the mssql-server package.

1.    Import the public repository GPG keys:

wget -qO- https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add –



1       2.   Register the Microsoft SQL Server Ubuntu repository:




           3.   Run the following commands to install SQL Server:
                Run the below command to update the packages in the server
                   sudo apt-get update
                   sudo apt-get install -y mssql-server


After the package installation finishes, run mssql-conf setup and follow the prompts to set the SA password and choose your edition





































To Check the MSSQL-Server Service Status

sudo systemctl status mssql-server
















Install the SQL Server command-line tools

To create a database, you need to connect with a tool that can run Transact-SQL statements on the SQL Server. The following steps install the SQL Server command-line tools: 
Use the following steps to install the mssql-tools on Ubuntu.

1    1.  Import the public repository GPG keys.

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
1   
     2. Register the Microsoft Ubuntu repository.

curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

1.     3. Update the sources list and run the installation command with the unixODBC developer package.
sudo apt-get update
sudo apt-get install mssql-tools unixodbc-dev



4.Set the .bash_profile on Linux envireonment 

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

source ~/.bashrc







 To check the MSSQL Processes

  ps -ef | grep mssql








To connect Sqlserver from SQLCMD utility 

sqlcmd -S mssql2 -U SA -P "Password"






















To connect SQLServer  from SQLServer Management Studio from outside






Done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!



Wednesday, 10 January 2018

%%Failed to Create Availability Group, SQL Server 2014 error 41042 encountered%%%

Failed to Create Availability Group, SQL Server 2014 error 41042        encountered


We were moving our SQL server boxes from one domain to another domain and after successful movement to the new domain, including windows cluster, Server's ,SQLServers, we have encountered the issues in Always on  availability group while recreating .

We had encountered the error SQL Server 2014 error 41042, Failed to Create  availability group

"Microsoft SQL Server, Error: 41042".


Below are the easy steps to resolve this issue 

Go to Windows Fail over Cluster Manager console  and “delete” the Resources/Listener which you are not able to create.





Then go the below Registry path for all servers where the Availability group exists.

“ HKEY_LOCAL_MACHINE\Cluster\HadrAgNameToldMap

                           Delete the entries  which are not able to create/failed 




Then re- run the wizard!!!!!!!!!!!!!!!!!!!! 









Wednesday, 3 February 2016

Schema Level Refresh in MS SQLServer


Consider a Scenario like, we have two environments Production and Development where having same set of tables with two different schemas based on the environment. So the application is working based on the Schema .

The Schema names are Prod ,Dev and the default schema dbo.
we are doing a Database Refresh activity from Production to Development as a normal DBA practice but after that the application is throwing error like 

                                       “Not able to connect or invalid object exist “

Reason: 

After the Database Restore activity, the Development Database will be replaced with Production Database in the object level also , Dev objects  have been replaced with Prod objects hence DEV application is not able to identify the changed objects having production schema.
In this Scenario, we need to check the objects where any schema level change is required or not after doing the DB Refresh.

Before the Database refresh activity from Production to Development

                                                            Production Database



                                                        
Developmet Database



So After the Database Refresh activity the Development database schema will be changed into production.


So we need to do a Schema level refresh also to make the application fully functional.
Below are the script to use a schema level refresh activity.


declare @sql varchar(8000), @table varchar(1000), @oldschema varchar(1000), @newschema   varchar(1000)

  set @oldschema = 'dbo'
  set @newschema = 'exe'

 while exists(select * from sys.tables where schema_name(schema_id) = @oldschema)

  begin
      select @table = name from sys.tables 
      where object_id in(select min(object_id) from sys.tables where  schema_name(schema_id)  = @oldschema)

    set @sql = 'alter schema ' + @newschema + ' transfer ' + @oldschema + '.' + @table

   exec(@sql)
 end



After the Schema level Change you should see the table like this




If you want to move all tables into a new schema, you can use the undocumented and to be deprecated at some point, sp_MSforeachtable stored procedure:

exec sp_MSforeachtable "ALTER SCHEMA TargetSchema TRANSFER ?"





Friday, 13 November 2015

SQLSvr 2012 Enterprise Failover cluster installation fails with error "Could not find the Database Engine startup handle"


While doing the 2012 SQL Failover Cluster Installation , received the below error in the last stage and not able to start your DB Engine and Agent Services .

SQL Svr 2012 Enterprise EN\ Failover cluster installation fails with error "Could not find the Database Engine startup handle"

In the SQLServer Error Log , we could see the below error message ,

TDSSNIClient initialization failed with error 0x80090331, status code 0x80. Reason: Unable to initialize SSL support. The client and server cannot communicate, because they do not possess a common algorithm

FIX: You cannot use the Transport Layer Security protocol version 1.2 to connect to a server that is running SQL Server 2014 or SQL Server 2012


Resolution

1. Enable TLS 1.0 & SSL 3.0 under [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\ in windows registry .
2. In case you want to use any other protocol e.g. TLS 1.2 then you have to apply Cumulative Update 6 for SQL Server 2012 SP2 .


Reference 



Thursday, 16 July 2015

Reporting Services Configuration Tool unable to connect Instance error - In windows 2012 server having SQLServer 2008/R2 named instance

When opening the Reporting Services Configuration Manager attempting
to connect to a named instance, no instances are available in the Report Server Instance drop down list, and when the find button is pressed the error

 "No report servers were found.
 Details:
 Invalid Namespace"

Resolution
Go to the path -Microsoft SQL Server\MSRS10.SQL2008\Reporting Services\ReportServer\bin\reportingservices.mof .
Take a backup of the file before modifying
Replace the the instance name in all places with your correct instance 
name in the .mof file . your named instance is SQL2008
//Name = "RS_SQL2008" change to Name = "SQL2008"//
Next Step : Open cmd as Administrator and Run the below command
mofcomp.exe "C:\Program Files\Microsoft SQL Server\MSRS10.SQL2008\Reporting Services\ReportServer\bin\reportingservices.mof"



                                  

After doing this , Please Restart WMI Services from Services.msc
and open Report Service configuration Manager console.
This time you will be able to see the Server and Instance details .
Good Luck 



Saturday, 23 August 2014

HARDWARE REQUIREMENTS FOR SQLSERVER CLUSTER BUILD ON A WINDOWS CLUSTER


  •  Microsoft Windows Server 2008 R2 with SP1 Enterprise Edition is installed on both computers in the cluster.
  • All nodes are added to the domain
  • A domain-level account that is a member of the local administrators group on each node.
  • A dedicated account is recommended.
  • Two PCI network adapters on each node in the cluster.
  • All hardware should be identical, slot for slot, card for card, BIOS, firmware revisions, and so on, for all nodes.
  • This makes configuration easier and eliminates compatibility problems.
  • Static IP addresses for all network interfaces on each node.
  • Each node must have at least two network adapters—one for connection to the client public network and the other for the node-to-node private cluster network
  • All nodes must have two physically independent LANs or virtual LANs for public and private communication.
  • All shared disks, including the quorum disk, must be physically attached to a shared bus.
  • Verify that disks attached to the shared bus can be seen from all nodes.
  • All shared disks must be configured as basic disks with RAID 5 - Logical Array and added to the windows cluster .
  • Required at least 3 shared disk/Mount for DATA, LOG and TEMPDB for each Instance
  • All partitions on the clustered disks must be formatted as NTFS with 64-KB allocation unit.
  • Cluster Quorum Disk Space Recommendation – 1 GB.
  • Quorum Configuration – Node and Disk Majority (Quorum)
  • IP Address and SQL Virtual/Cluster Name for SQL Instance
  • Cluster name will be added in DNS with virtual IP
  • Verify the Node Failover and Storage Failover
  • Configure MSDTC in the Cluster , It requires an IP  and Disk
  • MSDTC Disk Space Recommendation – 1 GB


PRE INSTALLATION CHECKS AND CONFIGURATIONS

Common Software Requirement
  •  SQL Server setup program requires Microsoft Windows Installer 4.5 or later.
  • DOT NET Framework 3.5.1
  • Microsoft Internet Explorer (IE) 6 SP1 or later is required.
  • For client connectivity, SQL Server 2008 R2 also requires the Shared Memory.Named Pipes, or TCP/IP network protocols.
  • SQL Server 2008 R2 Enterprise Edition with Service Pack 1 Software setup files.
  • Collation Settings Requirements for SQLServer


POST INSTALLATION CHECKS AND CONFIGURATIONS 

  • Configure the Possible Owners to the SQL Instance
  • Check the dependencies for SQL Instances which are properly configured
  • Validate Failover of Services and Application which includes all SQL Related Services and  Disks
  • Connect to SQL Instance and verify
  • Add the SQL Service Account ,DBA group in the SQL instance
  • Configure Min Server Memory and Max Memory  for SQL Server Instances depending on the available Physical Memory in the Sever
  • Configure MAX DOP in the SQLServer depends on the number of CPU in the Server.