您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MYSQL入门学习之九:索引的简单操作_MySQL

2025/7/4 7:27:50发布12次查看
bitscn.com
mysql入门学习之九:索引的简单操作
相关链接:
mysql入门学习之一:基本操作
http:///database/201212/173868.html
mysql入门学习之二:使用正则表达式搜索
http:///database/201212/173869.html
mysql入门学习之三:全文本搜索
http:///database/201212/173873.html
mysql入门学习之四:mysql的数据类型
http:///database/201212/175536.html
mysql入门学习之五:mysql的字符集
http:///database/201212/175541.html
mysql入门学习之六:mysql的运算符
http:///database/201212/175862.html
mysql入门学习之七:mysql常用函数
http:///database/201212/175864.html
mysql入门学习之八:数据库及表的基本操作
http:///database/201212/175867.html
一、创建索引    
        mysql常用的索引类型主要有以下几种:
1、普通索引
        create index idx_name on table_name(table_col(length));
        如果索引字段是char,varchar类型,length可以指定小于字段实际长度;如果是blob和text类型,必须指定length。
        mysql> create index idx_name on user(name(10));
        mysql> show index from user;
        +-------+------------+----------+--------------+-------------+-----------+
        | table | non_unique | key_name | seq_in_index | column_name | collation |
        +-------+------------+----------+--------------+-------------+-----------+
        | user  |          1 | idx_name |            1 | name        | a         |
        +-------+------------+----------+--------------+-------------+-----------+
2、唯一索引
        create unique index idx_name on table_name(table_col(length));
        它与普通索引类似,但不同的是,其索引列的值必须唯一,但允许有空值。
        mysql> create unique index idx_cn_name_u on user(cn_name(20));
        mysql> show index from user;
        +-------+------------+---------------+--------------+-------------+-----------+
        | table | non_unique | key_name      | seq_in_index | column_name | collation |
        +-------+------------+---------------+--------------+-------------+-----------+
        | user  |          0 | idx_cn_name_u |            1 | cn_name     | a         |
        | user  |          1 | idx_name      |            1 | name        | a         |
        +-------+------------+---------------+--------------+-------------+-----------+
3、主键索引
        alter table table_name add primary key (table_col);
        它是一种特殊的唯一索引,且不允许有空值。一个表只能有一个主键索引。
        mysql> alter table user add primary key (id);
        mysql> show index from user;
        +-------+------------+---------------+--------------+-------------+
        | table | non_unique | key_name      | seq_in_index | column_name |
        +-------+------------+---------------+--------------+-------------+
        | user  |          0 | primary       |            1 | id          |
        | user  |          0 | idx_cn_name_u |            1 | cn_name     |
        | user  |          1 | idx_name      |            1 | name        |
        +-------+------------+---------------+--------------+-------------+
4、组合索引
        create index idx_name on table_name(table_col_1,table_col_2,...,table_col_n);
        它允许使用多个列作为索引列。
        mysql> create index idx_name_sex on user(name,sex);
        mysql> show index from user;
        +-------+------------+---------------+--------------+-------------+-----------+-
        | table | non_unique | key_name      | seq_in_index | column_name | collation |
        +-------+------------+---------------+--------------+-------------+-----------+-
        | user  |          0 | primary       |            1 | id          | a         |
        | user  |          0 | idx_cn_name_u |            1 | cn_name     | a         |
        | user  |          1 | idx_name      |            1 | name        | a         |
        | user  |          1 | idx_name_sex  |            1 | name        | a         |
        | user  |          1 | idx_name_sex  |            2 | sex         | a         |
        +-------+------------+---------------+--------------+-------------+-----------+-    
二、删除索引
        drop index idx_name on table_name;
        alter table table_name drop index idx_name;
        alter table table_name drop primary key;
        mysql> alter table user drop primary key;
        mysql> show keys from user;
        +-------+------------+---------------+--------------+-------------+-
        | table | non_unique | key_name      | seq_in_index | column_name |
        +-------+------------+---------------+--------------+-------------+-
        | user  |          0 | idx_cn_name_u |            1 | cn_name     |
        | user  |          1 | idx_name      |            1 | name        |
        | user  |          1 | idx_name_sex  |            1 | name        |
        | user  |          1 | idx_name_sex  |            2 | sex         |
        +-------+------------+---------------+--------------+-------------+-
三、查看索引
        show index from table_name;
        show keys from table_name;
        查看索引语句的一个完全输出类似如下:
        mysql> show keys from user;
        +-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
        | table | non_unique | key_name      | seq_in_index | column_name | collation | cardinality | sub_part | packed | null | index_type | comment |
        +-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
        | user  |          0 | idx_cn_name_u |            1 | cn_name     | a         |        null |       20 | null   | yes  | btree      |         |
        | user  |          1 | idx_name      |            1 | name        | a         |        null |       10 | null   | yes  | btree      |         |
        | user  |          1 | idx_name_sex  |            1 | name        | a         |        null |     null | null   | yes  | btree      |         |
        | user  |          1 | idx_name_sex  |            2 | sex         | a         |        null |     null | null   | yes  | btree      |         |
        +-------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
        其中:
        table:表的名称
        non_unique:如果索引不能包括重复词,则为0。如果可以,则为1。
        key_name:索引的名称。
        seq_in_index:索引中的列序列号,从1开始。
        column_name:列名称。
        collation:列以什么方式存储在索引中。在mysql中,有值‘a’(升序)或null(无分类)。
        cardinality:索引中唯一值的数目的估计值。通过运行analyze table或myisamchk -a可以更新。基数根据被存储为整数的统计数据来计数,所以即使对于小型表,该值也没有必要是精确的。基数越大,当进行联合时,mysql使用该索引的机会就越大。
        sub_part:如果列只是被部分地编入索引,则为被编入索引的字符的数目。如果整列被编入索引,则为null。
        packed:指示关键字如何被压缩。如果没有被压缩,则为null。
        null:如果列含有null,则含有yes。如果没有,则该列含有no。
        index_type:使用的索引类型(btree, fulltext, hash, rtree)。
        comment:索引说明。
四、使用索引的注意事项
1、索引不会包含有null值的列
        只要列中包含有null值都将不会被包含在索引中,复合索引中只要有一列含有null值,那么这一列对于此复合索引就是无效的。所以在数据库设计时尽量不要让字段的默认值为null。
2、使用短索引
        对列进行索引,如果可能应该指定一个前缀长度。例如,如果有一个char(255)的列,如果在前10个或20个字符内,多数值是惟一的,那么就不要对整个列进行索引。短索引不仅可以提高查询速度而且可以节省磁盘空间和i/o操作。
3、不要在列上进行运算
        在列上进行运算,将导致索引失效而进行全表扫描。
4、不使用not和操作
5、索引列排序
        mysql查询只使用一个索引,因此如果where子句中已经使用了索引的话,那么order by中的列是不会使用索引的。因此数据库默认排序可以符合要求的情况下不要使用排序操作;尽量不要包含多个列的排序,如果需要最好给这些列创建复合索引。
bitscn.com
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product