SlideShare uma empresa Scribd logo
1 de 24
SQL Good Practices
Basic Concepts
Deepak Mehtani
Background


RDBMS is not based on regular programing paradigm



It operates on mathematical concept of sets



Sets are groups that have union or intersection or – or X operations



Most misunderstood concept – sets are ordered?


No sets do not guarntee order – very important to know in terms of
RDBMS

11/7/2013

2
Strategic Imperatives


The approach should provide a stable implementation and ease of
management



Provide ability to adapt new design paradigms moving forward



Ease of maintenance for prior code by means of documentation and
code clarity

11/7/2013

3
What’s new in SQL Server 2012


Editions & licensing




Three versions – Standard, Business Intelligence and Enterprise

xVelocity – is Microsoft SQL Server's family of memory-optimized and
in-memory technologies. These are next-generation technologies built
for extreme speed on modern hardware systems with large memories
and many cores


xVelocity In-Memory Analytics Engine (used in PowerPivot and
Analysis Services)



xVelocity Memory-Optimized Columnstore Index (used in the SQL
Server database).



Self Bi – PowerView



Data compression – high performance



Data Quality - Maintain the quality of data and ensure that the data is
suited for business usage
11/7/2013

4
xVelocity – ColumnStore


Column store - In a column store, values from a single column (from multiple
rows) are stored contiguously, potentially in a compressed form



Relational database management systems traditionally store data in row-wise
fashion. The values comprising one row are stored contiguously on a page.
We sometimes refer to data stored in row-wise fashion as a row store



Columnstore index – In SQL Server, a columnstore index is data stored in
column-wise fashion that can be used to answer a query just like data in any
other type of index


A columnstore index appears as an index on a table when examining
catalog views or the Object Explorer in Management Studio



The query optimizer considers the columnstore index as a data source for
accessing data just like it considers other indexes when creating a query
plan

More information:
http://social.technet.microsoft.com/wiki/contents/articles/3540.sql-servercolumnstore-index-faq-en-us.aspx
11/7/2013

5
Power View


Power View is a feature of SQL Server 2012 Reporting Services that is an
interactive data exploration, visualization, and presentation experience



Provides intuitive ad-hoc reporting for business users such as data analysts,
business decision makers, and information workers



Users can easily create and interact with views of data from data models
based on PowerPivot workbooks published in a PowerPivot Gallery, or tabular
models deployed to SQL Server 2012 Analysis Services (SSAS) instances



Power View is a browser-based Silverlight application launched from
SharePoint Server 2010 that enables users to present and share insights with
others in their organization through interactive presentations

11/7/2013

6
Data Compression


DBA can compress tables and indexes to conserve storage space at a slight
CPU cost. One of the main design goals of data compression was to shrink
data warehouse fact tables



SQL Server provides two methods, Page and Row compression, to reduce data
storage on disk and speed I/O performance by reducing the amount of I/O
required for transaction processing. Page and row compression work in
different, yet complementary



Page compression uses an algorithm called “deduplication.” When
deduplicating, as the name implies, SQL Server looks for duplicate values that
appear again and again within the data page



Using page compression, SQL Server can remove such duplicate values within
a data page by replacing each duplicate value with a tiny pointer to a single
appearance of the full value



By comparison, row compression does not actually use a compression
algorithm per se. Instead, when row compression is enabled, SQL Server
simply removes any extra, unused bytes in a fixed data type column, such as a
CHAR(50) column



Page and row compression are not compatible, but by enabling page
compression SQL Server automatically includes row compression
11/7/2013

7
Data Quality Service


Aggregating data from different sources that use different data standards can
result in inconsistent data, as can applying an arbitrary rule or overwriting
historical data. Incorrect data affects the ability of a business to perform



Aggregating data from different sources that use different data standards can
result in inconsistent data, as can applying an arbitrary rule or overwriting
historical data. Incorrect data affects the ability of a business to perform



Features include


Data Cleansing – modification of incorrect or incomplete data



Matching – identification of semantic duplicates



Reference Data Services – verification of quality of data using
reference data provider



Profiling – analysis of a data source to provide insight into the quality of
the data



Monitoring – tracking and determination of the state of data quality
activities



Knowledge Base – Data Quality Services is a knowledge-driven
solution that analyzes data based upon knowledge that you build with
DQS.

11/7/2013

8
Layered Coding Approach


Standardized way for coding




Code level comments




Layered coding – database, business and user interface

Standardized approach on both application and database level

Primary focus of this presentation is at database designing and coding

11/7/2013

9
Resource Utilization


Non-scalable code


Only so many people can access at
the same time



This is caused by



Inefficient use of resources



11/7/2013

Resource locking

Unnecessary use of locks or
transactions that were never
committed or rolled back

10
Unstructured Data – Paradigm Shift


Non-scalable code


Not only reduces performance



Increases maintenance



Requires more space


Disk space



Disk defragmentation



Requires continuous log file
management, sizing and maintenance



Increased maintenance cost for the
database
11/7/2013

11
Basic Design – that we forget


Defining a table


Key points


Identity columns



Define primary key (if not easily defined then use identity
columns)



Define indices



Huge performance difference using index and primary key



Also helps in joining other tables

11/7/2013

12
Truncate or Delete


Truncate vs. Delete





Using truncate is better than
delete
Why?

What do you think is better for import
tables?

11/7/2013

13
Looping in SQL


Cursors


Necessary evil



Resource hog – disk, network bandwidth (for result transmission
line by line)



Use read only cursor, if there is no update with fast forward option
and auto fetch to get some performance gain




http://technet.microsoft.com/enus/library/aa172573(SQL.80).aspx

Avoid cursors as much as possible

11/7/2013

14
Transactions


Using Transactions


Helpful in recovering from a problem / unexpected situation



Transactions should start as late as possible in the procedure
and end as early as possible to reduce the locking time



Make sure transactions are always rolled back or committed



Handle error using @@error and roll back transactions in
such a case



Use “with Mark” option to add the name to the transaction log
that can be used as a restore point if needed


11/7/2013

http://msdn.microsoft.com/en-us/library/ms188929.aspx

15
Locks & Deadlocks


Deadlocks and using no locks



Deadlock occurs when two users have
lock on separate objects and each user
is trying to lock other user’s resource


These are automatically detected
and resolved by SQL with one of the
transactions rolled back with an
error code of 1205



Using no locks may be helpful



No locks does not lock a record for read
or write


Advantage?



Pit fall?



Read isolation transaction better
than no lock

11/7/2013

16
Temporary Tables


Temp tables



Using # tables vs. @tables


# tables – pros and cons?



@tables – pros and cons?



Which one should be used for a
stored procedure that is called
very frequently

11/7/2013

17
User Defined Functions


Using User Defined Functions (UDF)


UDF are useful for calculations or results based on some input



UDF are slower than built-in functions



What will happen if we use a UDF in a


Select statement?



Join condition?



Where clause?



Any of the above in a while loop or cursor?

11/7/2013

18
Capturing Error


Using @@error (also trapping errors at application level to close sql
connection and roll back transactions)



Checking @@error variable after and insert and / or update can help
us determining if we need to roll back or commit a transaction



Using Try-catch construct
BEGIN TRY
-- Generate divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
-- Execute error retrieval routine.
EXECUTE usp_GetErrorInfo;
END CATCH;

11/7/2013

19
Joins vs. exists or in and not exists or not in


Using left joins with null conditions instead of not exist



Example below
Select z.first_name+' '+z.last_name AS [name],
z.[user_id]
From users z
inner join usergroup zug ON z.[userid] = zug.[userid]
and (z.first_name +z.last_name is not null)
and z.[user_id] NOT IN (SELECT [user_id] FROM Superuser)
inner join groups zg ON zug.group_id = zg.group_id
Where zg.[name]='ADMINISTRATOR'
Order by [name]



We could probably replace the not in with a left join on a = null


Joins are faster than “in” and “not in” and use index

11/7/2013

20
Low hanging fruits


Should Select * be used more frequently?



Scope_Identity vs. select max?



Utilize query analyzer / management studio to view query plan


Analyze for optimization and potential scalability problems



Analyze any potential bottleneck / blocking, use sql trace



Avoid dynamic queries



Use joins potentially on primary key and / or indexed columns



Do not use SP_ in the name of a stored procedure




First reference is checked in master database

Using count(*) vs. Count(Primary key) column


Need to have an ID index

11/7/2013

21
Sub-Queries


Sub-Queries



Avoid sub-queries if possible, they can be a resource hog



ClaimBatch_listDetailXml:
(((@searchClaimType = 'ALL'AND @userId IN( SELECT UserId
FROM NSFClaimTrack nct WHERE nct.UserId = @userId

11/7/2013

22
Conclusion


These are guidelines that will help in


Building a robust product



Standardized way for all programmer



Ease of understanding of code



Clear logic understanding



Easy maintenance

11/7/2013

23
Avoid This!

11/7/2013

24

Mais conteúdo relacionado

Mais procurados

Crystal xcelsius best practices and workflows for building enterprise solut...
Crystal xcelsius   best practices and workflows for building enterprise solut...Crystal xcelsius   best practices and workflows for building enterprise solut...
Crystal xcelsius best practices and workflows for building enterprise solut...
Yogeeswar Reddy
 
01 power center 8.6 basics
01 power center 8.6 basics01 power center 8.6 basics
01 power center 8.6 basics
uthayan87
 
Informatica and datawarehouse Material
Informatica and datawarehouse MaterialInformatica and datawarehouse Material
Informatica and datawarehouse Material
obieefans
 

Mais procurados (20)

People soft basics
People soft basicsPeople soft basics
People soft basics
 
Crystal xcelsius best practices and workflows for building enterprise solut...
Crystal xcelsius   best practices and workflows for building enterprise solut...Crystal xcelsius   best practices and workflows for building enterprise solut...
Crystal xcelsius best practices and workflows for building enterprise solut...
 
Whats New on SAP HANA SPS 11 Core Database Capabilities
Whats New on SAP HANA SPS 11 Core Database CapabilitiesWhats New on SAP HANA SPS 11 Core Database Capabilities
Whats New on SAP HANA SPS 11 Core Database Capabilities
 
Informatica PowerCenter
Informatica PowerCenterInformatica PowerCenter
Informatica PowerCenter
 
01 power center 8.6 basics
01 power center 8.6 basics01 power center 8.6 basics
01 power center 8.6 basics
 
Informatica power center 9 Online Training
Informatica power center 9 Online TrainingInformatica power center 9 Online Training
Informatica power center 9 Online Training
 
Designing And Monitoring In Informatica PowerCenter
Designing And Monitoring In Informatica PowerCenterDesigning And Monitoring In Informatica PowerCenter
Designing And Monitoring In Informatica PowerCenter
 
SAP BODS 4.2
SAP BODS 4.2 SAP BODS 4.2
SAP BODS 4.2
 
ETL tool evaluation criteria
ETL tool evaluation criteriaETL tool evaluation criteria
ETL tool evaluation criteria
 
Informatica and datawarehouse Material
Informatica and datawarehouse MaterialInformatica and datawarehouse Material
Informatica and datawarehouse Material
 
SAP BW connect db
SAP BW connect dbSAP BW connect db
SAP BW connect db
 
DB Change Manager XE6 Datasheet - The Essential Schema and Data Synchronizati...
DB Change Manager XE6 Datasheet - The Essential Schema and Data Synchronizati...DB Change Manager XE6 Datasheet - The Essential Schema and Data Synchronizati...
DB Change Manager XE6 Datasheet - The Essential Schema and Data Synchronizati...
 
ETL Using Informatica Power Center
ETL Using Informatica Power CenterETL Using Informatica Power Center
ETL Using Informatica Power Center
 
5 Reasons To Choose Informatica PowerCenter As Your ETL Tool
5 Reasons To Choose Informatica PowerCenter As Your ETL Tool5 Reasons To Choose Informatica PowerCenter As Your ETL Tool
5 Reasons To Choose Informatica PowerCenter As Your ETL Tool
 
Make Your Decisions Smarter With Msbi
Make Your Decisions Smarter With MsbiMake Your Decisions Smarter With Msbi
Make Your Decisions Smarter With Msbi
 
Obiee and database performance tuning
Obiee and database performance tuningObiee and database performance tuning
Obiee and database performance tuning
 
Aggreagate awareness
Aggreagate awarenessAggreagate awareness
Aggreagate awareness
 
informatica training
informatica traininginformatica training
informatica training
 
Ikenstudiolive
IkenstudioliveIkenstudiolive
Ikenstudiolive
 
Informatica Server Manager
Informatica Server ManagerInformatica Server Manager
Informatica Server Manager
 

Destaque (6)

Project folder-structure-
Project folder-structure-Project folder-structure-
Project folder-structure-
 
Folder Structure and Document Naming Convention Best Practices
Folder Structure and Document Naming Convention Best PracticesFolder Structure and Document Naming Convention Best Practices
Folder Structure and Document Naming Convention Best Practices
 
Software+struc+doc
Software+struc+docSoftware+struc+doc
Software+struc+doc
 
PMBOK 5th Edition Schema
PMBOK 5th Edition SchemaPMBOK 5th Edition Schema
PMBOK 5th Edition Schema
 
Pmi presentation structuring project v2
Pmi presentation   structuring project v2Pmi presentation   structuring project v2
Pmi presentation structuring project v2
 
Tcp Udp Icmp And The Transport Layer
Tcp Udp Icmp And The Transport LayerTcp Udp Icmp And The Transport Layer
Tcp Udp Icmp And The Transport Layer
 

Semelhante a Sql good practices

Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
kaashiv1
 
1 extreme performance - part i
1   extreme performance - part i1   extreme performance - part i
1 extreme performance - part i
sqlserver.co.il
 
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docxDBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
seifusisay06
 

Semelhante a Sql good practices (20)

Ebook5
Ebook5Ebook5
Ebook5
 
Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
 
Ebook11
Ebook11Ebook11
Ebook11
 
Remote DBA Experts 11g Features
Remote DBA Experts 11g FeaturesRemote DBA Experts 11g Features
Remote DBA Experts 11g Features
 
S18 das
S18 dasS18 das
S18 das
 
1 extreme performance - part i
1   extreme performance - part i1   extreme performance - part i
1 extreme performance - part i
 
Migration to Oracle 12c Made Easy Using Replication Technology
Migration to Oracle 12c Made Easy Using Replication TechnologyMigration to Oracle 12c Made Easy Using Replication Technology
Migration to Oracle 12c Made Easy Using Replication Technology
 
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docxDBA, LEVEL III TTLM Monitoring and Administering Database.docx
DBA, LEVEL III TTLM Monitoring and Administering Database.docx
 
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
DB Optimizer Datasheet - Automated SQL Profiling & Tuning for Optimized Perfo...
 
SQL Server Reporting Services (SSRS) 101
 SQL Server Reporting Services (SSRS) 101 SQL Server Reporting Services (SSRS) 101
SQL Server Reporting Services (SSRS) 101
 
Novidades do SQL Server 2016
Novidades do SQL Server 2016Novidades do SQL Server 2016
Novidades do SQL Server 2016
 
Ebook7
Ebook7Ebook7
Ebook7
 
Sql interview question part 7
Sql interview question part 7Sql interview question part 7
Sql interview question part 7
 
Azure SQL Database
Azure SQL DatabaseAzure SQL Database
Azure SQL Database
 
Air Line Management System | DBMS project
Air Line Management System | DBMS projectAir Line Management System | DBMS project
Air Line Management System | DBMS project
 
Erciyes university
Erciyes universityErciyes university
Erciyes university
 
Why Should you choose SQL Server 2019 ?
Why Should you choose SQL Server 2019 ?Why Should you choose SQL Server 2019 ?
Why Should you choose SQL Server 2019 ?
 
Building High Performance MySQL Query Systems and Analytic Applications
Building High Performance MySQL Query Systems and Analytic ApplicationsBuilding High Performance MySQL Query Systems and Analytic Applications
Building High Performance MySQL Query Systems and Analytic Applications
 
Building High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic ApplicationsBuilding High Performance MySql Query Systems And Analytic Applications
Building High Performance MySql Query Systems And Analytic Applications
 
Ebook9
Ebook9Ebook9
Ebook9
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Sql good practices

  • 1. SQL Good Practices Basic Concepts Deepak Mehtani
  • 2. Background  RDBMS is not based on regular programing paradigm  It operates on mathematical concept of sets  Sets are groups that have union or intersection or – or X operations  Most misunderstood concept – sets are ordered?  No sets do not guarntee order – very important to know in terms of RDBMS 11/7/2013 2
  • 3. Strategic Imperatives  The approach should provide a stable implementation and ease of management  Provide ability to adapt new design paradigms moving forward  Ease of maintenance for prior code by means of documentation and code clarity 11/7/2013 3
  • 4. What’s new in SQL Server 2012  Editions & licensing   Three versions – Standard, Business Intelligence and Enterprise xVelocity – is Microsoft SQL Server's family of memory-optimized and in-memory technologies. These are next-generation technologies built for extreme speed on modern hardware systems with large memories and many cores  xVelocity In-Memory Analytics Engine (used in PowerPivot and Analysis Services)  xVelocity Memory-Optimized Columnstore Index (used in the SQL Server database).  Self Bi – PowerView  Data compression – high performance  Data Quality - Maintain the quality of data and ensure that the data is suited for business usage 11/7/2013 4
  • 5. xVelocity – ColumnStore  Column store - In a column store, values from a single column (from multiple rows) are stored contiguously, potentially in a compressed form  Relational database management systems traditionally store data in row-wise fashion. The values comprising one row are stored contiguously on a page. We sometimes refer to data stored in row-wise fashion as a row store  Columnstore index – In SQL Server, a columnstore index is data stored in column-wise fashion that can be used to answer a query just like data in any other type of index  A columnstore index appears as an index on a table when examining catalog views or the Object Explorer in Management Studio  The query optimizer considers the columnstore index as a data source for accessing data just like it considers other indexes when creating a query plan More information: http://social.technet.microsoft.com/wiki/contents/articles/3540.sql-servercolumnstore-index-faq-en-us.aspx 11/7/2013 5
  • 6. Power View  Power View is a feature of SQL Server 2012 Reporting Services that is an interactive data exploration, visualization, and presentation experience  Provides intuitive ad-hoc reporting for business users such as data analysts, business decision makers, and information workers  Users can easily create and interact with views of data from data models based on PowerPivot workbooks published in a PowerPivot Gallery, or tabular models deployed to SQL Server 2012 Analysis Services (SSAS) instances  Power View is a browser-based Silverlight application launched from SharePoint Server 2010 that enables users to present and share insights with others in their organization through interactive presentations 11/7/2013 6
  • 7. Data Compression  DBA can compress tables and indexes to conserve storage space at a slight CPU cost. One of the main design goals of data compression was to shrink data warehouse fact tables  SQL Server provides two methods, Page and Row compression, to reduce data storage on disk and speed I/O performance by reducing the amount of I/O required for transaction processing. Page and row compression work in different, yet complementary  Page compression uses an algorithm called “deduplication.” When deduplicating, as the name implies, SQL Server looks for duplicate values that appear again and again within the data page  Using page compression, SQL Server can remove such duplicate values within a data page by replacing each duplicate value with a tiny pointer to a single appearance of the full value  By comparison, row compression does not actually use a compression algorithm per se. Instead, when row compression is enabled, SQL Server simply removes any extra, unused bytes in a fixed data type column, such as a CHAR(50) column  Page and row compression are not compatible, but by enabling page compression SQL Server automatically includes row compression 11/7/2013 7
  • 8. Data Quality Service  Aggregating data from different sources that use different data standards can result in inconsistent data, as can applying an arbitrary rule or overwriting historical data. Incorrect data affects the ability of a business to perform  Aggregating data from different sources that use different data standards can result in inconsistent data, as can applying an arbitrary rule or overwriting historical data. Incorrect data affects the ability of a business to perform  Features include  Data Cleansing – modification of incorrect or incomplete data  Matching – identification of semantic duplicates  Reference Data Services – verification of quality of data using reference data provider  Profiling – analysis of a data source to provide insight into the quality of the data  Monitoring – tracking and determination of the state of data quality activities  Knowledge Base – Data Quality Services is a knowledge-driven solution that analyzes data based upon knowledge that you build with DQS. 11/7/2013 8
  • 9. Layered Coding Approach  Standardized way for coding   Code level comments   Layered coding – database, business and user interface Standardized approach on both application and database level Primary focus of this presentation is at database designing and coding 11/7/2013 9
  • 10. Resource Utilization  Non-scalable code  Only so many people can access at the same time  This is caused by   Inefficient use of resources  11/7/2013 Resource locking Unnecessary use of locks or transactions that were never committed or rolled back 10
  • 11. Unstructured Data – Paradigm Shift  Non-scalable code  Not only reduces performance  Increases maintenance  Requires more space  Disk space  Disk defragmentation  Requires continuous log file management, sizing and maintenance  Increased maintenance cost for the database 11/7/2013 11
  • 12. Basic Design – that we forget  Defining a table  Key points  Identity columns  Define primary key (if not easily defined then use identity columns)  Define indices  Huge performance difference using index and primary key  Also helps in joining other tables 11/7/2013 12
  • 13. Truncate or Delete  Truncate vs. Delete    Using truncate is better than delete Why? What do you think is better for import tables? 11/7/2013 13
  • 14. Looping in SQL  Cursors  Necessary evil  Resource hog – disk, network bandwidth (for result transmission line by line)  Use read only cursor, if there is no update with fast forward option and auto fetch to get some performance gain   http://technet.microsoft.com/enus/library/aa172573(SQL.80).aspx Avoid cursors as much as possible 11/7/2013 14
  • 15. Transactions  Using Transactions  Helpful in recovering from a problem / unexpected situation  Transactions should start as late as possible in the procedure and end as early as possible to reduce the locking time  Make sure transactions are always rolled back or committed  Handle error using @@error and roll back transactions in such a case  Use “with Mark” option to add the name to the transaction log that can be used as a restore point if needed  11/7/2013 http://msdn.microsoft.com/en-us/library/ms188929.aspx 15
  • 16. Locks & Deadlocks  Deadlocks and using no locks  Deadlock occurs when two users have lock on separate objects and each user is trying to lock other user’s resource  These are automatically detected and resolved by SQL with one of the transactions rolled back with an error code of 1205  Using no locks may be helpful  No locks does not lock a record for read or write  Advantage?  Pit fall?  Read isolation transaction better than no lock 11/7/2013 16
  • 17. Temporary Tables  Temp tables  Using # tables vs. @tables  # tables – pros and cons?  @tables – pros and cons?  Which one should be used for a stored procedure that is called very frequently 11/7/2013 17
  • 18. User Defined Functions  Using User Defined Functions (UDF)  UDF are useful for calculations or results based on some input  UDF are slower than built-in functions  What will happen if we use a UDF in a  Select statement?  Join condition?  Where clause?  Any of the above in a while loop or cursor? 11/7/2013 18
  • 19. Capturing Error  Using @@error (also trapping errors at application level to close sql connection and roll back transactions)  Checking @@error variable after and insert and / or update can help us determining if we need to roll back or commit a transaction  Using Try-catch construct BEGIN TRY -- Generate divide-by-zero error. SELECT 1/0; END TRY BEGIN CATCH -- Execute error retrieval routine. EXECUTE usp_GetErrorInfo; END CATCH; 11/7/2013 19
  • 20. Joins vs. exists or in and not exists or not in  Using left joins with null conditions instead of not exist  Example below Select z.first_name+' '+z.last_name AS [name], z.[user_id] From users z inner join usergroup zug ON z.[userid] = zug.[userid] and (z.first_name +z.last_name is not null) and z.[user_id] NOT IN (SELECT [user_id] FROM Superuser) inner join groups zg ON zug.group_id = zg.group_id Where zg.[name]='ADMINISTRATOR' Order by [name]  We could probably replace the not in with a left join on a = null  Joins are faster than “in” and “not in” and use index 11/7/2013 20
  • 21. Low hanging fruits  Should Select * be used more frequently?  Scope_Identity vs. select max?  Utilize query analyzer / management studio to view query plan  Analyze for optimization and potential scalability problems  Analyze any potential bottleneck / blocking, use sql trace  Avoid dynamic queries  Use joins potentially on primary key and / or indexed columns  Do not use SP_ in the name of a stored procedure   First reference is checked in master database Using count(*) vs. Count(Primary key) column  Need to have an ID index 11/7/2013 21
  • 22. Sub-Queries  Sub-Queries  Avoid sub-queries if possible, they can be a resource hog  ClaimBatch_listDetailXml: (((@searchClaimType = 'ALL'AND @userId IN( SELECT UserId FROM NSFClaimTrack nct WHERE nct.UserId = @userId 11/7/2013 22
  • 23. Conclusion  These are guidelines that will help in  Building a robust product  Standardized way for all programmer  Ease of understanding of code  Clear logic understanding  Easy maintenance 11/7/2013 23