Presentation is loading. Please wait.

Presentation is loading. Please wait.

Efficient and Effective coding of stored procedures

Similar presentations


Presentation on theme: "Efficient and Effective coding of stored procedures"— Presentation transcript:

1 Efficient and Effective coding of stored procedures
Steve Simon

2 SQL Server MVP Consultant at DELL – EMC Ma Involved with database design for 34 years + Presenter at numerous PASS summits. Presenter at numerous SQL Saturday events. A contributor on SQLShack.com

3 Reasons for using stored procedures

4 Semi compiled. Easier to control SQL injection. Avoid runway SQL queries and SQL injection. Easy to check performance within the profiler.

5 Before we look at the benefits of utilizing SP’s, let’s understand what is driving the need.

6 Over the past few years Organizational reporting needs have changed.
More is required than a simple spreadsheet. KPI’s become critical to the decision maker. Why? Due to time demands on the decision maker.

7 Reporting Changes More graphs and charts to tell the story.
Data must be in the correct format for the report requirements. Format: Matrix vs. chart. New coding techniques required to ensure that the format is correct.

8 Efficient and effective code produces quality stored procedures
In short Efficient and effective code produces quality stored procedures

9 During this hour..we shall discuss
How to achieve what we have just mentioned. How to achieve this with efficient and effective stored procedures. How to replace queries with Store Procedures. How to effect THESE changes within SSRS.

10 Getting down to the ‘nuts and bolts and parceling our code’

11 We wish to avoid this type of scenario at ALL COSTS

12 In the following examples we shall be looking at code that may be converted to SP’s

13 The code must be efficient and effective BEFORE the stored procedures can be.

14 While Loop or Cursor ?

15 Are you sure ?

16 Are you using a Key field ?

17 While Loop

18 Cursor

19 Are you using a Non key field ?

20 While Loop

21 Cursor

22 Summary While loop if field is indexed (Seek).
Cursor when no index on field. Used standardly accepted SQL Server standards.

23 Bottom line Do not go by what the book tells you.
Experiment with all mechanisms prior to making a final decision.

24 Our friend! The Profiler

25 While Loop Non Key Run terminated

26 While Loop

27 Cursor Non Key

28 Cursor

29 Demo 1

30 Pivoting Data

31 Data format issue Query structure depends upon end report format.
Are we using a chart or a matrix to show the results? We may in fact want both!! Extracts for charts may be pivoted for matrix.

32 This format is great for a chart

33 This format is great for a Matrix

34 Create the PIVOT code string
= ' select City, [' + @month01+ '],[' '],[' '],[' '],[' +'],[' '],[' '],[' '],[' '],[' '],[' + @month11+ '],[' + '] from ' + ' ( ' + ' select Yearmth, City, name, value ' + ' from #rawdata9 ' + ' unpivot ' + ' ( ' + ' value for name in (Revenue) ' + ' ) unpiv '+ ' ) src ' + ' pivot ' + ' sum(value) ' + ' for YearMth in ([' '],[' '],[' '],[' '],[' +'],[' '],[' '],[' '],[' '],[' '],[' '],[' +'] )) piv ' + ' order by name asc '

35 The results of the two stored procedures
Chart form SP Matrix form SP

36 Demo 2

37 Take-Away Use of T-SQL queries prone to SQL injection.
True when parameters must be passed to a QUERY. Efficiency and effectiveness T-SQL (profiler). Calling stored procedures is better as we can trap for injection etc. within the ‘compiled’ code.

38 Take-Away By testing T-SQL using execution plans we succeed. Index Seeks vs Table scans Stored Procedure ensure that the power users do not run wild. SP’s are better from an audit point of view Lock down the environment.

39 So!! At the end of the day, how may we achieve all of this?
Through the..

40 SQL Server from Soup to Nuts
Efficient and Effective coding of stored procedures Questions Steve Simon


Download ppt "Efficient and Effective coding of stored procedures"

Similar presentations


Ads by Google