Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 70

03/18/2021 BY: SAMUEL S 1

MONITORING AND
ADMINISTERING
DATABASE
03/18/2021 BY: SAMUEL S 2

LO -1:
START UP A DATABASE
03/18/2021 BY: SAMUEL S 3

• The goal of many information systems is to transform


data into information in order to generate knowledge that
can be used for decision making.

• In order to do this, the system must be able to take data,


put the data into context, and provide tools for
aggregation and analysis.

• A database is designed for just such a purpose.


03/18/2021 BY: SAMUEL S 4

Cont..
• A database is an organized collection of related information.

• It is an organized collection, because in a database, all data


is described and associated with other data.

• All information in a database should be related as well;


separate databases should be created to manage unrelated
information.
03/18/2021 BY: SAMUEL S 5

Cont..
• When you start up a database, you create an instance of that
database and you determine the state of the database.

• Normally, you start up an instance by mounting and opening


the database.
• Doing so makes the database available for any valid user to
connect to and perform typical data access operations.
03/18/2021 BY: SAMUEL S 6

Cont..
Here are few ways we penned down here which are used
for standalone instance of SQL Server.
• These are our Top 5 most commonly used methods:
1. Object Explorer in SSMS (SQL server Management Studio)
2. Windows Services.
3. Net Start Command.
4. Using executable Sqlservr.exe
5. SSCM - SQL Server Configuration Manager.
• In case of failover cluster instances we should be failover
cluster manager to stop and start service by taking resource
offline and online respectively.
03/18/2021 BY: SAMUEL S 7
1.

1. Object Explorer in SSMS (SQL server Management Studio)

• The object explorer in SSMS can also start both local and
remote servers as long as the remote server is connected and we
have remote-service rights.
• Right Click and select the required action – Start, Pause,
Resume, Stop. If we are not already connected, we can’t start a
stopped instance.
03/18/2021 BY: SAMUEL S 8

2. Windows Services
• This is one of the most common way used by all DBAs. Top
open services applet,
 we can either go to start > Run and
 type services.msc or
 by using the Windows Start, Programs,
 Administrative Tools, Services menu.
Then, double-click (or right click)
the MSSQLServer service, and
right-click the required action.

• If we want to start a SQL Server named instance, look for the service called
SQL Server (Instance Name).
• Let’s say our machine has named instance called SQL2014 then we would see
SQL Server (SQL2014), as shown below:
03/18/2021 BY: SAMUEL S 9

Cont..
03/18/2021 BY: SAMUEL S 10

3. The Net Start Command

• Methods so far are the one to use User Interface, but we can
also use the Net Start command.
• Type net start mssqlserver at the command prompt.

• To start a named instance, type net start MSSQL$instancename.


• In below command prompt, I have started default instance.
03/18/2021 BY: SAMUEL S 11

4. Using Sqlservr.exe executable


•We can also use sqlservr.exe to start SQL Server when we want
to enable debugging or troubleshoot etc.

•To start an instance of SQL Server, we must run the sqlservr.exe


program from the \binn directory, using CMD where the SQL
Server binary files are stored by default.
03/18/2021 BY: SAMUEL S 12

Cont..
03/18/2021 BY: SAMUEL S 13

5. SQL Server Configuration Manager

•This is the best way to control the services because we are using the tool which

is designed for this purpose.


•Start > Programs > Microsoft SQL Server (version) > Configuration Tools >

SQL Server Configuration Manager.

•Once we click on SQL Server Services on left, we can see all services on right

pane.
•Then we can right-click SQL Server (MSSQLServer), and then choose required

action.
•A green icon next to the server name and indicates that the service is running.
03/18/2021 BY: SAMUEL S 14

Cont..
03/18/2021 BY: SAMUEL S 15
03/18/2021 BY: SAMUEL S 16

General Knowledge of
Structured Query Language (SQL)
• Structured Query Language (SQL) is a programming
language that is typically used in relational database or data
stream management systems.

• It was developed by IBM in the early 1970s and is now an


official standard recognized by the American National
Standards Institute (ANSI) and the International
Organization for Standardization (ISO)
03/18/2021 BY: SAMUEL S 17

Cont..
• SQL, structured query language, is the standard language for
creating and maintaining relational databases.

• Most relational database management systems, even those with


proprietary languages of their own, support some version of
SQL
03/18/2021 BY: SAMUEL S 18

Monitor Database Start-Up and Operation For


Irregularities
• Database performance monitoring is a critical discipline.
• If there isn’t a code issue, there’s a good chance you have a database
issue.

• Key metrics such as CPU and memory usage can give you important
insights into your database’s performance.

• Monitoring system and SQL Server databases shows the metrics that
indicate server performance.

• Efficient monitoring provides enough information to diagnose problems,


blocks, bottlenecks, and to fix them.
03/18/2021 BY: SAMUEL S 19

Why Should You Invest in Database Monitoring?

Here are a couple of reasons why you should invest in


database monitoring:
 Reduce costs for your organization
 Shift from reactive to proactive monitoring, reducing the number of
issues
 Improve the performance of your database and application
 Analyze logs and use this knowledge to improve performance
 Gain better insights through database health and performance metrics.
 Percent CPU time used by the database process
 Available memory
 Available disk space
 Disk queue for waiting IO
 Percent virtual memory use
 Network bandwidth for inbound and outbound traffic
03/18/2021 BY: SAMUEL S 20

What is SQL Server Activity Monitor?


• SQL Server Activity Monitor is a feature in SQL Server
Management Studio that displays information about the SQL
Server processes and their effect on SQL Server
performance
1. Activity Monitor panes
 Activity Monitor consists of several panes – Overview, Processes, Resource
Waits, Data File I/O, and Recent Expensive Queries.

 Panes can be expanded and collapsed.

 The activities are queried only when the specific pane is expanded “Activity
Monitor runs queries on the monitored instance to obtain information for the
Activity Monitor display panes.
03/18/2021 BY: SAMUEL S 21

Cont..
The Overview pane
• The Overview pane contains the graphs for the most
important SQL Server instance information.
• The context menu contains the Refresh interval option that
can be set to a predefined value from 1 second to 1 hour
03/18/2021 BY: SAMUEL S 22

SQL Server Activity Monitor - Overview pane


• % Processor Time – is the percentage of time the
processors spend to execute threads that are not idle
• Waiting Tasks – is the number of tasks that are waiting for
processor, I/O, or memory to be released so the tasks can be
processed
• Database I/O – is the data transfer rate in MB/s from
memory to disk, disk to memory, or disk to disk
• Batch Requests/sec – is the number of SQL Server batches
received by the instance in a second
03/18/2021 BY: SAMUEL S 23

The Processes pane


• The Processes pane shows the information about the
currently running processes on the SQL databases, who
run them, and from which application.

• A tooltip for each table header shows a short description


of the column data and system views used to obtain it.
• Each column can be filtered using the filters in the column
header

• The context menu for the specific process provides options


to see the last T-SQL command batch for the process, kill it, or trace it
in SQL Server Profiler
03/18/2021 BY: SAMUEL S 24

The Processes pane in Activity Monitor


03/18/2021 BY: SAMUEL S 25

Cont…
• Session ID – is a unique value assigned by Database Engine to
every user connection. This is the spid value returned by the
sp_who procedure
• User Process – 1 for user processes, 0 for system processes.
The default filter is set to 1, so only user processes are shown
• Login – the SQL Server login that runs the session
• Database – the database name on which the process is running
• Task State – the task state, blank for tasks in the runnable and
sleeping state.
• The value can also be obtained using the sys.dm_os_tasks view,
as the task_state column. The states returned can be:
• PENDING: Waiting for a worker thread.
03/18/2021 BY: SAMUEL S 26

Cont..
• RUNNABLE: Runnable, but waiting to receive a quantum.

• RUNNING: Currently running on the scheduler.

• SUSPENDED: Has a worker, but is waiting for an event.

• DONE: Completed.

• SPINLOOP: Stuck in a spinlock.” [2]

• Command – the current command type. The value can also be obtained using the
sys.dm_exec_requests view, as the command column

• Application – the name of the application that created the connection

• Wait Time (ms) – how long in milliseconds the task is waiting for a resource. The value can
also be obtained using the sys.dm_os_waiting_tasks view, as the wait_duration_ms column.
03/18/2021 BY: SAMUEL S 27

CONT..
• Wait Type – the last/current wait type. The value can also be obtained
using the sys.dm_os_waiting_tasks view, as the wait_type column. The
waits can be resource, queue and external waits

• Wait Resource – the resource the connection is waiting for. The value
can also be obtained using the sys.dm_os_waiting_tasks view, as the
resource_description column

• Blocked By – the ID of the session that is blocking the task. The value
can also be obtained using the sys.dm_os_waiting_tasks view, as the
blocking_session_id column

• Head Blocker – the session that causes the first blocking condition in
a blocking chain
03/18/2021 BY: SAMUEL S 28

CONT..
• Memory Use (KB) – the memory used by the task. The value
can also be obtained using the sys.dm_exec_sessions view, as the
memory_usage column.

• Host Name – the name of the computer where the current


connection is made. The value can also be obtained using the
sys.dm_exec_sessions view, as the host_name column.

• Workload Group – the name of the Resource Governor


workload group[3]. The value can also be obtained using the
sys.dm_resource_governor_workload_groups view, as the name
column.
03/18/2021 BY: SAMUEL S 29

The Resource Waits pane


• Shows information about waits for resources
03/18/2021 BY: SAMUEL S 30

Resource Waits pane


• Wait Category – the categories are created combining closely related wait types.
The wait types are shown in the Wait Type column of the Processes pane
• Wait Time (ms/sec) – the time all waiting tasks are waiting for one or more
resources

• Recent Wait Time (ms/sec) – the average time all waiting tasks are waiting for
one or more resources

• Average Waiter Count – is calculated for a typical point in time in the last sample
interval and represents the number of tasks waiting for one or more resources

• Cumulative Wait Time (sec) – the total time waiting tasks have waited for one or
more resources since the last SQL Server restart, or DBCC SQLPERF last execution
• The Data File I/O pane - Shows information about the database files on the SQL
Server instance. For each database, all database files are listed – MDF, LDF and
NDF, their paths, and names
03/18/2021 BY: SAMUEL S 31

Data File I/O pane in Activity Monitor

MB/sec Read – shows recent read activity for the database file
MB/sec Written – shows recent write activity for the database file
Response Time (ms) – average response time for recent read-and-write
activity
03/18/2021 BY: SAMUEL S 32

Cont..
• The Recent Expensive Queries pane - Expensive queries are
the queries that use much resources – memory, disk, network.
• The pane shows expensive queries executed in the last 30
seconds.

• The information is obtained from the sys.dm_exec_requests


and sys.dm_exec_query_stats views.

• A double-click on the query opens the monitored statement.

• The context menu for the specific query provides options to


open the query in Query Editor, and show the execution plan
03/18/2021 BY: SAMUEL S 33

Recent Expensive Queries pane in SQL Server Activity


Monitor
03/18/2021 BY: SAMUEL S 34

CONT..
• Query – the SQL query statement monitored
• Executions /min – the number of executions per minute, since
the last recompilation. The value can also be obtained using the
sys.dm_exec_query_stats view, as the execution_count column
• CPU (ms/sec) – the CPU rate used, since the last recompilation.
The value can also be obtained using the
sys.dm_exec_query_stats view, as the total_worker_time column
• Physical Reads/sec, Logical Writes/sec, and Logical
Reads/sec – the rate of physical reads/logical writes/logical
reads per second. The value can also be obtained using the
sys.dm_exec_query_stats view, as the
total_physical_reads/total_logical_writes/total_logical_reads
columns.
03/18/2021 BY: SAMUEL S 35

CONT..
Average Duration (ms) – average time that the query runs.
Calculated based on the total_elapsed_time and execution_count
columns in the sys.dm_exec_query_stats view
Plan Count – the number of duplicate query plans. A large number
requires investigation and potential explicit query parameterization
Requirements to use Activity Monitor
• The permission necessary to view Activity Monitor is VIEW
SERVER STATE
• To view the Data File I/O pane, besides VIEW SERVER STATE, the
login must be granted CREATE DATABASE, ALTER ANY DATABASE,
or VIEW ANY DEFINITION permissions
• To kill a process, it’s necessary to be a member of the sysadmin
role.
03/18/2021 BY: SAMUEL S 36

How to start Activity Monitor


• There are several ways to start Activity Monitor – in the SQL
Server Management Studio toolbar click the activity Monitor
icon, use the keyboard shortcut Ctrl+Alt+A, or in Object
Explorer right-click the SQL Server instance and select
Activity Monitor.
• One more option is to set Activity Monitor to be opened
when SQL Server Management Studio is started
1. In the SQL Server Management Studio menu click Tools and then
Options
2. Open the Environment | Start Up tab
3. Select the Open Object Explorer and Activity Monitor option
03/18/2021 BY: SAMUEL S 37

On the next SQL Server Management Studio start up, Object Explorer will be shown on the
left and Activity Monitor on the right

 Activity Monitor is a SQL Server Management Studio feature that tracks some

of the most important metrics that affect performance.

 It shows the metrics in real time, without the out-of-the-box solution to save

them for later analysis.

 Filtering by a specific database/parameter is easy, but excluding a specific

value is not possible.

 Monitoring additional metrics is also not possible.

 Due to its limited set of features and monitored metrics, it’s not recommended

for in-depth performance monitoring


03/18/2021 BY: SAMUEL S 38

Monitor Data Storage Space


•While creating a database initial size of the database files (Data

File and Log File) can be defined by us, with the Autogrowth
and MAXSIZE parameter.

•The initial size will be the same as the model database if by

chance user forgot to define the initial size parameter while


creating a database or do the same intentionally.
03/18/2021 BY: SAMUEL S 39

Cont..d
03/18/2021 BY: SAMUEL S 40

Cont...d

At the beginning, size of the database can be set up using the

SIZE parameter and how much maximum space can be


occupied by a database can be defined using the MAXSIZE
parameter.

How fast a database file can reach its maximum space can be

resolved using the FILEGROWTH parameter. We are


facilitated in a way we define the FILEGROWTH parameter.
03/18/2021 BY: SAMUEL S 41

Cont..d

It leaves us with two options either by specific absolute value or by

percentage, while in both cases we must define the value in MB


format.
There will be no boundary on the growth of a database file if we do

not take care of defining the MAXSIZE parameter, as the default


value is UNLIMITED.
03/18/2021 BY: SAMUEL S 42

Cont..
03/18/2021 BY: SAMUEL S 43

Cont.
SQL Server can not commit any transaction to disk storage in the situation where disk

is full so it’s always good to define the MAXSIZE parameter beforehand to avoid
such situation.
UNLIMITED would fill up the disk storage but before that, it makes several spaces

tinier than before.

Even this has a very hazardous impact on the operating system as well if it doesn’t

have enough space to execute its system programs.

We also need to take into consideration the size of multiple databases before defining

the MAXSIZE attribute.


03/18/2021 BY: SAMUEL S 44

Get a list of database files with size for all


databases in SQL Server:
SELECT DB_NAME(database_id) AS database_name,
type_desc,
name AS FileName,
size/128.0 AS CurrentSizeMB
FROM sys.master_files
WHERE database_id > 6 AND type IN (0,1)
03/18/2021 BY: SAMUEL S 45

Get a list of databases file with size and free space for a database in SQL Server:

 sys.database_files DMV returns the database file with the details

 sys.database_files is a system object which returns information for the


selected database only

SELECT DB_NAME() AS DbName,


name AS FileName,
type_desc,
size/128.0 AS CurrentSizeMB,
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS
FreeSpaceMB
FROM sys.database_files
WHERE type IN (0,1);
03/18/2021 BY: SAMUEL S 46

Cont..
03/18/2021 BY: SAMUEL S 47

Cont..

Now, free space for the file in the above query result set will be

returned by the FreeSpaceMB column.


600 MB of space will be preoccupied with system disk into the

file system for the AdventureWorks2016CTP3_Log file


However, 362 MB is free to compress it up to 238 MB (600 –

362 = 238).
03/18/2021 BY: SAMUEL S 48

Cont..
•There are cases where in the Deep Security Manager database is occupying

too much disk space.


•This article shows how to check disk usage by top tables when using

Microsoft SQL Server as database

To view the disk usage:


• Open the SQL Server Management Studio (SSMS).
• Right-click database.
• Navigate to Reports > Standard Reports.
• Click Disk Usage by Top Table.
03/18/2021 BY: SAMUEL S 49

Cont..
03/18/2021 BY: SAMUEL S 50

 The requested data will be generated in a table.


03/18/2021 BY: SAMUEL S 51

SQL SERVER – Disk Space Monitoring –


Detecting Low Disk Space on Server
• There are two different ways to do the same. I personally prefer
method 2 as that is very easy to use and I can use it creatively
along with database name.
EXEC MASTER.. xp_fixeddrives
GO
• Above query will return us two columns, drive name and MB
free.
• If we want to use this data in our query, we will have to create
a temporary table and insert the data from this stored procedure
into the temporary table and use it.
03/18/2021 BY: SAMUEL S 52

CONt..

SELECT DISTINCT dovs.logical_volume_name AS LogicalName,


dovs.volume_mount_point AS Drive,
CONVERT(INT,dovs.available_bytes/1048576.0) AS FreeSpaceInMB
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
ORDER BY FreeSpaceInMB ASC
GO
03/18/2021 BY: SAMUEL S 53

CONT..
We can further modify above query to also include database name in the query as:-

SELECT DISTINCT DB_NAME(dovs.database_id) DBName,


dovs.logical_volume_name AS LogicalName,
dovs.volume_mount_point AS Drive,
CONVERT(INT,dovs.available_bytes/1048576.0) AS FreeSpaceInMB
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
ORDER BY FreeSpaceInMB ASC
GO

If you see a database name, multiple times, it is because your


database has multiple files and they are on different drives.
03/18/2021 BY: SAMUEL S 54

LO 3: MANAGE DATABASE
ACCESS
03/18/2021 BY: SAMUEL S 55

Performance and Activity Monitoring


• The goal of monitoring databases is to assess how a server is
performing.
• Effective monitoring involves taking periodic snapshots of
current performance to isolate processes that are causing
problems, and gathering data continuously over time to track
performance trends.
• Microsoft SQL Server and the Microsoft Windows operating
system provide utilities that let you view the current condition
of the database and to track performance as conditions change.
03/18/2021 BY: SAMUEL S 56

Cont..
Some of the common performance issues:
 High I/O during peak or maintenance times
 Memory pressure slowing down queries
 CPU pressure slowing down database server
 Concurrency issue from locking/blocking
03/18/2021 BY: SAMUEL S 57

Cont..
• High input/output represents disk subsystems doing too much
work.
• Particularly during peak or maintenance times.
• Input/output bottlenecks can be the hardest to troubleshoot
because they are usually manifested from memory or CPU issues.
• Furthermore, they can be also manifested from poorly written
queries and not having a good indexing strategy.
• Bear in mind that proper indexing and query tuning can have a
significant impact on all areas of SQL Server performance.
• All these factors are a good reason why it’s a good idea to look at
I/O last, even though it first in the list, just to rule out any other
things that can cause I/O problems.
03/18/2021 BY: SAMUEL S 58

Cont..
• All these factors are a good reason why it’s a good idea to look
at I/O last, even though it first in the list, just to rule out any
other things that can cause I/O problems.
• Memory pressure can simply become an issue when the system
doesn’t have enough RAM of SQL Server cannot allocate
enough memory or more commonly poor database and query
design.
• Lack of memory can also be caused by high I/O. Yeah right, it
goes both ways.
• This is, again, why it can get tricky when troubleshooting SQL
Server performance.
03/18/2021 BY: SAMUEL S 59

Cont…
 This command helps to get the Dynamic Management Views
for performance monitoring.

SELECT * FROM sys.dm_os_wait_stats dows


ORDER BY dows.wait_time_ms DESC;
03/18/2021 BY: SAMUEL S 60

Cont..
03/18/2021 BY: SAMUEL S 61

User Access and Privileges


• Permissions are the rights to access the database objects.
• Permissions can be granted to a user or role to allow that user or
role to perform operations such as selection, insertion or
modification of data rows.
• Each database object has an owner. By default, the owner is the
creator of an object, but the ownership can be transferred later
after the object has been created.
• In addition to the owner, the members of the sysadmin fixed
server roles have full permissions on all objects in all user and
system databases.
03/18/2021 BY: SAMUEL S 62

Cont..
• There is also a public role.
• The public role is a special database role to which each database
user belongs.
• The public role contains default access permissions for any user
who can access the database.
• This database role cannot be dropped, but it is strongly
recommended not to grant superfluous permissions to the public
role, because each databases user has the public role's
permissions.
03/18/2021 BY: SAMUEL S 63

Cont..
• SQL Server 2000 provides the GRANT, DENY, and REVOKE
statements to give or take away permissions from a user or role.
Permissions Types
• To perform any activity in a database, user must have the
appropriate permissions. These permissions fall into three
categories, which we call permissions types:
• Permissions to work with data and execute procedures (object
permissions).
• Permissions to create a database or an item in the database
(statement permissions).
• Permissions to utilize permissions granted to predefined roles
(implied permissions).
03/18/2021 BY: SAMUEL S 64

1. Object Permissions

The object permissions are the permissions to act on the database objects (such as tables,
stored procedures and views).

They consist of the following permissions:


o SELECT - Enables a user to select or read data from a table or view. The SELECT permission
can be applied to individual columns within a table or view, and may be applied to user-defined
functions.
o INSERT - Enables a user to insert new data to a table or view.
o DELETE - Enables a user to delete data from a table or view.
o UPDATE - Enables a user to update data in a table or view. The UPDATE permission can be
applied to individual columns within a table or view, not just the entire table.
o EXECUTE - Enables a user to execute a stored procedure.
03/18/2021 BY: SAMUEL S 65

2. Statement Permissions
o These are the permissions to create a database or an object in the database. These

permissions are applied to the statement itself, rather than to a specific object defined
in the database.

They consist of the following permissions:


o BACKUP DATABASE - The BACKUP DATABASE statement is used to back up an

entire database or one or more files or file-groups.


o BACKUP LOG - The BACKUP LOG statement is used to back up the transaction log.

o CREATE DATABASE - The CREATE DATABASE statement is used to create a new

database and the files used to store the database.


o CREATE DEFAULT - The CREATE DEFAULT statement is used to create an object

called a default.
03/18/2021 BY: SAMUEL S 66

3. Managing Permissions
• You can use the GRANT, DENY, and REVOKE statements to
give or take away permission from a user or role.
• The GRANT statement is used to give permissions to a user or
role.
• By using the GRANT statement, it is possible to assign
permissions to both statements as well as objects.

• You can use the GRANT statement with the WITH GRANT
OPTION clause to permit the user or role receiving the
permission to further grant/revoke access to other accounts.
03/18/2021 BY: SAMUEL S 67

Cont..
• This example grants the SELECT permission on the authors
table to Alex

GRANT SELECT ON authors TO Alex


• The DENY statement is used to deny a permission from a
security account in the current database and prevents the
security account from inheriting the permission through its
group or role memberships.

DENY SELECT ON authors TO Alex


03/18/2021 BY: SAMUEL S 68

Cont..
• The REVOKE statement is used to remove a previously granted
or denied permission from a user in the current database.
• You can use the REVOKE statement to remove both statements
and objects permissions.
REVOKE SELECT ON authors TO Alex
03/18/2021 BY: SAMUEL S 69

Cont..
In the next screen, Enter
1. Login Name

2. Select SQL Server authentication

3. Enter Password

4. Click Ok
03/18/2021 BY: SAMUEL S 70

The End!

You might also like