Presentation is loading. Please wait.

Presentation is loading. Please wait.

Accidental DBA Developer Edition

Similar presentations


Presentation on theme: "Accidental DBA Developer Edition"— Presentation transcript:

1 Accidental DBA Developer Edition
What are you people talking about? Kenneth Fisher @sqlstudent144

2 What is an accidental DBA?
Most DBAs don’t start out as DBAs. They tend to fall into databases.

3 What are we doing? The anatomy of a table What went wrong?
The center of the universe? What went wrong? My query worked great in development?!? Tuning Queries Making it go faster.

4 The anatomy of a table Columns Internals Indexes Constraints
What data are we storing? Internals Data on the disk. Indexes Where is the data? Constraints How do we restrict the data?

5 Columns The lord of the data Data Employees Name Address
Its own table? Birthdate, Hiredate Pay (hourly, by paycheck, yearly) Title Mrs. Devy McDevFace

6 Columns DataTypes A wealth of choices
Strings (char, varchar, nchar etc) Exact Numerics (bit, int, decimal, money) Approximate Numerics (float, real) You should be listening to me (instead of reading this) Date and Time (datetime2, datetimeoffset, date, time) Binary (binary, varbinary) And even more (timestamp, spatial, xml)

7 Columns DataTypes Common mistakes Too big Too small
Possible values 1-10 stored in an INT instead of TinyINT The time isn’t important but the column is a DateTime. nChar or nVarChar when unicode isn’t needed. Too small Zipcode only a char(5). Name only 10 characters. Char or VarChar when unicode is needed.

8 Columns DataTypes Common mistakes Dates and numbers stored as strings

9 Columns Computed Columns There is no need to store everything. Store
Calculate Birth date Age Hire date Length of employment Salary (by hour, month, year) Salary and tax rates Amount of taxes paid Sales amount and commission rate Commission paid

10 Row Table Internals Header Header Pages
Tables are stored to disk in 8k pages. There are different types of pages (Index, Data, LOB and Row Overflow for example). Header Header Row Row Row

11 Table Internals Tables are index structures.
Every table can be found in sys.indexes and will have exactly one of these Heap : Type = 0 Clustered index : Type = 1 Columnstore Clustered index : Type = 5

12 Indexes Heap Unstructured Useful for fast loads

13 Indexes Clustered Narrow Unique Ever increasing (or decreasing) Static

14 Indexes Non-Clustered
These are the indexes we create beyond the clustered index for performance. Leaf pages point to the table. Clustered key, uniquifier and any included columns Pointer to a heap row (RID)

15 Indexes Covering How many indexes should I create? Unique
A covering index is an index that has all of the information required for a query. How many indexes should I create? Too many indexes can be as bad as too few. Indexes take up space. Indexes do increase the insert time. However in most systems the benefit far outweighs the cost. To a point! Unique Unique indexes enforce unique constraints but can stand alone. These are used to enforce business rules beyond the primary key.

16 Constraints Nullability Default Check Unique Primary Key Foreign Key

17 Constraints Nullability: Does the column allow Nulls?
Nulls are not a value, they are the absence of a value. FirstName LastName BirthDate Bob Boberson 04/29/1948 Devy McDevFace Null

18 ? = Constraints Nullability: What is a Null?
A null could be any allowed value. = Schrodinger's 8th birthday present

19 True False NULL Constraints Nullability: Comparisons
All comparisons (equal, not equal, in, exists etc.) can return one of the following values True False NULL

20 Null Constraints Nullability: Comparisons Equal Not Equal WHERE WHERE
Col1 = Null @Var = Null Null = Null WHERE Null <> 4 != Null Null != ‘A’ Col1 != Null ‘4/30/2010’ <> Null Null <> Null Null

21 Constraints Nullability: Comparisons IN
WHERE Col1 IN (42, 13, 7, Null) WHERE Col2 NOT IN (42, 13, 7, Null) The only possible values are FALSE and Null so no rows can be returned.

22 =? Constraints Nullability: Null vs Null
Nulls are not a value, they are the absence of a value. Null compared to Null also returns Null. =?

23 Constraints Nullability: Comparisons
Because Null is a complex concept you can’t use a simple comparison. WHERE Column Instead you have to use something like this OR (Column IS NULL IS NULL) WHERE (Column IS NULL) is null Return Nulls Return all rows

24 Constraints Default Check Unique
If no other value is specified use this one. Check Single or multi column rules. Unique Restrict duplicates in one or more columns. Columns can have values but are treated like a value for uniqueness. Enforced with a unique index. Otherwise known as Candidate Keys

25 Constraints Primary Key A form of unique constraint.
No Nulls are allowed. Only one allowed.

26 Constraints Artificial or Surrogate Key 15 47 65 Pros Cons 72 82 98
Smaller Static Easy Cons Meaningless Additional column More likely to need to join to other tables. 72 82 98

27 Constraints Natural Key Bob Devy Ken Pros Cons Amy Neil Tiffany
Data means something Possible space savings Cons Harder to select Larger Probable space cost Amy Neil Tiffany

28 Constraints Foreign Key The Lord of the Data Worker Dev Titles
CEO Dev Worker The Lord of the Data Foreign Key The values in one or more columns must exist in another table. Cascading updates and deletes Worker Dev CEO

29 Clustered Indexes vs Primary Keys
The Primary Key has nothing to do with a Clustered Index. They are two completely distinct concepts.

30 But it was working fine? Query plans The Data! RBAR vs Batch
Inline Functions

31 It worked yesterday? Query Plans

32 It worked yesterday? The Data

33 It worked yesterday? RBAR vs Batch

34 It worked yesterday? Inline Functions In the field list
SELECT FirstName, LastName, MiddleInitial, dbo.CalculateYearlyPay(EmployeeId) FROM Employees Runs once per row.

35 It worked yesterday? Functions in the WHERE clause
Sargability – SARG = Search ARGument SELECT FirstName, LastName, MiddleInitial FROM Employees WHERE DateDiff(year,BirthDate,getdate()) > 30 WHERE BirthDate> DateAdd(year,-30,getdate()) Not sargable. Can’t use an index. Sargable. Can use an index.

36 Tuning Queries Statistics IO & Time Query Plans

37 STATISTICS IO & TIME StatisticsParser.com

38 SQL Sentry Plan Explorer SSMS Query Plan Compare
Query Plans SQL Sentry Plan Explorer SSMS Query Plan Compare

39 Homework Select one of the following books that sounds interesting and buy/download it (some are free). Read at least one chapter. SQL Server Execution Plans by Grant Fritchey Inside the SQL Server Query Optimizer by Benjamin Nevarez Defensive Database Programming by Alex Kuznetsov Performance Tuning with SQL Server Dynamic Management Views by Tim Ford and Louis Davidson Microsoft SQL Server 2012 Internals by Kalen Delaney and Craig Freeman Expert Performance Indexing in SQL Server: Edition 2 by Jason Strate and Grant Fritchey Pro SQL Server 2012 Relational Database Design and Implementation by Louis Davidson and Jessica Moss

40 Thank You

41 The Quiz! Clues SARGABLE INDEX CONSTRAINTS FIELDS DEFAULT NULL
ACCIDENTALDBA WAITSTATISTICS UNICODE QUERYPLAN EXTENT PAGE CLUSTERED HEAP PRIMARY FOREIGN RBAR FUNCTION Clues The index will be used. Makes queries go faster. Enforces business rules. Where the data goes. Use this if you don't put anything else. I don't know? Has been assimilated. What's taking so long? Can hold any type of character. Steps taken to run the query. Multiple pages. 8k The table. The data is just thrown in a pile. References another table. Wow that's taking a long time. Stored code but not a stored procedure.


Download ppt "Accidental DBA Developer Edition"

Similar presentations


Ads by Google