0% found this document useful (0 votes)
41 views36 pages

Chapter 17 SQL Metadata

Uploaded by

Đoan Trang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
41 views36 pages

Chapter 17 SQL Metadata

Uploaded by

Đoan Trang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 36
PART IV Performance and Maintenance CHAPTER 17 SQL Server Metadata Metadata is data that describes other data, SQL Server exposes a vast array of metadata including structural metadata, which describes every object, and descriptive metadata, which describes the data itself. Metadata is exposed through a series of + Catalog views + Information schema views + Dynamic management views and functions + System functions + System stored procedures In this chapter, we will discuss how metadata can be used to perform actions at the instance level, such as expose registry values, examine how metadata can assist in capacity planning, and discuss how metadata can be used for troubleshooting and performance tuning. Finally, we will see how metadata can be used to drive automated maintenance. Tip Metadata is a high topic worthy of a book in its own right. | therefore encourage you to play with other metadata objects, which may not be covered in this chapter. Introducing Metadata Objects Catalog views reside in the sys schema. There are many catalog views, some of the most useful of which, such as sys.master_files, are explored in this chapter. Listing 17-1 shows an example of how to use a catalog view to produce a list of databases that are in FULL recovery model. 623 © Pewer A. Carter 2018 P.A. Carter, Pro SQL Server 2019 Administration, https: /doi.org/10.1007/978-1-4842-5089-1_17 CHAPTER 17 SQL SERVER METADATA Listing 17-1. Using Catalog Views SELECT name FROM sys.databases WHERE recovery model_desc = 'FULL' ; Information schema views reside in the INFORMATION_SCHEMA schema. They return less detail than catalog views but are based on the ISO standards. This means that you can port your queries between RDBMS (Relational Database Management Systems). Listing 17-2 shows an example of using information schema views to produce a list of principals that have been granted SELECT access to the Chapter10. dbo. SensitiveData table. Listing 17-2. Using Information Schema Views USE Chapterzo co SELECT GRANTEE, PRIVILEGE_TYPE FROM INFORMATION SCHEMA. TABLE_PRIVILEGES WHERE TABLE_SCHEMA = ‘dbo’ AND TABLE_NAME = 'SensitiveData’ AND PRIVILEGE TYPE = ‘SELECT’ ; Many dynamic management views and functions are available in SQL Server. Collectively, they are known as DMVs and they provide information about the current state of the instance, which you can use for troubleshooting and tuning performance. The following categories of DMV are exposed in SQL Server 2019: + AlwaysOn Availability Groups + Change data capture + Change tracking + Common language runtime (CLR) + Database mirroring + Databases + Execution 624 CHAPTER 17 SQL SERVER METADATA + Extended events + FILESTREAM and FileTable + Full-text search and semantic search + Geo-Replication + Indexes + Vo + Memory-optimized tables + Objects + Query notifications + Replication + Resource Governor * Security + Server + Service broker + Spatial + SQL Data Warehouse and POW * SQLServer operating system + Stretch Databases + Transactions ‘We demonstrate and discuss how to use DMVs many times throughout this chapter. DMVs always begin with a dm_prefix, followed by two to four characters that describe the category of the object—for example, os_for operating system, db_for database, and exec_for execution. This is followed by the name of the object. In Listing 17-3, you can see two things: an example of how to use a dynamic management view to find allist of logins that are currently connected to the Chapter16 database and a dynamic management function you can use to produce details of the pages that store the data relating to the Chapter16 .dbo. Customers table 625 CHAPTER 17 SQL SERVER METADATA Listing 17-3. Using Dynamic Management Views and Functions USE Chapter16 -This database will exist if you followed the examples in Chapteri6 of this book co --Find logins connected to the Chapter16 database SELECT login_name FROM sys.dm_exec_sessions WHERE database _id = DB_ID(‘Chapter16") ; --Return details of the data pages storing the Chapter16.dbo.Customers table SELECT * FROM sys.dm_db_database_page_allocations(DB_ID('Chapter16"), OBJECT_1D(‘ dbo. Customers"), NULL, NULL, “DETAILED') ; SQL Server also offers many metadata-related system functions, such as DB_ID() and OBJECT_ID(), which we used in Listing 17-3. Another example of a metadata-related system function is DATALENGTH, which we use in Listing 17-4 to return the length of each value in the LastName column of the Chapter16..dbo. Customers table. Listing 17-4. Using System Functions USE Chapter16 co SELECT DATALENGTH(LastName) FROM dbo.Customers ; Server-Level and Instance-Level Metadata Many forms of metadata are available for the server and instance. Server-level metadata can be very useful for DBAs who need to find configuration information or troubleshoot an issue when they do not have access to the underlying operating system. For example, the dn_server category of DMVs offers views that allow you to check the status of server 626 CHAPTER 17 SQL SERVER METADATA audits, view SQL Server's Registry keys, find the location of memory dump files, and find details of the instance's services. In the following sections, we discuss how to view the Registry keys associated with the instance, expose details of SQL Server's services, and view the contents of the buffer cache. Exposing Registry Values The sys.dn_server_registry DMV exposes key registry entries pertaining to the instance. The view returns three columns, which are detailed in Table 17-1 Table 17-1. sys.dm_server_registry Columns Column Description Registry key The name of the Registry key Value_name The name of the key’s value Value_data The data contained within the value Avery useful piece of information that you can find in the sys.dn_server_registry DMVis the port number on which SQL Server is currently listening. The query in Listing 17-5 uses the sys. dn_server_registry DMV to return the port on which the instance is listening, assuming the instance is configured to listen on all IP addresses. Listing 17-5. Finding the Port Number SELECT * FROM ( SELECT CASE WHEN value_name = ‘tcport' AND value_data <> ‘' THEN value_data WHEN value_name = 'tcpport' AND value_data = "' THEN ( SELECT value_data FROM sys.dm_server_registry WHERE registry key LIKE ‘%ipall' AND value_name = 'tepdynamicports’ ) 627 CHAPTER 17 SQL SERVER METADATA END PortNumber FROM sys.dm_server_registry WHERE registry key LIKE '%IPALI' ) a WHERE a.PortNumber IS NOT NULL ; Another useful feature of this DMV is its ability to return the startup parameters of the SQL Server service. This is particularly useful if you want to find out if switches such as -E have been configured for the instance. The -E switch increases the number of extents that are allocated to each file in the round-robin algorithm. The query in Listing 17-6 displays the startup parameters configured for the instance. Listing 17-6. Finding Startup Parameters SELECT + FROM sys.dm_server_registry WHERE value_name LIKE "SQLArg®’ ; Exposing Service Details Another useful DMV within the dn_server category is sys.dn_server_services, which exposes details of the services the instance is using. Table 17-2 describes the columns returned. Table 17-2. sys.dm_server_services Columns Column Description Servicename The name of the service. Startup_type An integer representing the startup type of the service, Startup_desc A textual description of the startup type of the service Status An integer representing the current status of the service. Status_desc A textual description of the current service state Process_id The process ID of the service. Last_startup_time The date and time that the service last started, Service_account The account used to run the service. (continued) 628 CHAPTER 17 SQL SERVER METADATA Table 17-2. (continued) Column Description Filename The file name of the service, including the full file path Is_clustered 1 indicates that the service is clustered; 0 indicates that it is stand-alone. Clusternodename Ifthe service is clustered, this column indicates the name of the node on Which the service is running, The query in Listing 17-7 returns the name of each service, its startup type, its current status, and the name of the service account that runs the service. Listing 17-7. Exposing Service Details SELECT servicename »startup_type_desc ,status_desc ,service_account FROM sys.dm_server_services ; Analyzing Buffer Cache Usage The dn_os category of DMV exposes 41 objects that contain information about the current status of SQLOS, although only 31 of these are documented. A particularly useful DMV in the dm_os category, which exposes the contents of the buffer cache, is sys.dn_os_buffer_descriptors. When queried, this object returns the columns detailed in Table 17-3. Table 17-3. sys.dm_os_buffer_descriptors Columns Column Description Database_id The ID of the database that the page is from File_id The ID of the file that the page is from Page_id The ID of the page Page_level The index level of the page (continued) 629

You might also like