Lesson 7 SQL NoSQL
Lesson 7 SQL NoSQL
Non-Relational Data
2
Indexes Overview
• Index Considerations
• Best Practices
• Scenarios:
3
SQL Server Index Types
• Clustered index
• Non-Clustered Index
4
Clustered Index vs Non-Clustered Index
5
SQL Server Statistics
• Created on columns
• Created manually
• Cardinality
6
SQL Server Statistics
• Header
• Density
• Histogram
7
Demo
Demo 1 – Clustered and Non-Clustered Indexes
8
Locks and Blocking
• When locks are held for a long period of time, they cause blocking
• One process must wait for the other process to finish with the data and release the lock
before the second process can continue
• Lock types
• Shared - Used for read operations that do not change or update data, such as a SELECT
statement
9
Lock Hierarchy
10
SQL Server – Row Update
USE AdventureWorks
GO
BEGIN TRANSACTION
UPDATE [Person].[Address]
SET AddressLine1 = 'Lisbon, Portugal'
WHERE AddressID = 2
-- COMMIT TRANSACTION
11
SQL Server – Lock Escalation
12
Viewing Blocking by Using DMVs
• sys.dm_tran_locks
• Replaces syslockinfo and sp_lock
• Each row has information about both the resource and the request
• request_status = WAIT implies blocking
• sys.dm_os_waiting_tasks
• blocking_session_id > 0
• sys.dm_exec_requests
• status = suspended
• blocking_session_id > 0
• sys.dm_exec_sessions
• Join with each of the above for session details (join on session_id)
13
Demo
Demo 2 – Locks and Blocking
14
Additional Resources
15
Concurrency Concepts
ISOLATION
CONCURRENCY
16
Concurrency Issues
• Blocking
• Deadlock
• Dirty Read
• Non-Repeatable Read
• Phantom Read
17
SERIALIZABLE Isolation Level
• Shared locks on read rows are held until the end of the transaction
• Guarantees that all reads are repeatable, and that no new data can be
inserted until the transaction completes
18
REPEATABLE READ Isolation Level
• Shared locks on read rows are held until the end of the transaction
• Guarantees that data rows previously read within a transaction will remain consistent
until the transaction ends
• Unlike SERIALIZABLE isolation level, shared locks are acquired only on rows read
• If a transaction reads a table multiple times, and another transaction inserts new rows to that table,
query results may be different
• Best used when performing data changes while preventing changes to read data
rows
• Use with caution – this option will cause blocking
19
READ UNCOMMITED Isolation Level
• When attempting to read a row with an exclusive lock on it, query will read uncommitted data (dirty read)
• READ UNCOMMITED and (NOLOCK) are not silver bullets to solve blocking issues.
Use as needed and understand their side effects
20
READ COMMITED Isolation Level
• When OFF, shared locks are acquired while reading data rows and released when the read is complete
• When ON, no shared locks are acquired and instead row versioning is used for concurrency control
21
SQL Server Isolation Levels Overview
22
Demo
Demo 3 – Isolation Levels
23
Deadlock
• How to identify
• Client error message
• Profiler trace
• Xevent
24
Deadlock
25
Deadlock Graph – Profile Trace
26
Demo
Demo 4 – DeadLocks
27
Query Optimizer cost
SELECT * FROM
Sales.SalesOrderDetail
WHERE ProductID = 750
ORDER BY ProductID;
28
Demo
Demo 5 – Execution Plans
29
SSMS Reports
• CPU bottlenecks (and what queries are consuming the most CPU)
• Blocking
• Latch contention
30
Demo
Demo 6 – SSMS Performance Dashboard
31
SQL Server 2014 – Top New Features
• AlwaysOn Enhancements
32
SQL Server 2014 – Top New Features
• Delayed durability
33
SQL Server 2016 – Top New Features
• Query Store
• Polybase
• JSON Support
34
SQL Server 2016 - SQL Server Query Store
• Tracks:
• Query plans
• Runtime statistics
• Queries/plans history
35
SQL Server 2016 - SQL Server Polybase - Hadoop
36
SQL Server 2016 – Top New Features
37
SQL Server 2017 – Top New Features
• Python Support
• Graph Database
38
SQL Server 2019 – New Features
• Replication support
39
SQL Server 2019 – New Features
• Deploy a Big Data cluster with SQL Server and Spark Linux containers on
Kubernetes
40
SQL Server 2019 – New Polybase connectors
41
SQL Server 2019
42
SQL Server 2019
43
SQL Server 2022
44
Summary
• DeadLocks
45
Obrigado!