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

MySQL创造索引原则

2024/4/25 22:58:55发布7次查看
mysql创建索引原则 索引是一种特殊的数据结构,可以用来快速查询数据库的特定记录,建立是一中提高数据库性能的重要方式。 ? 内容:索引的意义,索引的设计,创建和删除 ? 可参考的资料: http://www.51cto.com/specbook/52/12911.htm ? 索引简介 索引是建立
mysql创建索引原则
索引是一种特殊的数据结构,可以用来快速查询数据库的特定记录,建立是一中提高数据库性能的重要方式。?内容:索引的意义,索引的设计,创建和删除?可参考的资料:http://www.51cto.com/specbook/52/12911.htm?索引简介索引是建立在表上的,有一列或者多列组成,并对这一列或者多列进行排序的一种结构。?所有存储引擎对每个表至少支持16个索引,总索引长度至少为256字节,索引有两种存储类型,包括b型树索引和哈希索引。?索引的优点是可以提高检索的速度,但是创建和维护索引需要耗费时间,这个时间随着数据量的增加而增加。?索引可以提高查询的速度,但是会影响插入的速度,当要插入大量的数据时,最好的办法是先删除索引,插入数据后再建立索引。?mysql的索引分为:普通索引,唯一性索引,全文索引,单列索引,多列索引和空间索引。?目前只有myisam存储引擎支持全文索引,innodb引擎5.6以下版本还不支持全文索引。则需要升级mysql。例如:从5.1升级到5.5,再升级到5.6版本。详细见mysql手册。http://dev.mysql.com/doc/refman/5.6/en/upgrading.html?索引的设计原则选择唯一性索引。为经常需要排序,分组和联合操作的字段建立索引。为常作为查询条件的字段建立索引。限制索引的数目。尽量使用数据量少的索引。尽量使用前缀来索引。如果字段的值很长,最好使用值的前缀来索引,如果只检索子酸的前面的若干字符,可以提高检索的速度。删除不再使用或者很少使用的索引。原则只是参考而不能拘泥。?创建索引三种方式:在创建表是创建索引,在已存在的表上创建索引和使用alter table语句创建索引。?mysql>?show?tables;
+----------------+
| tables_in_kiwi |
+----------------+
| stu |
+----------------+
1?row?in?set?(0.00 sec)
mysql>?create?table?indextest(id?int,?name?varchar(20), sex?boolean,?index?index_id(id));
query ok,?0?rows?affected (0.08?sec)
mysql>?desc?indextest;
+-------+-------------+------+-----+---------+-------+
| field |?type?|?null?|?key?|?default?| extra |
+-------+-------------+------+-----+---------+-------+
| id |?int(11) | yes | mul |?null?| |
|?name?|?varchar(20) | yes | |?null?| |
| sex | tinyint(1) | yes | |?null?| |
+-------+-------------+------+-----+---------+-------+
3?rows?in?set?(0.01?sec)
mysql>?explain?select?*?from?indextest?where?id?=?1?\g;
***************************?1.?row?***************************
id:?1
select_type:?simple
table: indextest
type:?ref
possible_keys: index_id
key: index_id
key_len:?5
ref: const
rows:?1
extra:?using?where
1?row?in?set?(0.00 sec)?创建单列索引,subject(10)是为了不查询全部信息而提高检索的速度。
mysql>?create?table?singlerow(id?int,name?varchar(20),subject?varchar(30),index?index_st(subject(10)));
query ok,?0?rows?affected (0.17?sec)
mysql>?show?create?table?singlerow\g;
***************************?1.?row?***************************
table: singlerow
create?table:?create?table?`singlerow` (
`id`?int(11)?default?null,
`name`?varchar(20)?default?null,
`subject`?varchar(30)?default?null,
key?`index_st` (`subject`(10))
) engine=innodb?default?charset=latin1
1?row?in?set?(0.00 sec)多列索引,空间索引类似。?在已存在的表上建立索引语法为: create [unique|fulltext|spatial] index index_name on table_name (property_name[length] [asc|desc]);mysql>?desc?stu;
+--------+-------------+------+-----+---------+----------------+
| field |?type?|?null?|?key?|?default?| extra |
+--------+-------------+------+-----+---------+----------------+
| id |?int(10) |?no?| pri |?null?| auto_increment |
| s_num |?int(10) | yes | mul |?null?| |
| course |?varchar(20) | yes | |?null?| |
| score |?varchar(4) | yes | |?null?| |
+--------+-------------+------+-----+---------+----------------+
4?rows?in?set?(0.05?sec)
mysql>?show?create?table?stu \g;
***************************?1.?row?***************************
table: stu
create?table:?create?table?`stu` (
`id`?int(10)?not?null?auto_increment,
`s_num`?int(10)?default?null,
`course`?varchar(20)?default?null,
`score`?varchar(4)?default?null,
primary?key?(`id`),
unique?key?`id` (`id`),
unique?key?`index_id` (`id`),
key?`grade_fk` (`s_num`)
) engine=myisam?default?charset=latin1
1?row?in?set?(0.00 sec)?使用alter table创建索引语法为:alter?table?table_name?add?[unique|fulltext|spatial]?index?index_name(property_name[length] [asc|desc]);?mysql>?create?table?index_1(id?int,?name?varchar(20),?class?int);
query ok,?0?rows?affected (0.11?sec)
mysql>?show?tables;
+----------------+
| tables_in_kiwi |
+----------------+
| index_1 |
| singlerow |
| stu |
+----------------+
3?rows?in?set?(0.00 sec)
mysql>?show?create?table?index_1 \g;
***************************?1.?row?***************************
table: index_1
create?table:?create?table?`index_1` (
`id`?int(11)?default?null,
`name`?varchar(20)?default?null,
`class`?int(11)?default?null
) engine=innodb?default?charset=latin1
1?row?in?set?(0.00 sec)
mysql>?alter?table?index_1?add?fulltext?index?index_alter (name?desc);
error?1214?(hy000): the used?table?type?doesn't support fulltext indexes
mysql>?alter?table?index_1 engine=myisam;
query ok,?0?rows?affected (0.36?sec)
records:?0?duplicates:?0?warnings:?0
mysql>?alter?table?index_1?add?fulltext?index?index_alter (name?desc);
query ok,?0?rows?affected (0.13?sec)
records:?0?duplicates:?0?warnings:?0
mysql>?show?create?table?index_1 \g;
***************************?1.?row?***************************
table: index_1
create?table:?create?table?`index_1` (
`id`?int(11)?default?null,
`name`?varchar(20)?default?null,
`class`?int(11)?default?null,
fulltext?key?`index_alter` (`name`)
) engine=myisam?default?charset=latin1
1?row?in?set?(0.00 sec)?删除索引语法:drop?index?index_name?on?table_name;mysql>?show?create?table?index_1 \g;
***************************?1.?row?***************************
table: index_1
create?table:?create?table?`index_1` (
`id`?int(11)?default?null,
`name`?varchar(20)?default?null,
`class`?int(11)?default?null,
fulltext?key?`index_alter` (`name`)
) engine=myisam?default?charset=latin1
1?row?in?set?(0.00 sec)
error:
no?query specified
mysql>?drop?index?index_alter?on?index_1;
query ok,?0?rows?affected (0.11?sec)
records:?0?duplicates:?0?warnings:?0
mysql>?show?create?table?index_1 \g;
***************************?1.?row?***************************
table: index_1
create?table:?create?table?`index_1` (
`id`?int(11)?default?null,
`name`?varchar(20)?default?null,
`class`?int(11)?default?null
) engine=myisam?default?charset=latin1
1?row?in?set?(0.00 sec)
该用户其它信息

VIP推荐

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