MYSQL: Index keyword in create table and when to use it

前端 未结 2 647
北海茫月
北海茫月 2021-01-31 02:44

What does index keyword mean and what function it serves? I understand that it is meant to speed up querying, but I am not very sure how this can be done.

W

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-31 03:08

    This keyword means that you are creating an index on column blog_post_id along with the table.

    Queries like that:

    SELECT *
    FROM blog_comment
    WHERE blog_post_id = @id
    

    will use this index to search on this field and run faster.

    Also, there is a foreign key on this column.

    When you decide to delete a blog post, the database will need check against this table to see there are no orphan comments. The index will also speed up this check, so queries like

    DELETE
    FROM blog_post
    WHERE ...
    

    will also run faster.

提交回复
热议问题