Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ten 10 Things DBAs Want.NET Developers to Know Kevin Kline Technical Strategy Manager, Quest

Similar presentations


Presentation on theme: "Ten 10 Things DBAs Want.NET Developers to Know Kevin Kline Technical Strategy Manager, Quest"— Presentation transcript:

1 Ten 10 Things DBAs Want.NET Developers to Know Kevin Kline Technical Strategy Manager, Quest Software @kekline, http://KevinEKline.com

2 Your Speaker: Kevin Kline My first bookFounding PASSMVP Status

3 OMG! Free Stuff! Free posters, guides, and other goodies. http://www.quest.com/landing/?id=5700http://www.quest.com/landing/?id=5700 Free DVD Training: HTTP://db-management.com/liveHTTP://db-management.com/live March 2010 July 2010

4 Agenda ToolsTechniques Process, Patterns & Practices Happy & Helpful DBA!

5 TOOLS

6 #1. Tools: Everybody’s Best Friend Microsoft SQL Server Management Studio (SSMS) –Query plans –Object scripting –Quick and dirty data browsing Profiler –Monitor the SQL activity on the server –Replay template PerfMon –Monitor resource consumption

7 #2. Execution Plans and Traces SQL Server processes a query through an execution plan aka a query plan –Learn how to compare in SSMS –Learn how to watch it happen with SQL Profiler Query performance may be misleading –Should evaluate on comparable data sizes –Different CPU, Disk, caching, etc. –Evaluate the query “Cost”

8 TECHNIQUES

9 #3. Test Harness & Testing Clear your caches Measure: –Total elapsed time –Individual statement time –IO load Facilitates automation of execution plans

10 #4a. Query Performance Tips Indexes! WHERE clauses: –Don’t put a function around a column –Can ensure or prevent index usage Use EXISTS to check for existence Be careful with NOT IN clauses

11 #4b. More Query Performance Tips Temporary tables! –“More” physically instantiated –Allows Indexes –Greater than ~100 rows –Create the tables then use them Table variables –“Less” physically instantiated –No cleanup, fewer recompiles, no statistics

12

13 #5a. Set-Based Processing Microsoft ® Visual C# ®, Visual Basic ®, Visual C++ ®, etc. are procedural languages –Tell the computer what to do SQL is a declarative language –Tell the computer what you want Learn to think in terms of sets –One pass through a table –Let the computer process joins and filters Don’t write loops or cursors on the server!

14 #5b. Loops Cursors and WHILE loops –Call a stored procedure multiple times –Typically slower than processing all the rows at once Use READ ONLY cursors Avoid WHILE loops where possible

15 #6. Connecting to SQL Server Use the SqlClient namespace in Microsoft ®.NET Use Connection Pooling –On by default in.NET Set Application Name in connection string –Helps troubleshooting and monitoring –Slightly degrades connection pooling SqlDataReaders are much faster than DataTables –Update through stored procedures or SQL statements

16 Setting the Application Name SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(); csb.DataSource = “L40\YUKON”; csb.IntegratedSecurity = true; csb.InitialCatalog = "AdventureWorks"; csb.ApplicationName = "MyDemoApp"; string connectionString = csb.ToString(); SqlConnection conn = new SqlConnection(); conn.ConnectionString = @“Data Source=L40\YUKON; Initial Catalog=AdventureWorks; Integrated Security=True; Application Name=MyDemoApp”;

17 #7. Handling Nulls NULLs are an unknown value ANSI SQL-92 requires any comparison to a NULL to fail (i.e. false) –This is SET ANSI_NULLS ON Default setting –NULL = NULL returns false –NULL <> NULL returns false ANSI_NULLS OFF –WHERE NULL = NULL returns true How do I know when I don’t know what I know, y’know?

18 Handling Nulls Use – WHERE ColumnName IS NULL Avoid – WHERE ColumnName = NULL – WHERE Col=COALESCE(@Var, Col) – WHERE (@Var IS NULL OR Column=@var)

19 Nulls on the Client Basic types ( int, string, etc.) don’t handle Null SqlDataReader – IsDBNull tests whether a column is null DataColumn – AllowDBNull property System.DBNull.Value –Populate SqlParameters Nullable types (int?, string?, etc.) do handle Null Really just Nullable –Any type can be declared nullable

20 Nullable Types Classes (types) that support Null Use IsDBNull to check database for NULL string? s; Console.WriteLine(s.HasValue.ToString()); s = “Test”; string? x = null;

21 #8. Error Handling Ability to consume T-SQL errors Can nest TRY…CATCH blocks –Used in the CATCH block Preventing Errors –Use EXISTS() to check for rows –Use a return code to signify failure

22 Client-Side Error Handling Catch a SqlException Check the Errors collection for multiple errors try { SqlDataReader r = cmd.ExecuteReader(); } catch (SqlException ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.Errors.Count.ToString()); }

23 Transactions COMMIT TRAN matches BEGIN TRAN ROLLBACK TRAN cancels ALL transactions Always BEGIN, COMMIT, and ROLLBACK transactions at a consistent level Always test @@TRANCOUNT prior to COMMIT or ROLLBACK

24 Client-Side Transactions SqlConnection conn = new SqlConnection(“…") ; conn.Open(); SqlTransaction trans = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.Transaction = trans; try { // do some stuff trans.Commit(); } catch (SqlException) { trans.Rollback(); }

25 PATTERNS & PRACTICES

26 #9. Security is NOT Afterthought SQL Injection is the #1 hack on the internet today. –Remarkably, we knew as much about preventing SQL Injection ten years ago as we do today. Plan ahead of time to minimize issues: –Ensure the least privileges principle for applications running on your servers –How much surface area do your servers expose? –Who has access to your servers? –How do you find out the who, what, and when of a breach? 26

27 #10. Change Management is RISK Management Change management is important! Without it, DBAs face: –Changes that leave things worse than they started –Piecemeal rollbacks that cripple applications –Inconsistent support across applications and servers Proper change management means: –Processed by a “change management board” composed of all key stakeholders –Performed at pre-planned times and within a defined time limit –Change is tested and verified to have no effect or positive effect on production environment –Changes are isolated, atomic, and reversible 27

28 Additional Resources Microsoft Web Sites –www.microsoft.com/sqlwww.microsoft.com/sql –MSDN SQL Server Developer Center –TechNet SQL Server TechCenter Community Sites –SQLServerPedia.com –SQLServerCentral.com –SQLBlog.com –SQLTeam.com

29 SUMMARY SSMS, Traces, and Execution Plans Test Harness & Testing Query Tuning Tips Temp tables Cursors & Loops NULL Error Handling Transactions Security isn’t an afterthought Change Management is risk management

30 Resources Visit our SQL Server technical resource blog at: www.sqlserverpedia.com - feel free to send your SQL Server related technical questions to: ask@sqlserverpedia.comwww.sqlserverpedia.comask@sqlserverpedia.com Visit our dedicated SQL Server community site at: http://sqlserver.quest.comhttp://sqlserver.quest.com Check out our “Backstage” area with all of our collateral and upcoming events including our ‘Pain of the Week’ webcasts: www.quest.com/backstage www.quest.com/backstage/pow.aspx

31 Solution AreaProductDescription Fast, flexible backup and recovery with industry-leading compression technology Discover and resolve performance issues in production before they impact end users and service levels. Gain control of disk space, growth rates and index maintenance Plan and develop applications that deliver both functionality and optimal performance Project Lucy Backup and Recovery Performance Management Capacity Management Development Comprehensive schema, object, security and change management Administration Community crowdsourcing for SQL Server tracing and performance information! Community, Knowledge, Training

32 Questions ? Send q at: kevin.kline@quest.comkevin.kline@quest.com Twitter @kekline Blogs at SQLServerPedia.com, SQLblog.com, SQLMag.com Rate Me – http://SpeakerRate.com/kekline/http://SpeakerRate.com/kekline/ Content at http://KevinEKline.com/Slides/http://KevinEKline.com/Slides/


Download ppt "Ten 10 Things DBAs Want.NET Developers to Know Kevin Kline Technical Strategy Manager, Quest"

Similar presentations


Ads by Google