Necessary Evils, Building Optimized
CRUD Procedures
Introduction
Jason
Strate

e: jstrate@pragmaticworks.com
e: jasonstrate@gmail.com
b: www.jasonstrate.com
t: StrateSQL

Resources

jasonstrate.com

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
About Pragmatic Works

•

•
•
•
•

•
•

Industry leaders in Microsoft BI and SQL Server
Platform
SQL Server Professionals - PASS Board of Directors,
Speakers, Authors and MVP’s
National Sales Team Divided by Microsoft
Territories
National System Integrator (NSI)
Gold Certified in Business Intelligence and Data
Platform
Platform Modernization/Safe Passage
Premier Partner for PDW SI Partner Program
MS PDW Partner of Year FY13
Frontline Partnership Partner of the Year for Big Data
Executive sponsor - Andy Mouacdie, WW sales director
PDW

•
•

Over 7,200 customers worldwide
Over 186,000 people in PW database for demand
generation
MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Session Goals
1. Describe the uses for CRUD procedures in
databases
2. Identify the common problems associated with
CRUD procedures
3. Demonstrate methods for writing performant
CRUD procedures

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
What is CRUD?
CREATE, READ, UPDATE, DELETE
•
•
•
•
•
•

ABCD: add, browse, change, delete
BREAD: browse, read, edit, add, delete
QDIM: query, delete, insert, modify
SAID: show, alter, insert, delete
VEDAS: view, edit, delete, add, search
VADE(R): view, add, delete, edit

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Code Generation
• Many tools available:
– T-SQL Script
– Code Smith
– Entity Framework
– LINQ to SQL
– NHibernate

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
CRUD Issues?
• Treat tables as objects
–
–
–

People?
Relationships?
Relationship Type?

• Overly generic
– All columns touched
– One procedure to rule them all

• Performance
– Set versus Singleton
– Index selection
MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Perspectives

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Overview

CREATE, UPDATE, AND DELETE

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
CREATE
CREATE = INSERT
EXEC CRUD.FooInsert
@Column1 = 'Value‘,
@Column2 = 2

INSERT INTO dbo.Foo
(Column1, Column2)
VALUES(@Column1,
@Column2)

Issues:
• Singleton insert
• All columns affected
MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
UPDATE
UPDATE = UPDATE
EXEC CRUD.FooUpdate
@Column1 = 'Value‘,
@Column2 = 2

UPDATE dbo.Foo
SET Column1 = @Column1
WHERE Column2 =
@Column2

Issues:
Singleton update
All columns affected
MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
DELETE
DELETE = DELETE
EXEC CRUD.FooDelete
@Column2 = 2

DELETE FROM dbo.Foo
WHERE Column2 = Column2

Issues:
• Singleton delete

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
T-SQL Features
• Performance
– Table Valued Functions (TVF)
– Table Valued Parameters (TVP)
– XML document
– Merge statement

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Demos

CREATE, UPDATE, AND DELETE

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Overview

READ

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
READ
• READ = SELECT
EXEC CRUD.FooSelect
@Column1 = NULL
,@Column2 = 2

SELECT Column1
,Column2
FROM dbo.Foo
WHERE (@Column1 IS NULL
OR Column1 = @Column1)
AND (@Column2 IS NULL
OR Column2 = @Column2)

Issues
• Index selectivity
MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Demo

READ

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Summary
1. CRUD isn’t necessarily bad
2. Generic templates offer ability to tune and
extend data model behind the scenes.
3. Be cautious in choosing a template for CRUD
4. Know what SQL Server is doing
5. Leverage RECOMPILE as needed

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
Learn More About Performance

MAKING BUSINESS INTELLIGENT

www.pragmaticworks.com
For more information…
Name: Jason Strate
Email: jstrate@pragmaticworks.com
Blog: www.jasonstrate.com

Products
BI products to covert to a Microsoft BI
platform and simplify development on
the platform.

Services
Speed development through training, and
rapid development services from
Pragmatic Works.

Foundation
Helping those who do not have the
means to get into information technology
achieve their dreams.

Necessary Evils, Building Optimized CRUD Procedures

  • 1.
    Necessary Evils, BuildingOptimized CRUD Procedures
  • 2.
    Introduction Jason Strate e: jstrate@pragmaticworks.com e: jasonstrate@gmail.com b:www.jasonstrate.com t: StrateSQL Resources jasonstrate.com MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 3.
    About Pragmatic Works • • • • • • • Industryleaders in Microsoft BI and SQL Server Platform SQL Server Professionals - PASS Board of Directors, Speakers, Authors and MVP’s National Sales Team Divided by Microsoft Territories National System Integrator (NSI) Gold Certified in Business Intelligence and Data Platform Platform Modernization/Safe Passage Premier Partner for PDW SI Partner Program MS PDW Partner of Year FY13 Frontline Partnership Partner of the Year for Big Data Executive sponsor - Andy Mouacdie, WW sales director PDW • • Over 7,200 customers worldwide Over 186,000 people in PW database for demand generation MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 4.
    Session Goals 1. Describethe uses for CRUD procedures in databases 2. Identify the common problems associated with CRUD procedures 3. Demonstrate methods for writing performant CRUD procedures MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 5.
    What is CRUD? CREATE,READ, UPDATE, DELETE • • • • • • ABCD: add, browse, change, delete BREAD: browse, read, edit, add, delete QDIM: query, delete, insert, modify SAID: show, alter, insert, delete VEDAS: view, edit, delete, add, search VADE(R): view, add, delete, edit MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 6.
    Code Generation • Manytools available: – T-SQL Script – Code Smith – Entity Framework – LINQ to SQL – NHibernate MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 7.
    CRUD Issues? • Treattables as objects – – – People? Relationships? Relationship Type? • Overly generic – All columns touched – One procedure to rule them all • Performance – Set versus Singleton – Index selection MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 8.
  • 9.
    Overview CREATE, UPDATE, ANDDELETE MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 10.
    CREATE CREATE = INSERT EXECCRUD.FooInsert @Column1 = 'Value‘, @Column2 = 2 INSERT INTO dbo.Foo (Column1, Column2) VALUES(@Column1, @Column2) Issues: • Singleton insert • All columns affected MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 11.
    UPDATE UPDATE = UPDATE EXECCRUD.FooUpdate @Column1 = 'Value‘, @Column2 = 2 UPDATE dbo.Foo SET Column1 = @Column1 WHERE Column2 = @Column2 Issues: Singleton update All columns affected MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 12.
    DELETE DELETE = DELETE EXECCRUD.FooDelete @Column2 = 2 DELETE FROM dbo.Foo WHERE Column2 = Column2 Issues: • Singleton delete MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 13.
    T-SQL Features • Performance –Table Valued Functions (TVF) – Table Valued Parameters (TVP) – XML document – Merge statement MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 14.
    Demos CREATE, UPDATE, ANDDELETE MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 15.
  • 16.
    READ • READ =SELECT EXEC CRUD.FooSelect @Column1 = NULL ,@Column2 = 2 SELECT Column1 ,Column2 FROM dbo.Foo WHERE (@Column1 IS NULL OR Column1 = @Column1) AND (@Column2 IS NULL OR Column2 = @Column2) Issues • Index selectivity MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 17.
  • 18.
    Summary 1. CRUD isn’tnecessarily bad 2. Generic templates offer ability to tune and extend data model behind the scenes. 3. Be cautious in choosing a template for CRUD 4. Know what SQL Server is doing 5. Leverage RECOMPILE as needed MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 19.
    Learn More AboutPerformance MAKING BUSINESS INTELLIGENT www.pragmaticworks.com
  • 20.
    For more information… Name:Jason Strate Email: jstrate@pragmaticworks.com Blog: www.jasonstrate.com Products BI products to covert to a Microsoft BI platform and simplify development on the platform. Services Speed development through training, and rapid development services from Pragmatic Works. Foundation Helping those who do not have the means to get into information technology achieve their dreams.