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.
7. 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
9. 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)
22. 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
23. 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
24. 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
35. 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
36. EF Reverse POCO Generator Tips
• ConnectionStringName in app.config
• GenerateSeparateFiles = true;
• ForeignKeyFilter = (ForeignKey fk) => null;
38. Visual Studio Team Services
• Build your SQL Server Database
Project with Visual Studio Build
• DACPAC file in drop folder
• Deploy to Azure SQL Database
40. 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