Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Professional SQL for Developers

To most developers, the database is an afterthought. You're using an ORM to write your queries for you, migrations to build your schema for you, and the cloud to run your infrastructure for you. Once you get beyond the prototype stage, however, you need to either befriend your DBA or become one yourself by using SQL Server (and Azure SQL Database) like a professional. This session is targeted to developers, and is a broad-yet-shallow look at tools, techniques, and tips to up your SQL game. Be sure to take notes, as this is a launching point for you to learn more rather than a deep dive into one topic. Primarily the Microsoft stack will be used, although users of other RDBMSes and languages may be interested in attending. Topics covered include intermediate-to-advanced T-SQL language features, query tuning, numerous tool recommendations, advanced Entity Framework use, and Azure SQL Database features. I will also be around the conference after the session to discuss topics in more depth.

  • Be the first to comment

  • Be the first to like this

Professional SQL for Developers

  1. 1. Professional SQL for Developers Paul Irwin CTO, Docio Chief Software Engineer, feature[23]
  2. 2. Why? • ORMs + migrations + cloud = lazy developers • Lazy developers + high traffic = slow performance • Lazy developers + bad actors = security breaches • Slow performance + security breaches = unemployed developers
  3. 3. Overview T-SQL You Need To Know Query Tuning Basics 3 Entity Framework Tips Azure SQL Database Features Tool Recommendations
  4. 4. T-SQL You Need To Know
  5. 5. T-SQL: String Data Types (N)VARCHAR(X) (N)TEXT (N)VARCHAR(MAX) Max Length/Size 8000/4000 2GB 2GB Ease of Use Good Poor Good Performance Good Poor Good or Poor Indexable Yes No No Recommended Yes No Yes
  6. 6. T-SQL: OFFSET/FETCH
  7. 7. T-SQL: Window Functions • ROW_NUMBER() OVER (ORDER BY OrderDate DESC) • ROW_NUMBER() OVER (PARTITION BY TerritoryID ORDER BY OrderDate DESC) • RANK() OVER (ORDER BY Score DESC) • DENSE_RANK() OVER (ORDER BY Score DESC) • SUM(SubTotal) OVER (PARTITION BY TerritoryID ORDER BY SalesOrderID ROWS UNBOUNDED PRECEDING)
  8. 8. T-SQL: Table Valued Parameters
  9. 9. T-SQL: MERGE
  10. 10. T-SQL: Common Table Expressions
  11. 11. T-SQL: XML
  12. 12. T-SQL: JSON
  13. 13. Query Tuning Basics
  14. 14. Query Tuning: Finding Slow Queries Extended Events (XEvents) Query Store (2016+)
  15. 15. Query Tuning: Find Missing Indexes
  16. 16. Query Tuning: Missing Index DMVs http://bit.ly/2rudhX9
  17. 17. Query Tuning: Analyzing Query Plans
  18. 18. Query Tuning: Statistics SQL Server Statistics Basics by Robert Sheldon http://bit.ly/2apoFJO
  19. 19. 3 Entity Framework Tips
  20. 20. EF Tip #1: Limit LINQ • Complex LINQ queries: • Can be hard to tune • Can be hard to maintain • Require a code push to change • Heuristics: • Limit LINQ to 3-4 simple clauses (from, join, where, select, etc.) • Anything more complex goes into a sproc
  21. 21. EF Tip #2: No Nav Props • Navigation properties: • Can cause unexpected lazy loading • Can cause unexpected NREs • Can cause more data to be returned than needed • See previous points about LINQ • Recommendations: • Do not use nav props at all (pure POCO, no virtuals) • OR at least disable lazy-loading by default
  22. 22. EF Tip #3: No Code-First Migrations • Code-first migrations: • Can’t be verified at compile time • Can cause git merge issues due to .resx binary data • Require use of fragile __MigrationHistory table • Are difficult to use with multiple team members’ local DBs • Can’t do schema comparison • Can’t be deployed without running code • Recommendations: • Don’t use them at all (use DACPAC instead) • OR institute very deliberate team process around them
  23. 23. Azure SQL Database Features
  24. 24. Azure SQL Database: Security • TLS • Encrypt=True;TrustServerCertificate=False • Transparent Data Encryption • Always Encrypted • Row-level Security • Dynamic Data Masking • Auditing • Threat Detection • Security Center
  25. 25. Azure SQL Database: Scalability
  26. 26. Azure SQL Database: Scalability
  27. 27. Azure SQL Database: Geo-Replication
  28. 28. Tools
  29. 29. SQL Server Data Tools
  30. 30. SQL Server Data Tools
  31. 31. SQL Server Data Tools
  32. 32. EF Reverse POCO Generator http://bit.ly/2fX7Pql
  33. 33. EF SSDT Workflow Without Reverse POCO Generator 1. Create table/sproc in DB project 2. Publish to local DB 3. Create POCO class, ensure it matches exactly 4. Add data annotations attributes as needed 5. Add DbSet to EF context With Reverse POCO Generator 1. Create table/sproc in DB project 2. Publish to local DB 3. “Run Custom Tool” on .tt file
  34. 34. EF Reverse POCO Generator Tips • ConnectionStringName in app.config • GenerateSeparateFiles = true; • ForeignKeyFilter = (ForeignKey fk) => null;
  35. 35. SentryOne Plan Explorer https://www.sentryone.com/plan-explorer/
  36. 36. Visual Studio Team Services • Build your SQL Server Database Project with Visual Studio Build • DACPAC file in drop folder • Deploy to Azure SQL Database
  37. 37. Summary
  38. 38. Take-Aways • Learn some T-SQL for fun and profit • Tune queries early and often • With Entity Framework, less is more • Take advantage of cloud security and scalability • Use tools to help you code, version, tune, and deploy
  39. 39. Q&A

×