Get a Lever and Pick Any Turtle<br />Lifting With Metadata<br />
Cade Roux<br />Email<br />cade@rosecrescent.com<br />Web<br />caderoux.com<br />
Give me the place to stand, and I shall move the earth.<br />
Start Simple<br />
Scalethe Technique<br />
Use<br />Basic<br />Concepts<br />
Expand to Systems<br />
Mixing Metaphors – We’re Going Down a Rabbit Hole<br />Pam: And then outta that cake pops another stripper holding a small...
Avoid Injury<br />
Metadata has always been there<br />Codd Rule #0: The RDBMS must use the relational facilities to manage the database<br />
Metadata has always been there<br />Codd Rule #4: There must be an active online catalog based on the relational model<br />
DBMeta<br />DBHealth<br />Demo<br />
Tables<br />Views<br />SPs<br />
INFORMATION_SCHEMA contains tables and views (ANSI 92, kind of generic RDBMS model, doesn’t expose a lot of implementation...
You could make tables of additional information about database objects<br />DON’T<br />Built-in extended properties work g...
Layer this view on top of sys.extended_propertiesto:<br />Make it easier to use<br />Give a standard naming convention<br ...
Easier to use than sp_addextendedproperty, sp_updateextendedproperty, sp_dropextendedproperty<br />The builtinprocs have s...
With some simple tools we now have an easier way of dealing with the metadata in the extended properties<br />This can be ...
Some questions about the health of the database system will be very standard<br />By health, I mean outside of integrity y...
Let’s create some data structures in our Demo schema – some tables and indexes<br />Demo Data<br />
Arbitrary, but useful and chosen for variety<br />Rule #1: All tables should be identified with a SUBSYSTEM (Organizationa...
We simply make a view DBHealth.MonitoredSchemas<br />We’ll need to remember to use this view<br />You could make a whole l...
Look for tables without extended property ‘SUBSYSTEM’<br />Rule #1<br />
Look for unique indexes where any of the columns in the index are NULLable.<br />Rule #2<br />
Find all varchar/nvarchar columns with length <= 2<br />Rule #3<br />
Let’s tag things for exclusion with extended properties!<br />What about exclusions?<br />
How do we organize them?<br />Could we put them in individual views or procedures to make them easy to use?<br />Then make...
All procs in DBHealth should have HEALTH_CHECK_PROC and HEALTH_CHECK_SET properties<br />Show me any unindexed tables (hea...
Health Monitoring<br />.NET<br />Code Generation<br />T-SQL<br />T4<br />Let’s use this from a client<br />
Materials<br />http://bitly.com/bundles/caderoux/3<br />4 Part article by Adam Aspin on SQL Server Central<br />Article by...
Cade Roux<br />Email<br />cade@rosecrescent.com<br />Web<br />caderoux.com<br />
Upcoming SlideShare
Loading in …5
×

Get a Lever and Pick Any Turtle: Lifting with Metadata

962 views

Published on

Using SQL Server metadata and extended properties for code generation, documentation and system maintenance.

https://bitly.com/bundles/caderoux/3

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
962
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • First of all, thank you for coming. I’m Cade Roux, I’ve been designing and building software for over 17 years and hopefully I’m going to show you some techniques which can save you time and effort and make your systems better in a lot of different ways.
  • Archimedes is often associated with work with simple machines and this quote about the lever is attributed to him. With an appropriate tool, you can magnify your strengths and free up time to concentrate on less tractable problems. The analogy with the lever does break down – with a lever, you use a smaller force through a larger distance – with the techniques in this talk, you should use less effort and less time!
  • My Goal is to demonstrate concrete metadata techniques for SQL Server, simplified from actual production code that: Make for maintainable systems, Are flexible for a variety of problems andExtend your productivity for more leverage – taking simple building blocks and concepts to build larger structures.
  • My Goal is to demonstrate concrete metadata techniques for SQL Server, simplified from actual production code that: Make for maintainable systems, Are flexible for a variety of problems andExtend your productivity for more leverage – taking simple building blocks and concepts to build larger structures.
  • My Goal is to demonstrate concrete metadata techniques for SQL Server, simplified from actual production code that: Make for maintainable systems, Are flexible for a variety of problems andExtend your productivity for more leverage – taking simple building blocks and concepts to build larger structures.
  • My Goal is to demonstrate concrete metadata techniques for SQL Server, simplified from actual production code that: Make for maintainable systems, Are flexible for a variety of problems andExtend your productivity for more leverage – taking simple building blocks and concepts to build larger structures.
  • As you can imagine, this talk is going to be a bit recursive. Metadata is data about data. In this talk I’ll be showing tactics that use metadata to manage metadata… We normally build software in layers. I’m going to be talking about using metadata from one layer to support the next layer. We’ll be using metadata to do code generation, to inspect the database, and to generally automate tasks using the built-in features of the datatabase.
  • Although using metadata and code generation can be faster and more efficient than manual coding, using the proper lifting technique can also be safer as well by eliminating defects through consistently generated and re-generated code, exception reports and self-maintaining subsystems.
  • Most what I am going to be talking about is just effective use of features which have been around a long time to solve problems which have been around for a long time in a consistent, simple and coherent fashion.
  • Most what I am going to be talking about is just effective use of features which have been around a long time to solve problems which have been around for a long time in a consistent, simple and coherent fashion.
  • I’d like to lay out some simple database ground rules for this demo. Schemas are simply a container within a database, a feature added in SQL Server 2005. I’m simply going to have one schema for “user” data – Demo. I’m going to have a schema called DBMeta for some general metadata utilities and a schema called DBHealth which will contain things which depend upon DBMeta. So if you were to do something like this in your own database, DBMeta is standalone and DBHealth would require DBMeta to exist and provide those services. You want to avoid building anything with circular dependencies – that’s certainly possible in SQL Server, so you need to keep a clear conceptual model and manage this yourself. It will only be apparent when you go to deploy and script and the object give warnings that their dependencies don’t exist. You could probably use metadata techniques to detect cycles in a directed dependency graph you could extract from the metadata.Run Script LT-01.sql
  • I’m going to assume a pretty basic SQL Server platform for this demo: tables (with indexes) in schemas, views on top of tables, and stored procedures using those tables and views. I’m not really going to address user-defined functions, but these techniques can also be used there.
  • So SQL Server provides basic metadata services required by the relational model. This is the active online catalog required by Codd. You have the INFORMATION_SCHEMA which has been standardized by ANSI, and there are also the proprietary versions which have shifted around a little over the years with the different versions.
  • When we talk about systems, the built-in metadata is kind of limited. Say you need to categorized every table into different types, or you need to make a list of stored procs or some kind of dependency tracking which isn’t really part of a traditional database schema element like a constraint or something.You could make tables and design whole systems for tracking that. One problem with that is you either have to use DDL triggers or manual maintenance to ensure it stays updated when a table is added or removed.So in addition to the built-in metadata, you’ve also got very powerful ad-hoc extended properties which can be attached to most objects in the database. I am only going to be using character strings (varchar) in my extended properties. In actual fact, you can use ANYTHING (sql_variant) in extended properties (up to 7500 bytes of data).
  • sys.extended_properties is a convenient view, but it doesn’t expose everything in really useful form. This view on top helps. It might seem to be a lot of special cases, but that’s the same way you have to use sys.extended_properties anyway.Run Script LT-02.sql
  • These SPs simplify calling the underlying SPs. I built this on top of a table-valued function to eliminate repeating some code.Run Script LT-03.sql
  • With some simple tools we now have an easier way of dealing with the metadata in the extended propertiesThis can be expanded at will to join regular base metadata information, PIVOT with the property information and combine them in interesting waysFor instance, say your question was “I want to see NULLability (base metadata) of all money (base metadata) columns in tables in the Accounts subsystem (extended property) which have not been marked as reviewed. (extended property)” You can do that.
  • So let’s move on to trying to enable some way of looking for problems in the database – mainly design and implementation problems. I’m assuming that the database is doing everything you have told it – so it’s enforcing referential integrity where you’ve specified. But we’re talking meta – so, for instance, can it tell you all the tables you’ve forgotten to enforce referential integrity even though you have a primary key?
  • Run Script LT-Demo-01.sql
  • So let’s make up some rules and try to start checking the database using the metadata. These are arbitrarily chosen as examples, but they’re real-world examples.Rule #1 is that we’re going to specify that the database objects are documented beyond just having the standard MS_Description. We want every table to be identified with a SUBSYSTEM. Say you have a largish system, and you need an HR subsystem and an ACCOUNTING subsystem within the same database. You might use schemas for this or a prefix naming system, so it’s kind of self-documenting, but let’s say you didn’t and you need to retro fit this to help organize and inventory your database.Rule #2 is that say that if you have a unique index, you want to know if any of those columns are nullable. This should be a pretty rare possibility, but SQL Server allows it.Rule #3 is that we want to look for short character columns which don’t need the overhead of varchar.Finally, for Rule #0, we want to go back and say, let’s only check these rules on schemas that we want to manage.
  • We simply make a view DBHealth.MonitoredSchemasWe’ll need to remember to use this view when ever we are looking at objects to make sure they are in a monitored schemaYou could make a whole layer of views which filter through this, of courseSo let’s look at the view.Run Script LT-04.sql
  • We’ll write the query in ad hoc form. Then productionize it into an SP.Run Script LT-05.sql
  • We’ll write the query in ad hoc form. Then productionize it into an SP.Run Script LT-06.sql
  • We’ll write the query in ad hoc form. Then productionize it into an SP.Run Script LT-07.sql
  • Now let’s change that one a little bit, since it’s finding something we don’t want to change and there’s nothing like false positives to make a warning system easily ignorable.Run Script LT-08.sql
  • So now we have a bunch of rules. Great! How do we organize them?Could we put them in individual views or procedures to make them easy to use? Then make a master procedure which runs them all.But we’d need to make a list of all the procedures. But what did we say (don’t make more tables to keep track of tables!)? Let’s use the metadata to tag the procedures! We’ll mark the procedures We’ll categorize the procedures.We’ll be able to look for all the marked procedures and run them in an automated fashion – no new tables, limited maintenance!Let’s see how to do this.Run Script LT-09.sql
  • Now you can get all kinds of ideas for rules.
  • Health Monitoring in .NET – HealthMonitorFormCode Generation in T-SQL – LT-Demo-02.sql, LT-Demo-03.sql, LT-Demo-04.sqlCode Generation in T4 – DemoApp – GenerateLookups.tt, DumpLookups.tt
  • Get a Lever and Pick Any Turtle: Lifting with Metadata

    1. 1. Get a Lever and Pick Any Turtle<br />Lifting With Metadata<br />
    2. 2. Cade Roux<br />Email<br />cade@rosecrescent.com<br />Web<br />caderoux.com<br />
    3. 3. Give me the place to stand, and I shall move the earth.<br />
    4. 4. Start Simple<br />
    5. 5. Scalethe Technique<br />
    6. 6. Use<br />Basic<br />Concepts<br />
    7. 7. Expand to Systems<br />
    8. 8. Mixing Metaphors – We’re Going Down a Rabbit Hole<br />Pam: And then outta that cake pops another stripper holding a smaller cake and then an even smaller stripper pops outta that one.<br />Michael: What is that smaller stripper holding?<br />Pam: A cupcake! It's cupcakes and strippers all the way down.<br />
    9. 9. Avoid Injury<br />
    10. 10. Metadata has always been there<br />Codd Rule #0: The RDBMS must use the relational facilities to manage the database<br />
    11. 11. Metadata has always been there<br />Codd Rule #4: There must be an active online catalog based on the relational model<br />
    12. 12. DBMeta<br />DBHealth<br />Demo<br />
    13. 13. Tables<br />Views<br />SPs<br />
    14. 14. INFORMATION_SCHEMA contains tables and views (ANSI 92, kind of generic RDBMS model, doesn’t expose a lot of implementation details)<br />sys schema contains tables and views (more proprietary, particularly non-portable things like indexing features)<br />SQL Server Basic Metadata Services<br />
    15. 15. You could make tables of additional information about database objects<br />DON’T<br />Built-in extended properties work great for this!<br />MS_Description: Microsoft’s standard “description” property<br />sp_addextendedproperty, sp_updateextendedproperty, sp_dropextendedproperty<br />fn_listextendedproperty<br />sys.extended_properties<br />I’ll improve this in the DBMeta schema<br />SQL Server Extended Properties<br />
    16. 16. Layer this view on top of sys.extended_propertiesto:<br />Make it easier to use<br />Give a standard naming convention<br />Easier than CROSS APPLY fn_listextendedproperty<br />DBMeta.Properties<br />
    17. 17. Easier to use than sp_addextendedproperty, sp_updateextendedproperty, sp_dropextendedproperty<br />The builtinprocs have some awkward hierarchy aspects: e.g. a column or trigger is defined at level 2 where the table is level 1<br />DBMeta.AddXP, UpdateXP, DropXP<br />
    18. 18. With some simple tools we now have an easier way of dealing with the metadata in the extended properties<br />This can be expanded at will to join regular base metadata information, PIVOT with the property information and combine them in interesting ways<br />For instance, say your question was “I want to see NULLability (base metadata) of all money (base metadata) columns in tables in the Accounts subsystem (extended property) which have not been marked as reviewed. (extended property)” You can do that.<br />DBMeta<br />
    19. 19. Some questions about the health of the database system will be very standard<br />By health, I mean outside of integrity you can realistically enforce with constraints and beyond what you might use DDL triggers for<br />Can we use the metadata to do something generally useful?<br />Can we organize the health system using metadata so that it is self-maintaining as much a possible?<br />DBHealth<br />
    20. 20. Let’s create some data structures in our Demo schema – some tables and indexes<br />Demo Data<br />
    21. 21. Arbitrary, but useful and chosen for variety<br />Rule #1: All tables should be identified with a SUBSYSTEM (Organizational Rule)<br />Rule #2: No unique indexes with columns which allow NULLs (Indexing Rule)<br />Rule #3: No varchar(N) columns where N <= 2 (Table Design Rule)<br />Rule #0: We only want to enforce these on schemas under management<br />Let’s Make Up Some DBA Rules<br />
    22. 22. We simply make a view DBHealth.MonitoredSchemas<br />We’ll need to remember to use this view<br />You could make a whole layer of views which filter through this, of course<br />Rule #0<br />
    23. 23. Look for tables without extended property ‘SUBSYSTEM’<br />Rule #1<br />
    24. 24. Look for unique indexes where any of the columns in the index are NULLable.<br />Rule #2<br />
    25. 25. Find all varchar/nvarchar columns with length <= 2<br />Rule #3<br />
    26. 26. Let’s tag things for exclusion with extended properties!<br />What about exclusions?<br />
    27. 27. How do we organize them?<br />Could we put them in individual views or procedures to make them easy to use?<br />Then make a master procedure which runs them all.<br />But we’d need to make a list of all the procedures<br />But what did we say?<br />Let’s use the metadata to tag the procedures!<br />We’ll mark the procedures<br />We’ll categorize the procedures<br />We’ll be able to look for all the marked procedures and run them in an automated fashion – no new tables, limited maintenance!<br />Now we have a bunch of rules<br />
    28. 28. All procs in DBHealth should have HEALTH_CHECK_PROC and HEALTH_CHECK_SET properties<br />Show me any unindexed tables (heap tables with no non-clustered indexes)<br />Show me any items tagged with a ‘TODO’ extended property<br />Show me all columns in the database which aren’t actually used (yes, you can do this!)<br />Ideas For Rules<br />
    29. 29. Health Monitoring<br />.NET<br />Code Generation<br />T-SQL<br />T4<br />Let’s use this from a client<br />
    30. 30. Materials<br />http://bitly.com/bundles/caderoux/3<br />4 Part article by Adam Aspin on SQL Server Central<br />Article by Brent Shaub on MSSQLTips.com<br />Google Code Repository of presentation and code/scripts<br />
    31. 31. Cade Roux<br />Email<br />cade@rosecrescent.com<br />Web<br />caderoux.com<br />

    ×