What is an index in SQL?

后端 未结 11 2150
粉色の甜心
粉色の甜心 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:21

    An index is used to speed up the performance of queries. It does this by reducing the number of database data pages that have to be visited/scanned.

    In SQL Server, a clustered index determines the physical order of data in a table. There can be only one clustered index per table (the clustered index IS the table). All other indexes on a table are termed non-clustered.

    • SQL Server Index Basics

    • SQL Server Indexes: The Basics

    • SQL Server Indexes

    • Index Basics

    • Index (wiki)

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

    Indexes are all about finding data quickly.

    Indexes in a database are analogous to indexes that you find in a book. If a book has an index, and I ask you to find a chapter in that book, you can quickly find that with the help of the index. On the other hand, if the book does not have an index, you will have to spend more time looking for the chapter by looking at every page from the start to the end of the book.

    In a similar fashion, indexes in a database can help queries find data quickly. If you are new to indexes, the following videos, can be very useful. In fact, I have learned a lot from them.

    Index Basics
    Clustered and Non-Clustered Indexes
    Unique and Non-Unique Indexes
    Advantages and disadvantages of indexes

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

    An index is used to speed up searching in the database. MySQL have some good documentation on the subject (which is relevant for other SQL servers as well): http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

    An index can be used to efficiently find all rows matching some column in your query and then walk through only that subset of the table to find exact matches. If you don't have indexes on any column in the WHERE clause, the SQL server has to walk through the whole table and check every row to see if it matches, which may be a slow operation on big tables.

    The index can also be a UNIQUE index, which means that you cannot have duplicate values in that column, or a PRIMARY KEY which in some storage engines defines where in the database file the value is stored.

    In MySQL you can use EXPLAIN in front of your SELECT statement to see if your query will make use of any index. This is a good start for troubleshooting performance problems. Read more here: http://dev.mysql.com/doc/refman/5.0/en/explain.html

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

    First we need to understand how normal (without indexing) query runs. It basically traverse each rows one by one and when it finds the data it returns. Refer the following image. (This image has been taken from this video.)

    So suppose query is to find 50 , it will have to read 49 records as a linear search.

    Refer the following image. (This image has been taken from this video)

    When we apply indexing, the query will quickly find out the data without reading each one of them just by eliminating half of the data in each traversal like a binary search. The mysql indexes are stored as B-tree where all the data are in leaf node.

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

    If you're using SQL Server, one of the best resources is its own Books Online that comes with the install! It's the 1st place I would refer to for ANY SQL Server related topics.

    If it's practical "how should I do this?" kind of questions, then StackOverflow would be a better place to ask.

    Also, I haven't been back for a while but sqlservercentral.com used to be one of the top SQL Server related sites out there.

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

    A clustered index is like the contents of a phone book. You can open the book at 'Hilditch, David' and find all the information for all of the 'Hilditch's right next to each other. Here the keys for the clustered index are (lastname, firstname).

    This makes clustered indexes great for retrieving lots of data based on range based queries since all the data is located next to each other.

    Since the clustered index is actually related to how the data is stored, there is only one of them possible per table (although you can cheat to simulate multiple clustered indexes).

    A non-clustered index is different in that you can have many of them and they then point at the data in the clustered index. You could have e.g. a non-clustered index at the back of a phone book which is keyed on (town, address)

    Imagine if you had to search through the phone book for all the people who live in 'London' - with only the clustered index you would have to search every single item in the phone book since the key on the clustered index is on (lastname, firstname) and as a result the people living in London are scattered randomly throughout the index.

    If you have a non-clustered index on (town) then these queries can be performed much more quickly.

    Hope that helps!

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