How to use index in select statement?

前端 未结 8 630
误落风尘
误落风尘 2020-12-12 12:42

Lets say in the employee table, I have created an index(idx_name) on the emp_name column of the table.

Do I need to explicitly specify the index name in

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 13:19

    In general, the index will be used if the assumed cost of using the index, and then possibly having to perform further bookmark lookups is lower than the cost of just scanning the entire table.

    If your query is of the form:

    SELECT Name from Table where Name = 'Boris'
    

    And 1 row out of 1000 has the name Boris, it will almost certainly be used. If everyone's name is Boris, it will probably resort to a table scan, since the index is unlikely to be a more efficient strategy to access the data.

    If it's a wide table (lot's of columns) and you do:

    SELECT * from Table where Name = 'Boris'
    

    Then it may still choose to perform the table scan, if it's a reasonable assumption that it's going to take more time retrieving the other columns from the table than it will to just look up the name, or again, if it's likely to be retrieving a lot of rows anyway.

提交回复
热议问题