SlideShare a Scribd company logo
MS SQL SERVER TIPS
By Arman Aug-18
• Relationship Database Management System
• Database Design
• Indexes
• Useful Queries
RELATIONSHIP DATABASE MANAGEMENT SYSTEM
 Collection of information organized in tables
 Tables are also “Relations”
 Tables are constructed and associated to each other through shared fields.
 Fields are also “Columns” or “Attributes”
 A set of attributes comprises a record
 Records are also “Rows” or “Tuples”
 Tables are related through common fields designated as primary key and foreign keys.
 Allow us to find, update, and delete data quickly, and help to ensure accuracy
RELATIONSHIP DATABASE MANAGEMENT SYSTEM
 Primary Key :
 Primary key fields must be unique and cannot contain a null value
 Each table should have a primary key field[s].
 Composite Key: Using more than one field as a primary key.
 Foreign Key :
 Fields in a table that refer to the primary key in another table.
 The data in this field must exactly match data contained in the primary key
DATABASE DESIGN RELATIONSHIP TYPES
 One-to-one relationships
 One-to-many relationships
 Many-to-many relationships
 Mandatory or not?
DATABASE DESIGN NORMALIZATION
 First normal form (1NF)
 Second normal form (2NF)
 Third normal form (3NF)
DATABASE DESIGN NORMALIZATION 1NF
 First normal form (1NF)
I. It should only have single(atomic) valued attributes/columns.
II. Values stored in a column should be of the same domain
III. All the columns in a table should have unique names.
IV. And the order in which data is stored, does not matter.
DATABASE DESIGN NORMALIZATION 2NF
 Second normal form (2NF)
I. It should be in the First Normal form.
II. And, it should not have Partial Dependency.
DATABASE DESIGN NORMALIZATION 3NF
 Third normal form (3NF)
I. It is in the Second Normal form.
II. And, it doesn't have Transitive Dependency.
DATABASE DESIGN NORMALIZATION BCNF
 Boyce-Codd Normal Form (BCNF)
I. It should be in the Third Normal Form.
II. And, for any dependency A → B, A should be a super key.
DATABASE DESIGN NORMALIZATION 4NF
 Fourth Normal Form (4NF)
INDEXES TYPES
 Regulations
 1 Clustered Index per Table
 249 Non Clustered (2005)
 999 Non Clustered (2008+)
 16 Columns or 900 Bytes
 Clustered Index
 Nonclustered Index
 Unique index
 Filtered index
 Full-Text
 XML
CLUSTERED TABLE VS HEAP TABLE
Clustered Indexed Table Heap Table
CLUSTERED INDEX
 Clustered Index – sort order of the data within the table on the hard
drive and in memory.
 One per table
 Desired Qualities
 Narrow Data Type
 Unique
 Static
 Increasing
NONCLUSTERED INDEX
 Cover Common Queries
 Link to Clustered Indexes through Key
 Bookmark (Key/RID) Lookup
FRAGMENTATION
 Identifying Fragmentation
 2005
 DBCC SHOWCONTIG
 Above 2005
 sys.dm_db_index_physical_stats
Required Actions
avg_fragmentation_in_percent value Corrective statement
> 5% and < = 30% ALTER INDEX REORGANIZE
> 30% ALTER INDEX REBUILD WITH (ONLINE = ON)
USEFUL QUERIES
 Check index fragmentation using Transact-SQL
 2012
 2005
 DBCC SHOWCONTIG WITH TABLERESULTS, ALL_INDEXES;
 Currently running process
SELECT sqltext.TEXT,
req.session_id, req.status,req.command,
req.cpu_time, req.total_elapsed_time
,blocking_session_id ,wait_time ,wait_type
,logical_reads
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS
sqltext
SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(N'UniversoDB2018'),
OBJECT_ID(N'dbo.tblindividual'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b
ON a.object_id = b.object_id AND a.index_id = b.index_id;
USEFUL QUERIES
 Check index fragmentation using Transact-SQL
SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1,
((CASE qs.statement_end_offset
WHEN -1 THEN DATALENGTH(qt.TEXT)
ELSE qs.statement_end_offset
END - qs.statement_start_offset)/2)+1),
qs.execution_count,
qs.total_logical_reads, qs.last_logical_reads,
qs.total_logical_writes, qs.last_logical_writes,
qs.total_worker_time, qs.last_worker_time,
qs.total_elapsed_time/1000000 total_elapsed_time_in_S,
qs.last_elapsed_time/1000000 last_elapsed_time_in_S,
qs.last_execution_time, qp.query_plan
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY qs.total_logical_reads DESC -- logical reads
-- ORDER BY qs.total_logical_writes DESC -- logical writes
-- ORDER BY qs.total_worker_time DESC -- CPU time
READ TYPES
 Index Scan
 Table Scan (Heap)
 Index Seek
 Key Lookup
 RID Lookup (heap)
RULES FOR EXECUTION PLAN COMPILATION
 Build up most resource efficient plan
 AS little page reads as possible
 Fastest Operation possible
 Choosing Indexes:
 Prefer read range over scan
 Prefer small indexes over large

More Related Content

PPTX
Normalization by Ashwin and Tanmay
PPTX
SQL Basics
PPT
Fg d
PPTX
Sql(structured query language)
PDF
Normalization
PDF
Assignment 4
PPTX
Subqueries, Backups, Users and Privileges
PDF
Understanding about relational database m-square systems inc
Normalization by Ashwin and Tanmay
SQL Basics
Fg d
Sql(structured query language)
Normalization
Assignment 4
Subqueries, Backups, Users and Privileges
Understanding about relational database m-square systems inc

What's hot (19)

PPTX
Inner join and outer join
PDF
Assignment 3
PPTX
Entity relationship diagram - Concept on normalization
DOC
SQL Joins
PPTX
OrACLE RELATIONAL
PDF
Chapter8 my sql revision tour
PPTX
Sql joins
DOCX
Teradata imp
PPTX
Advance sqlite3
PPT
Sql join
PPT
Join sql
PPSX
Index Tuning
PPT
Displaying data from multiple tables
PDF
Solutions manual for guide to sql 9th edition by pratt
PPT
Normalization PRESENTATION
PPTX
Normalization
PDF
Chapter9 more on database and sql
Inner join and outer join
Assignment 3
Entity relationship diagram - Concept on normalization
SQL Joins
OrACLE RELATIONAL
Chapter8 my sql revision tour
Sql joins
Teradata imp
Advance sqlite3
Sql join
Join sql
Index Tuning
Displaying data from multiple tables
Solutions manual for guide to sql 9th edition by pratt
Normalization PRESENTATION
Normalization
Chapter9 more on database and sql
Ad

Similar to Ms sql server tips 1 0 (20)

PDF
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
PPTX
Query Optimization in SQL Server
PPS
07 qmds2005 session10
PPTX
dotnetMALAGA - Sql query tuning guidelines
PPTX
Sql performance tuning
PPTX
Index the obvious and not so obvious
PPTX
Elegant and Efficient Database Design
PPTX
presentation is on database for sql and stored procedures
PPTX
Sql server ___________session_17(indexes)
PPTX
SQL Server 2012 Best Practices
PPTX
Introduction of sql server indexing
PDF
PDF
153680 sqlinterview
PDF
PDF
Sql
PDF
Sql Interview Questions
PPTX
Database Performance Tuning
PDF
3 indexes
PPTX
Geek Sync | Understand Indexes to Write Better Queries
PPTX
Sql server lesson6
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
Query Optimization in SQL Server
07 qmds2005 session10
dotnetMALAGA - Sql query tuning guidelines
Sql performance tuning
Index the obvious and not so obvious
Elegant and Efficient Database Design
presentation is on database for sql and stored procedures
Sql server ___________session_17(indexes)
SQL Server 2012 Best Practices
Introduction of sql server indexing
153680 sqlinterview
Sql
Sql Interview Questions
Database Performance Tuning
3 indexes
Geek Sync | Understand Indexes to Write Better Queries
Sql server lesson6
Ad

Recently uploaded (20)

PDF
AI in Product Development-omnex systems
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
medical staffing services at VALiNTRY
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
history of c programming in notes for students .pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
AI in Product Development-omnex systems
VVF-Customer-Presentation2025-Ver1.9.pptx
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Odoo POS Development Services by CandidRoot Solutions
medical staffing services at VALiNTRY
wealthsignaloriginal-com-DS-text-... (1).pdf
How Creative Agencies Leverage Project Management Software.pdf
top salesforce developer skills in 2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Design an Analysis of Algorithms I-SECS-1021-03
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PTS Company Brochure 2025 (1).pdf.......
Operating system designcfffgfgggggggvggggggggg
history of c programming in notes for students .pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
CHAPTER 2 - PM Management and IT Context
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Wondershare Filmora 15 Crack With Activation Key [2025

Ms sql server tips 1 0

  • 1. MS SQL SERVER TIPS By Arman Aug-18
  • 2. • Relationship Database Management System • Database Design • Indexes • Useful Queries
  • 3. RELATIONSHIP DATABASE MANAGEMENT SYSTEM  Collection of information organized in tables  Tables are also “Relations”  Tables are constructed and associated to each other through shared fields.  Fields are also “Columns” or “Attributes”  A set of attributes comprises a record  Records are also “Rows” or “Tuples”  Tables are related through common fields designated as primary key and foreign keys.  Allow us to find, update, and delete data quickly, and help to ensure accuracy
  • 4. RELATIONSHIP DATABASE MANAGEMENT SYSTEM  Primary Key :  Primary key fields must be unique and cannot contain a null value  Each table should have a primary key field[s].  Composite Key: Using more than one field as a primary key.  Foreign Key :  Fields in a table that refer to the primary key in another table.  The data in this field must exactly match data contained in the primary key
  • 5. DATABASE DESIGN RELATIONSHIP TYPES  One-to-one relationships  One-to-many relationships  Many-to-many relationships  Mandatory or not?
  • 6. DATABASE DESIGN NORMALIZATION  First normal form (1NF)  Second normal form (2NF)  Third normal form (3NF)
  • 7. DATABASE DESIGN NORMALIZATION 1NF  First normal form (1NF) I. It should only have single(atomic) valued attributes/columns. II. Values stored in a column should be of the same domain III. All the columns in a table should have unique names. IV. And the order in which data is stored, does not matter.
  • 8. DATABASE DESIGN NORMALIZATION 2NF  Second normal form (2NF) I. It should be in the First Normal form. II. And, it should not have Partial Dependency.
  • 9. DATABASE DESIGN NORMALIZATION 3NF  Third normal form (3NF) I. It is in the Second Normal form. II. And, it doesn't have Transitive Dependency.
  • 10. DATABASE DESIGN NORMALIZATION BCNF  Boyce-Codd Normal Form (BCNF) I. It should be in the Third Normal Form. II. And, for any dependency A → B, A should be a super key.
  • 11. DATABASE DESIGN NORMALIZATION 4NF  Fourth Normal Form (4NF)
  • 12. INDEXES TYPES  Regulations  1 Clustered Index per Table  249 Non Clustered (2005)  999 Non Clustered (2008+)  16 Columns or 900 Bytes  Clustered Index  Nonclustered Index  Unique index  Filtered index  Full-Text  XML
  • 13. CLUSTERED TABLE VS HEAP TABLE Clustered Indexed Table Heap Table
  • 14. CLUSTERED INDEX  Clustered Index – sort order of the data within the table on the hard drive and in memory.  One per table  Desired Qualities  Narrow Data Type  Unique  Static  Increasing
  • 15. NONCLUSTERED INDEX  Cover Common Queries  Link to Clustered Indexes through Key  Bookmark (Key/RID) Lookup
  • 16. FRAGMENTATION  Identifying Fragmentation  2005  DBCC SHOWCONTIG  Above 2005  sys.dm_db_index_physical_stats Required Actions avg_fragmentation_in_percent value Corrective statement > 5% and < = 30% ALTER INDEX REORGANIZE > 30% ALTER INDEX REBUILD WITH (ONLINE = ON)
  • 17. USEFUL QUERIES  Check index fragmentation using Transact-SQL  2012  2005  DBCC SHOWCONTIG WITH TABLERESULTS, ALL_INDEXES;  Currently running process SELECT sqltext.TEXT, req.session_id, req.status,req.command, req.cpu_time, req.total_elapsed_time ,blocking_session_id ,wait_time ,wait_type ,logical_reads FROM sys.dm_exec_requests req CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext SELECT a.index_id, name, avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats (DB_ID(N'UniversoDB2018'), OBJECT_ID(N'dbo.tblindividual'), NULL, NULL, NULL) AS a JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id;
  • 18. USEFUL QUERIES  Check index fragmentation using Transact-SQL SELECT TOP 10 SUBSTRING(qt.TEXT, (qs.statement_start_offset/2)+1, ((CASE qs.statement_end_offset WHEN -1 THEN DATALENGTH(qt.TEXT) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2)+1), qs.execution_count, qs.total_logical_reads, qs.last_logical_reads, qs.total_logical_writes, qs.last_logical_writes, qs.total_worker_time, qs.last_worker_time, qs.total_elapsed_time/1000000 total_elapsed_time_in_S, qs.last_elapsed_time/1000000 last_elapsed_time_in_S, qs.last_execution_time, qp.query_plan FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) qt CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp ORDER BY qs.total_logical_reads DESC -- logical reads -- ORDER BY qs.total_logical_writes DESC -- logical writes -- ORDER BY qs.total_worker_time DESC -- CPU time
  • 19. READ TYPES  Index Scan  Table Scan (Heap)  Index Seek  Key Lookup  RID Lookup (heap)
  • 20. RULES FOR EXECUTION PLAN COMPILATION  Build up most resource efficient plan  AS little page reads as possible  Fastest Operation possible  Choosing Indexes:  Prefer read range over scan  Prefer small indexes over large

Editor's Notes

  • #7: Student and Branch Example Insertion Anomaly Updation Anomaly Deletion Anomaly
  • #8: Student and Branch Example Insertion Anomaly Updation Anomaly Deletion Anomaly
  • #9: Student and Branch Example Insertion Anomaly Updation Anomaly Deletion Anomaly
  • #10: Student and Branch Example Insertion Anomaly Updation Anomaly Deletion Anomaly
  • #11: In the table above, student_id, subject form primary key, which means subject column is a prime attribute. But, there is one more dependency, professor → subject. And while subject is a prime attribute, professor is a non-prime attribute, which is not allowed by BCNF.
  • #12: For a dependency A → B, if for a single value of A, multiple value of B exists, then the table may have multi-valued dependency. Also, a table should have at-least 3 columns for it to have a multi-valued dependency. And, for a relation R(A,B,C), if there is a multi-valued dependency between, A and B, then B and C should be independent of each other.
  • #14: HEAP Data is not stored in any particular order Specific data can not be retrieved quickly, unless there are also non-clustered indexes Data pages are not linked, so sequential access needs to refer back to the index allocation map (IAM) pages Since there is no clustered index, additional time is not needed to maintain the index Since there is no clustered index, there is not the need for additional space to store the clustered index tree These tables have a index_id value of 0 in the sys.indexes catalog view Clustered Indexed Table Data is stored in order based on the clustered index key Data can be retrieved quickly based on the clustered index key, if the query uses the indexed columns Data pages are linked for faster sequential access Additional time is needed to maintain clustered index based on INSERTS, UPDATES and DELETES Additional space is needed to store clustered index tree These tables have a index_id value of 1 in the sys.indexes catalog view
  • #15: Clustered Clustered indexes sort and store the data rows in the table or view based on their key values. These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be stored in only one order. The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. When a table has a clustered index, the table is called a clustered table. If a table has no clustered index, its data rows are stored in an unordered structure called a heap.
  • #16: Nonclustered Nonclustered indexes have a structure separate from the data rows. A nonclustered index contains the nonclustered index key values and each key value entry has a pointer to the data row that contains the key value. The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row locator is a pointer to the row. For a clustered table, the row locator is the clustered index key. You can add nonkey columns to the leaf level of the nonclustered index to by-pass existing index key limits, and execute fully covered, indexed, queries.