Database health check & Optimization steps

Database health check & Optimization steps

Steps and walkthrough to do a health check and optimization of iVend database.

The entire process broadly consists of 8 steps:

Step 1: Take Backup of DB.        

Open SQL Server Management Studio Express and connect to the SQL server.
  1. Expand Databases.
  2. Right-click on the database you want to back up, then select Tasks > Back up. (This is not available for version 2018/2019)
  3. On the Back Up Database window, make sure the Database field contains the name of the database you want to back up.
  4. Select the Backup Type. By default, it is Full - leave it set to that.
  5. Click Remove to remove the default/last backup file name.
  6. Click Add to open the Select Backup Destination window.
  7. Click [...] next to the File Name field.
  8. On the Locate Database Files window, select the folder where you want the backup file to go. By default, it is ..\Microsoft SQL Server\MSSQL.1\MSSQL\Backup.
  9. In the File Name field, type the name for this backup, with a .bak extension. For example, xyz_20080221.bak for a backup of the XYZ database created on 21 February 2008.
  10. Click OK to close the Locate Database Files window.
  11. Click OK to close the Select Backup Destination window.
  12. Click OK to start the backup. The progress icon displays in the lower-left corner, and a ‘completed successfully’ message displays when it's done.

Step 2 : Check allocation and structural integrity of all the objects.       

"DBCC CHECKDB
Note: If the health check returns any errors, then please contact your DBA immediately.

Step 3 : If above Query is Successful then run below Query to check Fragmentation of Major tables

"SELECT  top 10 OBJECT_NAME(i.OBJECT_ID) AS TableName, i.name AS IndexName, indexstats.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'DETAILED') indexstats
INNER JOIN sys.indexes i ON i.OBJECT_ID = indexstats.OBJECT_ID
WHERE indexstats.avg_fragmentation_in_percent > 30
AND i.index_id = indexstats.index_id
ORDER BY 3 desc"

Step 4: Check database size  

"SELECT DB_NAME(database_id) AS DBName,Name AS Logical_Name, Physical_Name,(size*8)/1024 SizeMB
FROM sys.master_files
WHERE DB_NAME(database_id) = 'CXSRetail66' --databasename"

Step 5: Shrinking the database


In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
Expand Databases, and then right-click the database that you want to shrink.
Point to Tasks, point to Shrink, and then click Database.
Database - Displays the name of the selected database.
Current allocated space - Displays the total used and unused space for the selected database.
Click OK

Step 6: Keep the Recovery model - Simple on Retail stores.


Step 7: Check SQL Server Version         

Select @@VERSION“       
 

Step 8: Optimization

Run the following command for database optimization
"Exec sp_updatestats
Exec     sp_databaseoptimization
Suggestion: Kindly take SQL Database backup on daily basis to avoid any unseen issues in future.
Note : it is suggested to perform such above activities in downtime with help of IT Team or SQL DBA when no one is working on iVend databases. Incase if anyone using SQL Express please check the DB size after optimization to avoid 10GB crossing limit. As SQL Express cannot grow more than 10 GB and it will stop processing data on reaching 10 GB limit, this is default behavior of MS SQL.