【问题标题】:Automatic index on a table表上的自动索引
【发布时间】:2019-01-25 17:12:55
【问题描述】:

我试图找出是否在表上自动创建了任何索引。我使用这个link 在Employees 表上查找索引,该表有4 列,即empid、empName、salary 和dept。查询显示该表已经有一个索引 - 我在表上没有键。这个link 表示不会在表上自动创建非聚集索引。那么Employees表上已经存在的索引类型是什么?此外,索引名称显示为空。

用于在Employees表上查找索引的创建语句和查询如下:

 Create table Employees( 
      empid int,
      empName varchar(100),
      salary int,
      dept varchar(10)
    )

   select * from sys.indexes
   where object_id = (select object_id 
                     from sys.objects where name = 'Employees')

【问题讨论】:

  • 主键会添加一个唯一索引,以强制唯一性。 empid 可能是主键
  • 我的桌子上没有主键。如果它有任何帮助,索引已经在堆上创建。
  • 能分享一下表的create语句,以及你用来查找索引的实际查询吗?
  • 添加了查询。此外,如果有任何帮助,SQL Server Management Studio 中的对象资源管理器不会在表上显示任何索引。
  • 根据您的查询,除非您告知,否则不会创建索引。

标签: sql-server


【解决方案1】:

这是您问题中sys.indexes 查询的输出:

+------------+------+----------+------+-----------+-----------+---------------+----------------+----------------+----------------------+-------------+-----------+-------------+-----------------+----------------------------+-----------------+------------------+------------+-------------------+-------------------+---------------------------+--------------+
| object_id  | name | index_id | type | type_desc | is_unique | data_space_id | ignore_dup_key | is_primary_key | is_unique_constraint | fill_factor | is_padded | is_disabled | is_hypothetical | is_ignored_in_optimization | allow_row_locks | allow_page_locks | has_filter | filter_definition | compression_delay | suppress_dup_key_messages | auto_created |
+------------+------+----------+------+-----------+-----------+---------------+----------------+----------------+----------------------+-------------+-----------+-------------+-----------------+----------------------------+-----------------+------------------+------------+-------------------+-------------------+---------------------------+--------------+
| 1746105261 | NULL |        0 |    0 | HEAP      |         0 |             1 |              0 |              0 |                    0 |           0 |         0 |           0 |               0 |                          0 |               1 |                1 |          0 | NULL              | NULL              |                         0 |            0 |
+------------+------+----------+------+-----------+-----------+---------------+----------------+----------------+----------------------+-------------+-----------+-------------+-----------------+----------------------------+-----------------+------------------+------------+-------------------+-------------------+---------------------------+--------------+

注意type_desc 的值为“HEAP”,表示没有聚集索引的表。所以返回的行是表本身而不是索引。 documentation for sys.indexes 状态(我的重点):

每个索引包含一行或表格对象的,例如表格, 视图或表值函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-22
    • 2012-05-26
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2013-01-13
    相关资源
    最近更新 更多