Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: should I use information_schema tables over sys tables?

In SQL Server there is two schemas for metadata:

  • INFORMATION_SCHEMA
  • SYS

I have heard that INFORMATION_SCHEMA tables are based on ANSI standard. When developing e.g. stored procedures, should it be wise to use INFORMATION_SCHEMA tables over sys tables?

like image 967
juur Avatar asked Sep 06 '10 18:09

juur


People also ask

What is the use of INFORMATION_SCHEMA in SQL Server?

INFORMATION_SCHEMA provides access to database metadata, information about the MySQL server such as the name of a database or table, the data type of a column, or access privileges. Other terms that are sometimes used for this information are data dictionary and system catalog.

What is INFORMATION_SCHEMA tables in SQL Server?

The INFORMATION_SCHEMA. TABLES view allows you to get information about all tables and views within a database. By default it will show you this information for every single table and view that is in the database.

Is information schema deprecated?

The INFORMATION_SCHEMA is a deprecated schema that I believe Microsoft keeps around because it's a notion of ANSI SQL (SQL-92) compliance. As the warning you quoted mentions, the INFORMATION_SCHEMA may be missing meta-data and information on some of the objects in your database.


2 Answers

I would always try to use the Information_schema views over querying the sys schema directly.

The Views are ISO compliant so in theory you should be able to easily migrate any queries across different RDBMS.

However, there have been some cases where the information that I need is just not available in a view.

I've provided some links with further information on the views and querying a SQL Server Catalog.

http://msdn.microsoft.com/en-us/library/ms186778.aspx

http://msdn.microsoft.com/en-us/library/ms189082.aspx

like image 37
codingbadger Avatar answered Sep 26 '22 17:09

codingbadger


Unless you are writing an application which you know for a fact will need to be portable or you only want quite basic information I would just default to using the proprietary SQL Server system views to begin with.

The Information_Schema views only show objects that are compatible with the SQL-92 standard. This means there is no information schema view for even quite basic constructs such as indexes (These are not defined in the standard and are left as implementation details.) Let alone any SQL Server proprietary features.

Additionally it is not quite the panacea for portability that one may assume. Implementations do still differ between systems. Oracle does not implement it "out of the box" at all and the MySql docs say:

Users of SQL Server 2000 (which also follows the standard) may notice a strong similarity. However, MySQL has omitted many columns that are not relevant for our implementation, and added columns that are MySQL-specific. One such column is the ENGINE column in the INFORMATION_SCHEMA.TABLES table.

Even for bread and butter SQL constructs such as foreign key constraints the Information_Schema views can be dramatically less efficient to work with than the sys. views as they do not expose object ids that would allow efficient querying.

e.g. See the question SQL query slow-down from 1 second to 11 minutes - why? and execution plans.

INFORMATION_SCHEMA

Plan

sys

Plan

like image 150
Martin Smith Avatar answered Sep 25 '22 17:09

Martin Smith