What is an index in SQL?

后端 未结 11 2166
粉色の甜心
粉色の甜心 2020-11-22 11:01

What is an index in SQL? Can you explain or reference to understand clearly?

Where should I use an index?

相关标签:
11条回答
  • 2020-11-22 11:37

    Well in general index is a B-tree. There are two types of indexes: clustered and nonclustered.

    Clustered index creates a physical order of rows (it can be only one and in most cases it is also a primary key - if you create primary key on table you create clustered index on this table also).

    Nonclustered index is also a binary tree but it doesn't create a physical order of rows. So the leaf nodes of nonclustered index contain PK (if it exists) or row index.

    Indexes are used to increase the speed of search. Because the complexity is of O(log N). Indexes is very large and interesting topic. I can say that creating indexes on large database is some kind of art sometimes.

    0 讨论(0)
  • 2020-11-22 11:37

    An index is an on-disk structure associated with a table or view that speeds retrieval of rows from the table or view. An index contains keys built from one or more columns in the table or view. These keys are stored in a structure (B-tree) that enables SQL Server to find the row or rows associated with the key values quickly and efficiently.

    Indexes are automatically created when PRIMARY KEY and UNIQUE constraints are defined on table columns. For example, when you create a table with a UNIQUE constraint, Database Engine automatically creates a nonclustered index.

    If you configure a PRIMARY KEY, Database Engine automatically creates a clustered index, unless a clustered index already exists. When you try to enforce a PRIMARY KEY constraint on an existing table and a clustered index already exists on that table, SQL Server enforces the primary key using a nonclustered index.

    Please refer to this for more information about indexes (clustered and non clustered): https://docs.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described?view=sql-server-ver15

    Hope this helps!

    0 讨论(0)
  • 2020-11-22 11:42

    INDEXES - to find data easily

    UNIQUE INDEX - duplicate values are not allowed

    Syntax for INDEX

    CREATE INDEX INDEX_NAME ON TABLE_NAME(COLUMN);
    

    Syntax for UNIQUE INDEX

    CREATE UNIQUE INDEX INDEX_NAME ON TABLE_NAME(COLUMN);
    
    0 讨论(0)
  • 2020-11-22 11:43

    An index is used for several different reasons. The main reason is to speed up querying so that you can get rows or sort rows faster. Another reason is to define a primary-key or unique index which will guarantee that no other columns have the same values.

    0 讨论(0)
  • 2020-11-22 11:44

    INDEX is a performance optimization technique that speeds up the data retrieval process. It is a persistent data structure that associated with a Table (or View) in order to increase performance during retrieving the data from that table (or View).

    Index based search is applied more particularly when your queries include WHERE filter. Otherwise, i.e, a query without WHERE-filter selects whole data and process. Searching whole table without INDEX is called Table-scan.

    You will find exact information for Sql-Indexes in clear and reliable way: follow these links:

    1. For cocnept-wise understanding: http://dotnetauthorities.blogspot.in/2013/12/Microsoft-SQL-Server-Training-Online-Learning-Classes-INDEX-Overview-and-Optimizations.html
    2. For implementation-wise understanding: http://dotnetauthorities.blogspot.in/2013/12/Microsoft-SQL-Server-Training-Online-Learning-Classes-INDEX-Creation-Deletetion-Optimizations.html
    0 讨论(0)
提交回复
热议问题