lunes, 29 de junio de 2015

SQL SERVER – Find Current Location of Data and Log File of All the Database

As I am doing lots of experiments on my SQL Server test box, I sometime gets too many files in SQL Server data installation folder – the place where I have all the .mdf and .ldf files are stored. I often go to that folder and clean up all unnecessary files I have left there taking up my hard drive space. I run following query to find out which .mdf and .ldf files are used and delete all other files. If your SQL Server is up and running OS will not let you delete .mdf and .ldf files any way giving you error that file already in use. This list also helps sometime to do documentation of which files are in being used by which database.
SELECT name, physical_name AS current_file_locationFROM sys.master_files
Following is the output of files used by my SQL Server instance.
mastefiles SQL SERVER   Find Current Location of Data and Log File of All the Database

How to monitor your SQL Server instances and databases

Monitoring SQL Server instances and databases provides information necessary to diagnose and troubleshoot SQL Server performance issues, as well as to fine tune SQL Server. Optimal performance is not easy to define and set, as there is usually a trade-off between multiple software and hardware factors. It also depends on your environment, business requirements, and company policy.

While slow reporting may be acceptable in a small factory, it’s not in large enterprises where slowdowns, hiccups, and bottlenecks affect a large number of users and can significantly affect business. The listed issues are usually unacceptable and must be fixed as soon as possible.
Once SQL Server performance is tuned, it has to be constantly monitored, as everyday data, schema, and configuration changes often lead to a situation where additional, manual tuning is needed. The most common example is obsolete statistics – a query runs well and then, without any obvious reasons, becomes very slow.
To have a fine tuned, optimally performing SQL Server instance, a DBA must be proactive and to monitor system performance constantly, define normal trends and set baselines for specific SQL Server performance metrics, be notified about metrics that are out of the normal value range, and take adequate actions.

What SQL Server metrics to monitor?

The metrics that should be monitored depend on your performance goals. However, there is a range of commonly monitored metrics that provide the information sufficient for basic troubleshooting. Based on their values, additional, more specific metrics could be monitored in order to find the root cause of the problem.
These commonly monitored SQL Server performance metrics are memory and processor usage, network traffic, and disk activity.
Besides monitoring SQL Server parameters, it’s recommended to monitor parameters for the specific database, as well as Windows system parameters.
The commonly monitored metrics are: processor time, processor queue length, page reads and writes per second, page life expectancy, target and total server memory, buffer cache hit ratio, batch requests, processor utilization, lazy writes, network usage, paging, user connections, etc.
SQL Server provides two built-in monitoring features: Activity Monitor and Data Collector.

Activity Monitor

Activity Monitor tracks only the most important SQL Server performance metrics. To obtain them, it executes queries against its host SQL Server instance every 10 seconds. The performance is monitored only while Activity Monitor is opened, which makes it a lightweight solution with almost no overhead.
The metrics are shown in 5 collapsible panes: Overview, Processes, Resource Waits, Data File I/O, and Recent Expensive Queries.
The Overview pane shows the processor time percentage, number of waiting tasks, database I/O operations in MB/sec, and the number of batch requests.
The Processes pane shows currently running SQL Server processes for each database on the instance. The information shown is: login, application, and host used, tasks state and command, wait time, etc. The information in the table can be filtered by the specific column value.
The Process pane context menu provides a feature useful for deeper analysis and troubleshooting. That’s tracing the selected process in SQL Server Profiler.
The Resource Waits pane shows waits for different resources: memory, compilation, network, etc.
It shows the wait time (the time SQL Server tasks are waiting on system resources), recent wait time, cumulative wait time, and average waiter counter.
The Data File I/O pane shows a list of all database files: MDF, NDF, and LDF, their names and paths, recent read and write activity, and response time.
The Recent Expensive Queries pane shows the queries executed in the last 30 seconds that used most of hardware resources: processor, memory, disk, and network. The context menu enables opening the query in a SQL Server Management Studio query tab and opening its execution plan.

How to use Activity Monitor

Activity Monitor can be opened via the SQL Server Management Studio toolbar’s Activity Monitor icon, keyboard Ctrl+Alt+A shortcut, or the SQL Server instance context menu in Object Explorer.
As shown, Activity Monitor tracks only a pre-defined set of the most important SQL Server performance metrics. Additional metrics cannot be monitored; the monitored ones cannot be removed. Only real time monitoring is possible. There’s not built-in option to save metrics history for later use. Therefore, Activity Monitor is useful for current monitoring and basic troubleshooting; for any deeper analysis and performance tuning, a monitoring tool where monitored metrics can be selected, threshold values defined, and historic data stored is necessary.

Data Collector

Data Collector is another SQL Server Management Studio built-in performance monitoring and tuning feature. It collects performance metrics from SQL Server instances, saves it in a local repository, so they can be used for later analysis. It uses Data Warehousing, SQL Server Agent, and Integration Services.
Unlike Activity Monitor, Data Collector allows you to specify the metrics you will monitor. It offers three built-in monitor metrics sets (data collectors) with the most important and commonly monitored performance metrics. To monitor additional performance metrics, custom data collectors can be created via T-SQL code or API.

How to use Data Collector

Make sure that SQL Server Integration Services are installed and SQL Server Agent, Management Data Warehouse, and Data Collection are enabled.
  1. Expand Management in SQL Server Management Studio Object Explorer
  2. In Data Collection context menu select Configure Management Data Warehouse
  3. Select Set up data collection
  4. Click Next
  5. Select the SQL Server instance name and the database that will host the management data warehouse, and the local folder where the collected data will be cached
  6. Click Next, review the settings, and click Finish
Data Collection provides three pre-defined sets, available in the System Data Collection Sets folder in the Object Explorer Management | Data Collection node: Disk Usage, Query Statistics, and Server Activity. Each has its built-in report.
The Disk Usage data collection set collects data for the database data files (MDF and NDF), transaction log file (LDF), and I/O statistics.
The Disk Usage built-in report is available in the Data Collection context menu. It shows disk space used by the database files, growth trends, and average day growth.
The Query Statistics data collection set collects query code, statistics, activity, and query execution plans for the 10 most expensive queries.
The Server Activity data collection set collects data about processor, memory, disk I/O, and network used. The report shows CPU, memory, disk I/O, and network usage, SQL Server waits, SQL Server instance, and operating system activity.
As shown above, Data Collection has to be configured and started in order to capture data. It provides three built-in collection sets and adequate reports. There is no wizard for adding additional metrics; it has to be done through code.
One of the Data Collector advantages is that it doesn’t have to collect data all the time, but only in pre-defined schedules, and thus adds less overhead. A downside of the feature is that there’s no option to select the databases on the SQL Server instance that will be monitored, all databases will be monitored, which adds overhead, noise to the collected data, and requires more disk space.
The feature is not available in SQL Server versions earlier than 2008 and in editions other than Enterprise, Standard, Business Intelligence, and Web.
Unlike Activity Monitor, there is no option to watch real-time graphs, but captured data can be saved for a specified number of days. The feature provides basic performance metrics, but coding knowledge is required for monitoring any additional metrics.

ApexSQL Monitor

ApexSQL Monitor is a SQL server monitoring software that monitors system, SQL Server, and database performance in real time through more than 30 counters. It allows users to select the SQL Server instances and databases that will be monitored, to select the metrics for each database, SQL Server instance, and machine, and to specify the metrics and threshold values that if exceeded, the alerts will be raised. ApexSQL Monitor shows the performance metrics in real-time graphs, and stores the corresponding metrics data in a central repository database for later use.
The captured performance metrics and queries data are shown as a graphs in six default tabs, but also in user created custom tabs.
The Overview tab shows the most important SQL Server information and configuration and SQL Server wait stats and performance metrics (buffer cache hit ratio, full scans per second, lazy writes per second, and lock requests per second).
Overview tab - image 1
Overview tab - image 2
The System performance tab shows the operating system performance metrics: processor queue length/utilization of processor time, network utilization, paging file usage, page input per second, pages per second, average bytes per write/read, average and current disk queue length, average read/write time, disk writes/reads per second, free space and available memory in MB.
System performance tab
The SQL Server performance tab shows metrics related to SQL Server instance performance: the number of batch requests, compilations and re-compilations per second, longest running transaction, full scans, index searches, forwarded records, free list stalls, lazy writes, lock requests, and deadlocks per second, user connections, target and total server memory in KBs, memory grants pending, buffer cache hit ratio, page life expectancy, page reads/writes, and page splits per second.
SQL Server performance tab
The Database performance tab shows details about database data and transaction log file size, and number of transactions per second.
Database Performance tab
The Query waits tab shows the top queries with the largest wait time during the selected time period, including the graphical presentation of the queries and their wait times. Also, single or grouped query details and query plans can be reviewed and analyzed.
Query waits tab
The Query performance tab shows the top queries that used the most resource during the selected time period, including the graphical presentation of the slowest queries by average CPU time, average execution time, and I/O per second. The number of queries can be selected by the user.
Query performancetab
Custom tabs can be molded for individual users need. Users can make and set their own tabs to be displayed so as to know on critical metrics quick and easy after they start the ApexSQL Monitor. The custom tabs can be created to provides an at-a-glance overview of the most important system, SQL Server instance, and database metrics arranged in a way that can give the easy insight of the potential problem

How to use ApexSQL Monitor

  1. Start ApexSQL Monitor
  2. Click Add in the menu to add the SQL Server instance you want to monitor. All monitored SQL Server instances will be shown in the SQL Server instances list on the left
  3. To configure the monitored performance metrics, click Metrics in the menu. The metrics monitored can be configured on the device, database, SQL Server instance, and machine level
    Metrics tab
  4. In the same screen, you can configure alerts. To enable an alarm, select the Alerting check box and enter the threshold values, or use baseline threshold if baseline values are calculated
  5. After you’ve set the alerts and metrics, click Apply at the top of the form
If there’s an alert, it will be indicated by an icon next to the SQL Server instance in the Servers list. To see the list and details for active alerts, click Alerts in the menu. It shows the performance metric, start and end time, and number of occurrence for each severity level.
Alerts information
ApexSQL Monitor is a monitoring solution easy to start and configure, and no coding knowledge is required. It provides granular monitoring, and unlike native SQL Server monitoring solutions, it enables selecting the metrics, as well as databases that will be monitored. Different metrics can be monitored for each device, database, SQL Server instance, and operating system. This reduces overhead added by the monitoring process.
Unlike Activity Monitor and Data Collector, ApexSQL Monitor shows the metrics in real-time graphs, stores historic data in a central repository, and displays the historic data in graphs. Alerts can be configured in a few clicks, with a specific threshold value entered for low, medium, and high severity alerts.

Server Procedure Cache

It is possible to clear out the entire SQL Server procedure cache using DBCC FREEPROCCACHE
The procedure cache is where SQL Server will cache execution plans after they have been compiled. The benefit of this caching is that there is no need for the execution plans to be compiled at run time. This compiling operation typically consumes resource and slows down the execution time of the query.
The obvious disadvantage of clearing out the plan cache is that all execution plans for your SQL Server instance are recompiled upon execution which may slow things down temporarily until the cache is re-populated. Great for development, give thought before executing in production.
You may be under pressure to quickly get performance back to normal but it’s better to laser target the offending queries if you have time and address accordingly. You can clear out the procedure cache for specific queries as we will see further down the post.

Running DBCC FREEPROCCACHE

1DBCC FREEPROCCACHE;
…and you should be presented with the following informational message:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
This is optional – WITH NO_INFOMSGS and will simply suppress the informational message above.
1DBCC FREEPROCCACHE WITH NO_INFOMSGS;
Upon executing, messages with be written to the SQL Server error log for each cache store in the plan cache.
Here is an example:
SQL Server has encountered 1 occurrence(s) of cachestore flush for the ‘Object Plans’ cachestore (part of plan cache) due to ‘DBCC FREEPROCCACHE’ or ‘DBCC FREESYSTEMCACHE’ operations.
Using DBCC FREEPROCCACHE to clear specific execution plans from the cache
You first need to pinpoint the identifier of the execution plan which you want to clear out of the cache. This is known as a “plan handle” and you can find the plan handles and the cached SQL by issuing a query against sys.dm_exec_cached_plans and sys.dm_exec_sql_text
1SELECT cp.plan_handle, st.text
2FROM sys.dm_exec_cached_plans cp
3CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
4WHERE st.text LIKE N'%ThePlanYouAreLookingFor%'
So here I will clear a plan from the cache, firstly by running this query to get the plan inserted to the plan cache.
1SELECT TOP 1 * FROM Person.Person;
Now I will attempt to find the plan_handle for the execution plan.
1SELECT cp.plan_handle, st.text
2FROM sys.dm_exec_cached_plans cp
3CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
4WHERE st.text LIKE '%Person.Person%'
dbcc freeproccache clear sql server plan cache
So I can see that I want to clear out the cache for the second row as displayed in the screenshot.
So add the plan_handle and say bye to that query plan. :) (The plan handle has been deliberately shortened in the code below otherwise my CMS has trouble displaying it :) )
1DBCC FREEPROCCACHE (0x060006002FE61B1D40FDAFF501000000010000....);