What is the SQL index name for?

后端 未结 6 577
深忆病人
深忆病人 2021-01-04 10:58

In SQL, when I create a database index, I\'m required to give the index a name, as in

CREATE INDEX timestamp_index ON hit (timestamp);

The

相关标签:
6条回答
  • 2021-01-04 11:05
    ALTER INDEX timestamp_index ...
    

    T-SQL link

    0 讨论(0)
  • 2021-01-04 11:05

    How else would you identify the index?

    There is probably logging attached as well.

    0 讨论(0)
  • 2021-01-04 11:06

    Sometimes it's necessary to do index hinting, whereby you tell the query engine what index it should be using. If your indexes don't have named, you can't do this.

    0 讨论(0)
  • 2021-01-04 11:08

    It's also helpful when viewing/reviewing execution plans (at least in SQL Server), since the indexes used are shown by name.

    0 讨论(0)
  • 2021-01-04 11:11

    I often feel the same way about filenames. Does an mp3 filename need to contain metadata (artist, album, is not a podcast, etc) when the file's attributes does a better job? The files on my ipod do not resemble the corresponding filenames on my hard drive because it uses an index and I wasn't required to think up a name for that index!

    Unlike a data element name, the name of an index is arbitrary. The ideal index is one that gets created and forever more 'looked after' by the system as the system knows best. Sadly, the reality is that on the rare occassions you may need to do something with it (e.g. drop it!). You could refer to a system dictionary to identify the index based on its attributes, usually just the columns it comprises.

    0 讨论(0)
  • 2021-01-04 11:25

    Here are a couple of things to think about:

    How would you rebuild the index?

    How would you defragment/reorganize the index?

    how would you alter the index?

    How would you look at the index to see what columns are in it?

    When viewing an execution plan how would you know what index was used?

    How would you disable the index when doing BULK INSERT/BCP?

    How would you use the sys.dm_db_missing_index_details DMV when you don't know the name?

    BTW when you create a primary key a clustered index will be created by default, SQL Server will give it a name for you

    create table bla  (id int primary key)
    go
    
    select * from sys.sysobjects s 
    join sys.sysobjects s2 on s.parent_obj = s2.id
    where s2.name = 'bla'
    

    Here is the name that got generated for that index PK__bla__3213E83F66603565

    0 讨论(0)
提交回复
热议问题