Friday, May 31, 2024

Azure QA

 1. What is the best way to migrate the On prim database to Azure cloud.

2.     Do you have experience on deploying the iaas, paas services.
3.     Have you knowledge on Azure storage options.
4.     Can we replicate the databases into azure cloud.
5.     Do you have experience on log shipping, mirroring and replication on hybrid cloud method.
6.     How can we move the 2TB database from on prim to azure cloud.
7.     Can we migrate windows and SQL logins to azure SQL VM.
8.     Cluster rebalancing.
9.     How can we move the TDE enabled database into cloud.
10.  They are asking need some experience and knowledge about azure administration.

Wednesday, May 29, 2024

Common SQL Server Intereview Q & A

 What is query optimizer fix setting in database scoped configuration. 
This was introduced in SQL 2016 version , this actually will take care of the query optimizer improvements while the hotfix or CU is updated when the setting is ON depending  on the db compatability option , when the db compatability is 130 (sql 2016) then it doesn't matter  if the setting is ON or OFF.  The trace flag 4199 needs to enabled to get the advantage of this configuration after hotfix is updated. 
WHAT IS OPTIMIZE FOR ADHOC WORKLOAD
When this setting is set to 1 the plan cache stores the stub plan for the query in the buffer cache instead the entire plan , when the same query is executed it  reuses the stub plan . 
hashtagTransparent Data Encryption (TDE):
Encrypts the entire database at rest.
Data is automatically encrypted and decrypted as it is read from and written to disk.
Provides encryption at the file level, securing backups and data files.
Key management is simplified, as TDE handles key encryption with a database encryption key (DEK) and a certificate stored in the master database.
Column-level Encryption:
Allows encryption of specific columns within a table, providing granular control over data security.
Different columns within the same table can have different encryption keys.
Offers protection for sensitive data while allowing non-sensitive data to remain unencrypted for easier querying and processing.
Requires explicit encryption and decryption functions to access encrypted data.

Always Encrypted:
Enables client-side encryption of sensitive data before it is sent to the database.
Data remains encrypted during transmission and while stored in the database, ensuring end-to-end encryption.
Encryption keys are managed outside of SQL Server, typically by client applications, enhancing security.
Supports deterministic and randomized encryption schemes, allowing for different levels of security and search capabilities.

Backup Encryption:
Provides the ability to encrypt database backups to protect sensitive data during storage and transmission.
Backup encryption can be applied at the time of backup creation using a certificate or asymmetric key.
Protects backups stored locally or in the cloud, preventing unauthorized access to data in case of backup theft or interception.
Decryption is required to restore encrypted backups, ensuring data integrity and security.

Transport Layer Security (TLS):
Encrypts data transmitted between SQL Server and client applications.
Provides secure communication over the network, preventing eavesdropping and data interception.
Supports various TLS protocols and cipher suites for encryption and authentication.
Configuration options in SQL Server allow for enabling and enforcing TLS encryption to protect data in transit.


COLUMN LEVEL ENCRYPTION
Lets understand with and example for column-level encryption:

-- Creating a master key
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'StrongPassword123';

-- Creating a certificate
CREATE CERTIFICATE MyCertificate WITH SUBJECT = 'My Certificate';

-- Creating a symmetric key
CREATE SYMMETRIC KEY MySymmetricKey WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE MyCertificate;

-- Encrypting data in a table
OPEN SYMMETRIC KEY MySymmetricKey DECRYPTION BY CERTIFICATE MyCertificate;
UPDATE dbo.YourTable
SET EncryptedColumn = EncryptByKey(Key_GUID('MySymmetricKey'), 'SensitiveData');

-- Decrypting data
SELECT CONVERT(varchar, DecryptByKey(EncryptedColumn)) AS DecryptedData
FROM dbo.YourTable;
CLOSE SYMMETRIC KEY MySymmetricKey;


Connect SQL SERVER instance when there is no logins working.
Sometimes we may miss password or logins not working to connect sql server in this case follow the below steps to connect the instance. 
1. Stop the instance.
2. Open CMD in Adminstator mode- Root to SQL Server bin folder  with -m which will start the sql server in single user mode.
3. Open another CMD in adminstrator mode- Run the script to set the new login or create login .
4. Exit  both CMD and start the instance.   

Why the query is running slow in application and fast in SSMS.
This can be due to below factors.
1. The parameters or variables are not same between Application and SSMS.
2. Check execution time on server.
execution time on server can be checked by
SET STATISTICS TIME ON <YourQuery> SET STATISTICS TIME OFF
The time  may vary due to network speed between the servers or hardware resource different. 
3. SET options
Some options like artithabort ,nulls,force_plan settings may different in server and application.
For eg. by default arithabort is ON in ssms and OFF in application, this may affect the performance.  

How to migrate SSRS from one server to another.
https://datasteve.com/2024/04/03/migrating-ssrs-from-one-server-to-another.

Difference between Full and tail log backup ?


Is it possible to take backup when db is corrupted ?

what parameter we need to use whenever we are taking the backup  and db is  corrupted ?


Scenario: You have an on-premises Microsoft SQL Server (SQL1) hosting five databases. The goal is to migrate these databases to an Azure SQL Managed Instance with minimal downtime and no data loss. What tool should you use?

Options:
A) Always On Availability Groups
B) Backup and Restore
C) Log Shipping
D) Database Migration Assistant

Answer: B) Backup and Restore
Explanation:
While the Database Migration Assistant (DMA) is valuable for assessment and conversion tasks, it doesn’t directly support migrations to Azure SQL Managed Instance. The recommended approach for database migration is the Azure SQL Migration extension for Azure Data Studio, which supports both online and offline migrations. However, this option isn’t listed here. Log shipping isn’t viable for Azure SQL Managed Instance as it only supports full backups for restoration.

AZURE DB HELP

 Script to check the db space of all databases on azure db


Courtesy : https://www.sqlservercentral.com/articles/check-azure-sql-db-space-used

SELECT
    [database_name],
    start_time AS 'LastCollectionTime',
    storage_in_megabytes AS 'CurrentSize(MBs)',
    allocated_storage_in_megabytes AS 'AllocatedStorage(MBs)'
  FROM (
            SELECT
                ROW_NUMBER() OVER(PARTITION BY [database_name] ORDER BY start_time DESC) AS rn,
                [database_name],
                start_time,
                storage_in_megabytes,
                allocated_storage_in_megabytes
            FROM sys.resource_stats
        ) rs
WHERE rn = 1

Monday, April 22, 2024

Always on troubleshoot

 Scenarios where the failover not happening in Always on.

1. Port number is not allowed 1433,5022 in firewall settings.

2. Event viewer no erros found.

3. Cluadmin- Check preffered nodes in node properties.

4. check if connect permission is not enabled  for service account.

5. check alteravailability group and serverstate permission for NTAuthority/system account  on both the nodes.  (why Ntauthority/system- coz healthcheck is authorised to this account)


Failover disconnected .

1. Check Endpoint algorithm on both the nodes which must be compatible i.e AES from 2016 version or rc4 from 2014 version.

2. Check nodes are up in cluadmin.


Failover is initiated and state is in resolving state on the secondary.

Investigate the sql error log and find if any errors, this may be due to login failed 

The account Ntauthority system must be available on  both the nodes , if not then create the account with grant alter any availability group to NTauthority system service account.

what happens when secondary server is offline and comes back online in synchrnous commit mode.

The state will be shown as not synchronizing in secondary and  the transactions are not hardened due to disconnected state. the primary will not wait for the acknowledgement as it changes to asynchronous commit mode . in this case there may be chances to log file size gets increased. 

As soon as the secondary comes online it establises conneciotn to primary replica sends the lsn details to primary and primary starts sends  log block to secondary while here the state changes to synchronzing in secondary until all the lsn are matched then the state changes to synchronised and commit mode is changed to synchronous commit mode. 

How to Healthcheck on AG

Look for Alwayson health check on extended events.
Look for SQL Server errorlog to corelate the events.
Generate cluster-logs if required

Tuesday, February 27, 2024

LOG SHIPPING Interview QA

Log shipping can be configured in multiple secondary databases.
Log shipping can be configured in diff domain or in diff workgroups.
There are two modes in Logshipping
 Standby/Readonly mode : we can access the database for read only operations
Norecovery mode : restoring mode where we cannot access the database

For backup folder , Need Read/write permissions for Sql agent service account from secondary server.

Monitoring Log shipping :
Monitoring log shipping can be done through
1. Error logs- EXEC xp_readerrorlog 0,1,"Shipping",Null
2. System Stored procedure--> sp_help_log_shipping_monitor, sp_help_log_shipping_monitor_primary, sp_help_log_shipping_monitor_secondary
3. SSMS Report- Management Studio > Reports > Standard Reports > Transaction Log Shipping Status.
4.Querying MSDB database-->
SELECT *  FROM [msdb].[dbo].[log_shipping_monitor_error_detail] WHERE [message] like '%Operating system error%'
5. Application/Event Viewer log.



How to failover in the log shipping 

 Steps to failover in the Log shipping.

1. Disable the Log shipping jobs on both Primary and secondary server.

Primary server :    

    Backup Job

Secondary server 

Copy Job

Restore backup job


2. Backup the database in the primary server with tail log backup  with norecovery.

This can be achieved via backup Script with NO_truncate cmd or in the GUI -backup-T log - Tail log backup option.  With no recovery the db will be moved to recovery state.

3. Manually copy the tail log backup to secondary server .

4. Restore the tail log backup with recovery 

The secondary DB will be restored with recovery.

5. Migrate the logins (orphaned) to server via script or manually for the missing logins.

Run the login script  and sp_helprevlogin in primary server  which will script out the login command and exec these scripts in secondary server .     The user will be migrated once the db is restored in secondary. 

  Courtesy : https://www.youtube.com/watch?v=-1M_Xdrq1SY