How do I add indices to MySQL tables?

后端 未结 7 1954
北荒
北荒 2020-11-22 13:54

I\'ve got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run

SELECT * FROM table WHERE id = \'1\';

the

7条回答
  •  梦谈多话
    2020-11-22 14:05

    Indexes of two types can be added: when you define a primary key, MySQL will take it as index by default.

    Explanation

    Primary key as index

    Consider you have a tbl_student table and you want student_id as primary key:

    ALTER TABLE `tbl_student` ADD PRIMARY KEY (`student_id`)
    

    Above statement adds a primary key, which means that indexed values must be unique and cannot be NULL.

    Specify index name

    ALTER TABLE `tbl_student` ADD INDEX student_index (`student_id`)
    

    Above statement will create an ordinary index with student_index name.

    Create unique index

    ALTER TABLE `tbl_student` ADD UNIQUE student_unique_index (`student_id`)
    

    Here, student_unique_index is the index name assigned to student_id and creates an index for which values must be unique (here null can be accepted).

    Fulltext option

    ALTER TABLE `tbl_student` ADD FULLTEXT student_fulltext_index (`student_id`)
    

    Above statement will create the Fulltext index name with student_fulltext_index, for which you need MyISAM Mysql Engine.

    How to remove indexes ?

    DROP INDEX `student_index` ON `tbl_student`
    

    How to check available indexes?

    SHOW INDEX FROM `tbl_student`
    

提交回复
热议问题