Skip to content

Repository files navigation

CatalogStore

An experimental .NET Core library to more efficiently reverse engineer a SQL Server database.

Overview

All database tooling needs to reverse engineer a database. Tooling does this by querying the system catalog about the objects in the database (e.g. tables, views, triggers, store procedures, etc). Here we compare DacFx and a new strategy for reverse engineering a database: CatalogStore.

DacFx Reverse Engineering

Today, when DacFx reverse engineers a database, it issues a brutal query that punches SQL Server in the face. The query is 380K in size, and does all kinds of complicated joins and fancy TSQL manipulation. See file DacFxReverseEngineeringQuery2016.sql. After viewing that file and changing your pants, you can probably guess that executing this query is very inefficient. Executing this query against an Azure 'Basic SKU' database, pegs the DTU quota, making the database unusable for a few seconds after execution.

Not only are these queries inefficient, they are expensive to maintain. All of the queries are handcrafted against a SQL version. When we release a new version of SQL, we cut and paste the query text, along with the SqlClient processing code, and then update the copy to handle the new system catalog tables and columns.

CatalogStore Reverse Engineering

This project take a different approach. SQL Server exposes rich meta data over the system catalog. We leverage this, and code generate a library that can execute simple 'select *' statements for each system catalog table. We take the results, and place them all in a sqlite database. Then, we code generate an entity framework model over the sqlite system catalog tables. This give us a nice type-safe query experience over the system catalog, with all processing occurring on the client side. Because everything is code generated, maintenance should be lower.

This design can potentially have other benefits

  • Opens the door to implement an efficient global search over all database objects.
  • The sqlite system catalog database can be persisted and cached on disk.
  • Intellisense cache

Benchmarks

Initial benchmarks indicate CatalogStore is in-line with DacFx when reverse engineering small databases. However as object counts grow, CatalogStore scales better than DacFx.

EngineDatabaseObject CountTime
DacFxAdventureWorks16031 sec
CatalogStoreAdventureWorks16031 sec
DacFxWideWorldImporters26181 sec
CatalogStoreWideWorldImporters26181 sec
DacFxDynamics52306 sec
CatalogStoreDynamics52302 sec
DacFxDynamics x 2787118 sec
CatalogStoreDynamics x 278714 sec
DacFxDynamics x 5157941 min
CatalogStoreDynamics x 51579411 sec
DacFxDynamics x 10289993 min
CatalogStoreDynamics x 102899921 sec
DacFxDynamics x 205540914 min
CatalogStoreDynamics x 205540943 sec
DacFxAzure Basic AdventureWorks16016 sec
CatalogStoreAzure Basic AdventureWorks16018 sec
DacFxAzure S3 AdventureWorks16012 sec
CatalogStoreAzure S3 AdventureWorks16012 sec
DacFxAzure P1 AdventureWorks16031 sec
CatalogStoreAzure P1 AdventureWorks16031 sec

Projects

Microsoft.SqlServer.CatalogStore

  • The code generated CatalogStore

CatalogStoreCodeGenerator

Microsoft.SqlServer.CatalogStore.Tests

  • Has tests to compare the execution of the DacFx query with the CatalogStore version.

ReverseEngineeringTableDump

  • Uses ScriptDom to parse the DacFx reverse engineering queries for Azure and SQL 2016, and generates the Catalog.Whitelist.txt file, which is input into the code generation process. This way, both DacFx and CatalogStore query the same tables.

TODO

  • Explicit mapping of primary keys
  • Figure out how to handle sql_variants - currently not mapped
  • Handle timespan column types - currently not mapped
  • Introduce loading profiles: lazy, object explorer, reverse engineer

About

An experimental .NET Core library to more efficiently reverse engineer a SQL Server database.

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages