Quick & Easy SQL Tips

Download Quick & Easy SQL Tips

Post on 24-Jun-2015

915 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

SQL Tips for the YouTube generation. 20 unrelated and independant tips, one right after another to speed up your database application without a ton of work.

TRANSCRIPT

<ul><li> 1. Quick &amp; Easy SQL Tips<br />Ike Ellis<br />http://www.ellisteam.net<br />ike@ellisteam.net<br />Twitter: @ike_ellis<br />1<br /></li></ul> <p> 2. Assumptions @ You<br />You are a new DBA<br />You dont want to rewrite your entire application with a new schema, new DAL, or new queries<br />You want to learn just enough so that your SQL apps are fast and maintainable<br />2<br /> 3. Tip #1 Performance Problem: Check the low-hanging fruit <br />Long-running jobs<br />Long-running transactions<br />DBCC OPENTRAN<br />Check for long-running queries/both in amount and in duration<br />3<br /> 4. Tip #2:Prettify!<br />4<br />http://extras.sqlservercentral.com/prettifier/prettifier.aspx<br /> 5. Tip #3 Performance Problem : Identify hardware performance bottlenecks.<br />Memory<br />Disk*<br />Processor<br />Network I/O<br />*Most common bottleneck.Its the Disk I/O, Stupid.(But it could be memory thats causing it.)<br />5<br /> 6. Tip #4: The right way to find hardware problems<br />Merging PerfMon and Tracing<br />Get the Batch and Completed Events Only<br />Never trace from the computer you are monitoring<br />Always trace to a file and then load in a table after.<br />6<br /> 7. Tip #5: Files, Files Everywhere<br />All need their own physical drive for space management and performance<br />Master Data File (MDF)<br />Log Files (LDF)<br />TempDB Files<br />O/S/SQL Files<br />BAK Files<br />7<br /> 8. Tip #6: The Log File<br />Fills sequentially, so no need for striping, mirror is fine.<br />Dont let it get filled up: Simple Mode or Backup.<br />8<br /> 9. Tip #7 - Good memory management<br />Check for other applications running on the SQL Server<br />Move anti-virus (or at least make sure it wasn't scanning the SQL ports or the SQL files)<br />Move Exchange and F&amp;P services (cluster)<br />Turn off unneeded services<br />SQL is I/O bound, so I would turn off any network/disk intensive services (DHCP, DNS, etc)<br />9<br /> 10. Tip #8 - Quick Indexing Tricks.<br />check for clustered indexes<br />SELECT t.[Name] FROM sys.Indexes i <br />JOIN sys.Tables t <br />ON t.Object_ID = i.Object_id<br />WHERE i.type_desc = 'HEAP'<br />ORDER BY t.[Name]<br />check for nonclustered indexes on foreign key columns (ID Columns)<br />select * from sys.columns c <br />where c.name like '%id%'<br />and c.object_id not in <br />(<br />select object_id from sys.index_columns<br />)<br />check for non-clustered covering indexes<br />reads outnumber inserts/updates 5 to 10 to 1<br />10<br /> 11. Tip #9 - Run the Index Tuning Wizard (DB Tuning Advisor)<br />Run it a really long time, it is more accurate the longer it runs<br />Dont drop existing objects<br />Its OK to over-index<br />11<br /> 12. Tip #10 I dont really know the symptoms, but SQL Doctor will find the cure.<br />Idera<br />Red Gate<br />DB Artison<br />Quest<br />12<br /> 13. Tip #11 Baseline the right way<br />Idera Diagnostics Manager &amp; RedGate<br />13<br /> 14. Tip #12 Enforce Business Rules in the DB<br />Foreign Keys<br />Unique Constraints<br />Check Constraints<br />14<br /> 15. Tip #13 - Eliminate Cursors<br />Cursors focus on how, not why or what<br />Cursors are expensive<br />Cursors take up memory, which is usually a problem already<br />Cursors can often be written using a set-based method<br />15<br /> 16. Easy Tip #14 - Avoid Deadlocking, Blocking<br />Index Tune<br />Keep transactions short<br />Dont lock when you dont have to<br />Hit the tables in the same order (create a table order document)<br />Minimize the use of triggers<br />16<br /> 17. Tip #15: CTEs<br />A named temporary result set based on a SELECT query<br />Common Table Expression<br /></p> <ul><li>Result set can be used in SELECT, INSERT, UPDATE, or DELETE </li></ul> <p> 18. Advantages of common table expressions: 19. Queries with derived tables become more readable 20. Provide traversal of recursive hierarchiesWITH TopSales (SalesPersonID, NumSales) AS<br />( SELECT SalesPersonID, Count(*) <br />FROM Sales.SalesOrderHeader GROUP BY SalesPersonId )<br />SELECT * FROM TopSales <br />WHERE SalesPersonID IS NOT NULL<br />ORDER BY NumSales DESC<br /> 21. Tip #16: apply operator<br />right parameter can be a table, but meant for tvf<br />cross apply does inner join<br />no output for row when udf produces no output<br />udf can get its parameters from left input<br />outer apply does left outer join<br />all rows from left input returned<br />may have nulls for columns returned by udf<br /> 22. Tip #17: temptables vs. table variables<br /></p> <ul><li>table variables </li></ul> <p> 23. private to batch 24. avoids transaction affects 25. designed for smaller number of rows where scans are cheaper than seeks 26. limited indexing 27. static nature reduces recompiles 28. prefer to use with small number of rows 29. temporary tables 30. persists for session 31. can be shared over sessions and scopes 32. can participate in transactions 33. can be indexed 34. can trigger frequent recompiles 35. get statistics 36. prefer to use when you have more rows 37. buffering data locally</p>