Wednesday, April 17, 2019

MSSQL: Tracking Pages Recovery and restoring

Tracking Pages Recovery and restoring

Prerequisite
The database must be FULL or Bulk-logged Recovery Model.

To save time and reducing further I/O contentions on the database by running DBCC CHECKDB, we can use PHYSICAL_ONLY.

DBCC CHECKDB WITH PHYSICAL_ONLY
RESTORE DATABASE VCDB_2016 PAGE='2:42' FROM BACKUP WITH NORECOVERY.

This can be achieved with GUI as well.

  1. Right Click at the database needs to be restored.
  2. Select Task - Pages






Tuesday, April 16, 2019

MSSQL: Improving the statistic plan for dynamic queries and procedures.

Dynamic queries are those that having similarity yet not exactly the same syntax and causing them to be cached for reuse by the planner. This can cause bloating to memory usage. In order to tackle the shortcomings, the user can set the optimizer for ad hoc only. Memory utilization can grow out of a desirable level.

Figure out what is the current plan size.

SELECT objtype, cacheobjtype,  AVG(usecounts) AS Avg_UseCount,  
SUM(refcounts) AS AllRefObjects, 
 SUM(CAST(size_in_bytes AS bigint))/1024/1024 AS Size_MB
FROM sys.dm_exec_cached_plans
WHERE objtype = 'Adhoc' AND usecounts = 1
GROUP BY objtype, cacheobjtype;













Run sp_configure to find out what the optimize for ad hoc set to.


Example of changing the optimize for ad hoc workloads and reduce procedure cache usage from these statements without creating any additional indexes.

USE VCDB
EXEC sp_configure 'show advanced options', 1
RECONFIGURE 
GO
EXEC sp_configure 'optimize for ad hoc workloads', 1
RECONFIGURE
GO 

After reconfiguring the optimizer for ad hoc workloads, the config_value would change to 1.







Example of clearing procedure cache. 
alter database scoped configuration clear procedure cache;

This is relatively similar to Oracle's shared pool cache clearing in term of mechanism.
alter system flush shared_pool;

Friday, April 12, 2019

MSSQL: Enable and Disable SQL Server Agent log


Something to watch out on "SQLAGENT.OUT" log if it gets too large. It is disabled (unchecked) by default. Enabling this, it will log all the job execution traces to SQLAGENT.OUT and may grow rapidly. 


Here is where to locate the "Include execution trace messages" feature.




The Include execution trace messages can be turned On or Off without restarting the agent.

Wednesday, April 10, 2019

Tuesday, April 9, 2019

MSSQL: SQL Server Job Agent detail.

SQL Server job agent has a limitation in reporting job steps error messages. First, it only maintains up to 9 most recent error events and detail trace do not get written to it.

https://docs.microsoft.com/en-us/sql/ssms/agent/sql-server-agent-error-log?view=sql-server-2017


In order to get more complete details, the errors need to be directed to a specific file. How to configure the error to be direct to a file will be shown here. This can be confusing to get to.



SQL Server Agent 
|-----Jobs
       |-----Name of the job (Right Click)
               |-----Properties
                         |-----Steps
                                 |-----New
                                        |-----Advanced page

                               [This where the user can allocate a file to store the errors in detail.]





             





The errors logged to the StoreJobFailureDetails will be very verbose.








MSSQL: Getting more detail information for failed jobs.

Often when a job failed, it provides trivial information for diagnostic. In order to expand the detail of the failed job, it can be done th...