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

juur picture juur · Sep 6, 2010 · Viewed 25.7k times · Source

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?

Answer

Martin Smith picture Martin Smith · Sep 6, 2010

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