Final Exam
Final Exam
bases de données
www.imenemami.com/adminBD/cours.pdf
Plan
1. Introduction
2. Backup and Restore
3. Manage logins and server roles
4. Implement and maintain indexes
5. Import and export data
6. Manage SQL Server Agent
7. Manage and configure databases
8. Identify and resolve concurrency problems
9. Collect and analyse troubleshooting data
10. Audit SQL Server Instances
11. Additional SQL server components
Introduction
Downloading SQL Server Developer 2019
Download a free specialized edition
▪ Basic, Custom or Download Media
▪ Langage: English
▪ Package ISO or CAB
Editions and supported features of SQL Server 2019: Enterprise, Developer, Express,…
https://docs.microsoft.com/en-us/sql/sql-server/editions-and-components-of-sql-server-version-
15?view=sql-server-ver15
AdventureWorks
www.imenemami.com/adminBD/sampleBD/AdventureWorks.bak
Backup and Restore
Restoring AdventureWorks with SSMS
T-SQL (Transact-SQL)
Restoring a backup consists of creating a database containing exclusively all the data contained in
the backup
T-SQL (Transact-SQL)
j
Backing up Database
Backing up Database
BACKUP DATABASE [AdventureWorks2014] TO DISK =
N'E:\adminBD\DBA\AdventureWorksBackup'
WITH NOFORMAT, NOINIT,
NAME = N'AdventureWorks2014-Full-Database-Backup’,
SKIP, NOREWIND, NOUNLOAD, STATS = 1
Recovery Model
❖ SIMPLE
❖ FULL
❖ BULK LOGGED
Different backup models
❖ FULL BACKUP
❖DIFFERENTIAL BACKUP
Go
✓ Mirrored Media
❖Securityadmin
❖ Alter any login
❖Processadmin
❖ Alter any connection
❖Setupadmin
❖ Alter any linked server
❖Bulkadmin
❖ Administer Bulk operations
Manage access to the server
❖ Diskadmin
❖ Alter resources
❖Dbcreator
❖ Alter any database, create any database
❖Sysadmin
❖ Can perform any activity on the server
❖Public
❖ No server-level permission (except view any database and connect permission)
❖db_securityadmin
❖ Alter any role, create role, view definition
❖db_accessadmin
❖Alter any user, connect
❖db_backupoperator
❖Backup database, backup log, checkpoint
❖db_ddladmin
❖Data definition Langage commands in the database
Fixed database-level roles
❖ db_datawriter
❖ Grant Insert, update, delete on database
❖db_denydatawriter
❖Deny Insert, update, delete on database
❖db_datareader
❖Grant Select on database
❖db_denydatareader
❖Deny Select on database
❖public
❖No database level permissions (except some database permissionss for example: view any column
master key definition and select permission on system tables)
❖ Schema owner
Creating access to server/database with
least privilege
❖ Principle of least privilege
❖ Use fixed server roles
❖ Restrict use of sysadmin
❖ Assign permissions to role
❖ Use stored procedures and functions
❖Permission statements
❖ Grant, Deny, Revoke (REVOKE SELECT ON HumanResources.Department TO myNewDBRole)
❖ Owernership chains
Protect objects from being modified
use [AdventureWorks2014]
❖ Non-clustred indexes
❖Non-unique
Implement indexes
CREATE CLUSTERED INDEX [IX_NewTable_ID] ON [dbo].[NewTable]
(
[ID] ASC
)
Printer Red 5 C D
Bookcase Brown 6 ObjectName ColorName ID ObjectName ColorName ID
Computer Black 7
Bookcase Brown 6
Book White 8
Computer Black 7
Book Green 9
Table Glod 10
Fragmentation A B
ObjectName ColorName ID ObjectName ColorName ID
ObjectName ColorName ID
Table Black 1 Computer Gold 3 Table Black 1
Table Brown 2 Printer Black 4 Table Brown 2
Computer Gold 3 Printer Red 5 Table Gold 10
Printer Black 4 C D
ObjectName ColorName ID ObjectName ColorName ID
Printer Red 5
Bookcase Brown 6 Book White 8 Book Green 9
Computer Black 7 Bookcase Brown 6
Book White 8 Computer Black 7
Book Green 9
Book (Green) D
Table Glod 10
Book (White), Computer (Black) C
Computer(Gold), Printer A
Table B
Fragmentation A B
ObjectName ColorName ID ObjectName ColorName ID
ObjectName ColorName ID
Table Black 1 Computer Gold 3 Table Black 1
Table Brown 2 Table Brown 2
Computer Gold 3 Printer Red 5 Table Gold 10
Printer Black 4 C D
ObjectName ColorName ID ObjectName ColorName ID
Printer Red 5
Bookcase Brown 6 Book White 8 Book Green 9
Computer Black 7 Bookcase Brown 6
Book White 8 Computer Black 7
Book Green 9
Book (Green) D
Table Glod 10
Book (White), Bookcase (Brown), Computer (Black) C
Computer (Gold), Printer A
Table B
Fragmentation A B
ObjectName ColorName ID ObjectName ColorName ID
ObjectName ColorName ID
Table Black 1 Computer Gold 3 Table Black 1
Table Brown 2 Table Brown 2
Computer Gold 3 Printer Red 5 Table Gold 10
Printer Red 5 C D
ObjectName ColorName ID ObjectName ColorName ID
Bookcase Brown 6
Computer Black 7 Book White 8 Book Brown 11
Book White 8 Bookcase Brown 6 Book Green 9
Book Green 9 Computer Black 7
Table Glod 10
Book (Brown) Book (Green) D
Book Brown 11
Book (White), Compter (Black) C
Computer(Gold), Printer A
Table B
ObjectName ColorName ID
Bookcase Brown 6 E
Computer Black 7
Fragmentation A B
ObjectName ColorName ID ObjectName ColorName ID
ObjectName ColorName ID
Table Black 1 Computer Gold 3 Table Black 1
Table Brown 2 Table Brown 2
Computer Gold 3 Printer Red 5 Table Gold 10
Printer Red 5 C D
ObjectName ColorName ID ObjectName ColorName ID
Bookcase Brown 6
Computer Black 7 Book White 8 Book Brown 11
Book White 8 Book Yellow 12 Book Green 9
Book Green 9
Book (Brown) Book (Green) D
Table Glod 10
Book (White), Book(Yellow) C
Book Brown 11
BookCase Computer (Black) E
Book Yellow 12
Computer(Gold), Printer A
Table B
Fragmentation
❖ Reorganize
Computer(Gold), Printer A ObjectName ColorName ID
Table B
Book (White), Book(Yellow) C Computer Gold 3
Table B
Fragmentation
❖ Rebuild
❖ Drops the index and starts from scratch
❖ Sort out (Book, Bookcase, Computer,…)
Only use rebuild when it is absolutely necessary, when the fragmentation has reached such an
extent that you just have to delete it and start again.
Fragmentation
❖ Reorganize
❖ Rebuild
❖ Import data
❖Export data
Go
❖ Autoshrink
ALTER DATABASE [AdventureWorks2014] SET AUTO_SHRINK ON
Autoclose and Autoshrink
Design multiple file groups
❖Big Database: one filegroup ?
❖Primary data file (.mdf)
❖ Secondary data file (.ndf)
❖ Filegroups
❖ Data Files on different filegroups
Creating database with multiple
filegroups
Creating database with multiple
filegroups
Creating database with multiple
filegroups
CREATE DATABASE [DBAdatabase]
CONTAINMENT = NONE
ON PRIMARY
FILEGROUP [Secondary]
LOG ON
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'Secondary')
CREATE PARTITION FUNCTION [PartitionFunctionPartition](date) AS RANGE RIGHT FOR VALUES (N'2018-01-01', N'2022-01-01’)
[dateOfEntry]
) ON [PartitionSchemeParttition]([dateOfEntry])
COMMIT TRANSACTION
Partitionning
select *, $PARTITION.PartitionFunctionPartition(dateOfEntry) as PartitionNumber
from [dbo].[partitionTable]
select $PARTITION.PartitionFunctionPartition('2018-01-01')
Filegroup backup
❖ How can you manage a VERY BIG database ?
❖ How do you back that up ? It is huge (hours, days)
❖Not to backup the entirety
BACKUP DATABASE [DBAdatabase]
FILEGROUP = N'Secondary' TO
DISK = N'C:\Program Files\
Microsoft SQL Server\MSSQL15.MSSQLSERVER
\MSSQL\Backup\DBAdatabase.bak'
Filegroup and Page restore
RESTORE DATABASE [DBAdatabase] FILE = N'DBAdatabase2' FROM DISK = N'C:\Program
Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\DBAdatabase.bak'
WITH FILE = 1, NOUNLOAD, STATS = 10
❖ Enable Autogrowth
DBCC
dbcc shrinkdatabase(DBAdatabase,20)
❖ File Id
◦ select * from sys.database_files
dbcc shrinkfile(3,truncateonly)
dbcc shrinkfile(DBAdatabase3,emptyfile)
Implement and configure contained
databases and logins
❖ Moving database from one particular instance of sql server to another
❖ A contained database
❖ A database that is isolated from the instance of SQL Server
❖Prefix compression
✓ Stores commonly used prefixes elsewhere
✓ Prefix values are replaced by a reference to the prefix
❖Dictionary compression
❖ Replaces commonly used values
Data Compression
Data Compression
ALTER TABLE [HumanResources].[Employee] REBUILD PARTITION = ALL
WITH
(DATA_COMPRESSION = PAGE
)
ALTER INDEX [IX_Employee_OrganizationNode] ON
[HumanResources].[Employee] REBUILD PARTITION = ALL WITH
(DATA_COMPRESSION = PAGE)
❖ Sparse columns require more storage space for non-NULL values than the space required for
identical data that is not marked SPARSE
❖what percent of the data must be NULL for a net space savings ?
❖Estimated space savings by data type
https://docs.microsoft.com/en-us/sql/relational-databases/tables/use-sparse-columns?view=sql-
server-ver15
Sparse columns
create table sparsetable (heading1 nvarchar(10) sparse null)
Columnstore Indexes
❖ Clustered and nonclustered columnstore index
❖Live locking- Shared locks prevent another process from acquiring exclusive locks (but one
process wins then the next process wins)
dbcc traceon(1204,-1)
dbcc traceon(1222,-1)
Examine deadlocking issues using the
SQL server logs
Examine deadlocking issues using the
SQL server logs
Quiz
Collect and analyse troubleshooting data
Collect trace data by using SQL Server
Profiler
❖ SQL Server Profiler
❖ DMVs
❖ Performance monitor
Diagnose performance problems with
DMVs
❖CPU usage
select current_workers_count, work_queue_count, pending_disk_io_count
from sys.dm_os_schedulers
where scheduler_id <=255
❖ Processor
❖Processor : % Privildged Time
❖Processor : % User Time
❖ System: Processor queue length
IO, Memory and CPU bottlenecks
❖ IO Primary
❖PhysicalDisk: Avg Disk sec/Write
❖PhysicalDisk: Avg Disk sec/Read
❖ IO Secondary
❖PhysicalDisk: Avg Disk queue length
❖PhysicalDisk: Disk Bytes/sec
❖ PhysicalDisk: Disk Transfer/sec
Quiz
Audit SQL Server Instances
Implement a security strategy for
auditing and controlling the instance
❖ Server-level audits
❖ Database-level audits
❖Components of audits
❖ Audit itself
❖ Server audit specification
❖ Database audit specification
❖ Target
Configure server audits
Configure server audits – monitor the
attemps to connect
Configure server audits – log file
Monitor elevated privileges
❖A fixed list of what the privileges currently are:
select princ.name, perm.permission_name
from sys.server_permissions as perm join
sys.server_principals as princ on
perm.grantee_principal_id =
princ.principal_id
select *
from [Person].[Address]
where CONTAINS(AddressLine1, 'Drive NEAR Glaze')
Filestream
▪ Allows to store unstructed data like documents and images on the file system.
▪ Filestream is not automatically enabled
Filestream
exec sp_configure filestream_access_level, 2
reconfigure
▪Store files and documents in special tables in SQL Server called FileTables