Table of Contents
解剖SQLSERVER 第九篇  OrcaMDF现在能通过系统DMVs显示元数据(译)
Home Database Mysql Tutorial 解剖SQLSERVER 第九篇 OrcaMDF现在能通过系统DMVs显示元数据(

解剖SQLSERVER 第九篇 OrcaMDF现在能通过系统DMVs显示元数据(

Jun 07, 2016 pm 03:19 PM
sqlserver Now pass

解剖SQLSERVER 第九篇 OrcaMDF现在能通过系统DMVs显示元数据(译) http://improve.dk/orcamdf-now-exposes-metadata-through-system-dmvs/ 我坐在去丹麦的火车上,参加在北欧的SQL RALLY会议的最后一站。在演讲的过程中,我根据OrcaMDF 的工作比较含蓄地宣

解剖SQLSERVER 第九篇  OrcaMDF现在能通过系统DMVs显示元数据(译)

http://improve.dk/orcamdf-now-exposes-metadata-through-system-dmvs/

我坐在去丹麦的火车上,参加在北欧的SQL RALLY会议的最后一站。在演讲的过程中,我根据OrcaMDF 的工作比较含蓄地宣布了OrcaMDF可以读取元数据

现在,我也不妨在这里分享。除了在OrcaMDF中扩大对核心引擎的支持之外,另一个重要的我想要实现的主要功能之一就是显示关于的你的数据库的元数据

你的表,列,数据库是怎麽展开的?

解剖SQLSERVER 第九篇  OrcaMDF现在能通过系统DMVs显示元数据(

 

 

避免错误的抽象

我最初的想法是在上层对象下面创建自己的抽象层,你可以通过database.GetMetadata().UserTables 枚举出你的所有的用户表

你可以获取用户表的列表,包括数据列。从开发层面考虑这是一个非常清晰的接口,每样东西都是普通的.NET对象。

然而,这需要我自己去定义抽象- 如何去划分出哪些数据需要暴露,哪些数据不能,怎样抽象对于DBA是最自然的,需不需要使用

类似SQLSERVER的 sys. DMVs

 

从SQLSERVER里暴露出内置的DMVs

我花费了一些时间考虑最后会有哪类人使用OrcaMDF ,最后得出结论是这个世界可能只有四类人会使用

而将这四类人再拆分开最终只有DBA和SQLSERVER开发人员。而他们通常会通过DVMs 例如sys.tables,sys.columns,sys.indexes

来浏览SQLSERVER数据库的元数据。那么能够使我有信心开发这个功能的动力是我已经能解析出所有的系统表,并且使用 SELECT OBJECT_DEFINITION() 

我能够看到内置的系统DMVs的源代码。因此,创建我自己的内置DMVs的复制品是一件很简单事

 

 

在OrcaMDF里如何使用DMVs 

比如我们需要获取某个表的所有列的信息,我们会建立如下SQL语句

<span>SELECT</span><span>
    c.</span><span>*</span>
<span>FROM</span><span>
    sys.columns c
</span><span>INNER</span> <span>JOIN</span><span>
    sys.tables t </span><span>ON</span> c.<span>object_id</span> <span>=</span> t.<span>object_id</span>
<span>WHERE</span><span>
    t.name </span><span>=</span> <span>'</span><span>Persons</span><span>'</span>
Copy after login

在OrcaMDF里的C#代码里看起来会像这样:

<span>using</span> (<span>var</span> db = <span>new</span> Database(<span>new</span>[] { <span>@"</span><span>C:Test.mdf</span><span>"</span><span> }))
{
    </span><span>var</span> sys =<span> db.Dmvs;

    </span><span>var</span> table = sys.Tables.Where(t => t.Name == <span>"</span><span>Persons</span><span>"</span><span>).Single();
    </span><span>var</span> columns = sys.Columns.Where(c => c.ObjectID ==<span> table.ObjectID);

    </span><span>foreach</span> (<span>var</span> col <span>in</span><span> columns)
        Console.WriteLine(col.Name);
}</span>
Copy after login

如果你更喜欢SQL-esque LINQ的语法,你当然可以像下面那样做

<span>using</span> (<span>var</span> db = <span>new</span> Database(<span>new</span>[] { <span>@"</span><span>C:Test.mdf</span><span>"</span><span> }))
{
    </span><span>var</span> sys =<span> db.Dmvs;

    </span><span>var</span> columns =    <span>from</span> c <span>in</span><span> sys.Columns
            join t </span><span>in</span><span> sys.Tables on c.ObjectID equals t.ObjectID
            </span><span>where</span> t.Name == <span>"</span><span>Persons</span><span>"</span>
            <span>select</span><span> c;

    </span><span>foreach</span> (<span>var</span> col <span>in</span><span> columns)
        Console.WriteLine(col.Name);
}</span>
Copy after login

不管使用哪种写法,结果都会像下面那样

解剖SQLSERVER 第九篇  OrcaMDF现在能通过系统DMVs显示元数据(

 

 

可用的DMVs有哪些

如果你获取到最新的已提交的OrcaMDF代码,你可以访问下面的DMVs,他们是通过SQLSERVER暴露出来的

<span>sys.columns
sys.indexes
sys.index_columns
sys.objects
sys.objects$
sys.system_internals_allocation_units
sys.system_internals_partitions
sys.system_internals_partition_columns
sys.tables
sys.types</span>
Copy after login

 

如果你对DMV 有期望可以跟我联系,我会让你愿望成真!

 

第九篇完

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that the object named already exists in the sqlserver database How to solve the problem that the object named already exists in the sqlserver database Apr 05, 2024 pm 09:42 PM

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

What to do if the sqlserver service cannot be started What to do if the sqlserver service cannot be started Apr 05, 2024 pm 10:00 PM

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

How to check sqlserver port number How to check sqlserver port number Apr 05, 2024 pm 09:57 PM

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

Where is the sqlserver database? Where is the sqlserver database? Apr 05, 2024 pm 08:21 PM

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

How to recover accidentally deleted database in sqlserver How to recover accidentally deleted database in sqlserver Apr 05, 2024 pm 10:39 PM

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

How to solve Java connection SqlServer error How to solve Java connection SqlServer error May 01, 2023 am 09:22 AM

The problem was found that this time I was using the SqlServer database, which I had not used before, but the problem was not serious. After I connected the SqlServer according to the steps in the requirements document, I started the SpringBoot project and found an error, as follows: At first I thought it was a SqlServer connection. There was a problem, so I went to check the database and found that everything was normal. I first asked my colleagues if they had such a problem, and found that they did not, so I started my best part, facing Baidu. programming. The specific error message I started to solve was this, so I started Baidu error reporting: ERRORc.a.d.p.DruidDataSource$CreateCo

See all articles