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

Contenu connexe

Similaire à Get a Lever and Pick Any Turtle: Lifting with Metadata

Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
kaashiv1
 
Introduction to SQL.pptx
Introduction to SQL.pptxIntroduction to SQL.pptx
Introduction to SQL.pptx
InduVerma40
 
Coddrules 120309094807-phpapp01
Coddrules 120309094807-phpapp01Coddrules 120309094807-phpapp01
Coddrules 120309094807-phpapp01
dhruv patel
 
Coddrules 120309094848-phpapp02
Coddrules 120309094848-phpapp02Coddrules 120309094848-phpapp02
Coddrules 120309094848-phpapp02
dhruv patel
 

Similaire à Get a Lever and Pick Any Turtle: Lifting with Metadata (20)

Oracle dba interview question
Oracle dba interview questionOracle dba interview question
Oracle dba interview question
 
Ebook5
Ebook5Ebook5
Ebook5
 
Sql interview question part 5
Sql interview question part 5Sql interview question part 5
Sql interview question part 5
 
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data TablesPostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
PostgreSQL Performance Tables Partitioning vs. Aggregated Data Tables
 
Data massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodesData massage: How databases have been scaled from one to one million nodes
Data massage: How databases have been scaled from one to one million nodes
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
DATABASE ADMINSTRATION
DATABASE ADMINSTRATION DATABASE ADMINSTRATION
DATABASE ADMINSTRATION
 
Geek Sync | Why Did My Clever Index Change Backfire?
Geek Sync | Why Did My Clever Index Change Backfire?Geek Sync | Why Did My Clever Index Change Backfire?
Geek Sync | Why Did My Clever Index Change Backfire?
 
7. SQL.pptx
7. SQL.pptx7. SQL.pptx
7. SQL.pptx
 
Introduction to SQL.pptx
Introduction to SQL.pptxIntroduction to SQL.pptx
Introduction to SQL.pptx
 
Coddrules 120309094807-phpapp01
Coddrules 120309094807-phpapp01Coddrules 120309094807-phpapp01
Coddrules 120309094807-phpapp01
 
Codd rules
Codd rulesCodd rules
Codd rules
 
Coddrules 120309094848-phpapp02
Coddrules 120309094848-phpapp02Coddrules 120309094848-phpapp02
Coddrules 120309094848-phpapp02
 
Codd rules
Codd rulesCodd rules
Codd rules
 
ETL Assistant Screenshots
ETL Assistant ScreenshotsETL Assistant Screenshots
ETL Assistant Screenshots
 
Database testing
Database testingDatabase testing
Database testing
 
Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...
Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...
Recipes 8 of Data Warehouse and Business Intelligence - Naming convention tec...
 
21
2121
21
 
Ebook6
Ebook6Ebook6
Ebook6
 
Sql interview question part 6
Sql interview question part 6Sql interview question part 6
Sql interview question part 6
 

Dernier

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Get a Lever and Pick Any Turtle: Lifting with Metadata

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

Notes de l'éditeur

  1. 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.
  2. 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!
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. 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
  12. 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.
  13. 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.
  14. 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).
  15. 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
  16. 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
  17. 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.
  18. 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?
  19. Run Script LT-Demo-01.sql
  20. 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.
  21. 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
  22. We’ll write the query in ad hoc form. Then productionize it into an SP.Run Script LT-05.sql
  23. We’ll write the query in ad hoc form. Then productionize it into an SP.Run Script LT-06.sql
  24. We’ll write the query in ad hoc form. Then productionize it into an SP.Run Script LT-07.sql
  25. 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
  26. 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
  27. Now you can get all kinds of ideas for rules.
  28. 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