select * from emp where ename="研发部";---测试案例命令如下 (最好以 select * from emp e,dept d where e.empno=123451 )
ALTER TABLE emp ADD PRIMARY KEY(empno);*删除主键
alter table emp drop primary key;索引的原理说明
哪些列上适合添加索引
1、较频繁的作为查询条件字段应该创建索引
select * from emp where empno = 1;2、唯一性太差的字段不适合单独创建索引,即使频繁作为查询条件
select * from emp where sex = "男"3、更新非常频繁的字段不适合创建索引
select * from emp where logincount = 14、不会出现在WHERE子句中的字段不该创建索引
create table news(id int , title varchar(32),con varchar(1024)) engine=MyISAM;2.建立全文索引
create fulltext index ful_inx on news (con);3.插入数据
"but it often happens that they are not above supporting themselves by dishonest means.which should be more disreputable.Cultivate poverty like a garden herb"4.看看匹配度
mysql> select match(con) against("poverty") from news;+-------------------------------+| match(con) against("poverty") |+-------------------------------+| 0 || 0 || 0 || 0.9853024482727051 |+------------------------------+0表示没有匹配到,或者你的词是停止词,是不会建立索引的.
create index 索引名 on 表名(列1,列2);索引的使用
create [UNIQUE|FULLTEXT] index index_name on tbl_name (col_name [(length)] [ASC | DESC] , …..);alter table table_name ADD INDEX [index_name] (index_col_name,...) 添加主键(索引) ALTER TABLE 表名 ADD PRIMARY KEY(列名,..); 联合主键删除索引
DROP INDEX index_name ON tbl_name;alter table table_name drop index index_name; 删除主键(索引)比较特别: alter table t_b drop primary key;查询索引(均可)
show index(es) from table_name;show keys from table_name;desc table_Name;修改索引,我们一般是先删除在重新创建.
CREATE TABLE dept(deptno MEDIUMINT UNSIGNED NOT NULL DEFAULT 0,dname VARCHAR(20) NOT NULL DEFAULT "",loc VARCHAR(13) NOT NULL DEFAULT "") ENGINE=MyISAM DEFAULT CHARSET=utf8 ; --放入数据,前面应该已经添加了,如果没有则需要重新添加--测试开始.添加一个主键索引
alter table dept add primary key (deptno)--测试语句
explain select * from dept where deptno=1;结果是:
mysql> explain select * from dept where deptno=1;*************************** 1. row ***************************id: 1select_type: SIMPLEtable: depttype: constpossible_keys: PRIMARYkey: PRIMARYkey_len: 3ref: constrows: 1Extra:1 row in set (0.00 sec)--创建多列索引
alter table dept add index myind (dname,loc);--证明对于创建的多列索引,只要查询条件使用了最左边的列,索引一般就会被使用
explain select * from dept where dname="研发部"; 会显示使用到了索引myindexplain select * from dept where loc="MsBDpMRX"; 不会显示使用到了索引myind
explain select * from dept where dname like "%研发部"; 不会显示使用到了索引myindexplain select * from dept where dname like "研发部%"; 会显示使用到了索引myind--如果条件中有or,即使其中有条件带索引也不会使用
alter table dept drop index myindalter table dept add index myind (dname)explain select * from dept where dname="研发部" or loc="aa";-- 就不会使用到dname列上的--如果列类型是字符串,那一定要在条件中将数据使用引号引用起来。否则不使用索引
select * from dept from dname=1234; //不会使用到索引select * from dept from dname="1234"; //会使用到索引查看索引的使用情况
show status like "Handler_read%";大家可以注意:
alter table table_name disable keys;loading data//insert语句;alter table table_name enable keys;对于Innodb:
select * from 表名 where 条件1="" or 条件2="tt"explaine select * from dept group by dname; =>这时显示 extra: using filesort 说明会进行排序explaine select * from dept group by dname order by null =>这时不含有显示 extra: using filesort 说明不会进行排序***有些情况下,可以使用连接来替代子查询。因为使用join,MySQL不需要在内存中创建临时表
explain select * from emp , dept where emp.deptno=dept.deptno;
explain select * from emp left join dept on emp.deptno=dept.deptno;
create table bbs(id int ,con varchar(1024) , pub_time int);date("Ymd",时间-3*24*60*60); 2038年-1-19对于使用浮点数和定点数的案例说明
create table temp1( t1 float(10,2), t2 decimal(10,2));insert into temp1 values(1000000.32,1000000,32); 发现 t1 成了 1000000.31 所以有问题.
create table temp2( id int) engine=MyISAM;insert into temp2 values(1); insert into temp2 values(2); insert into temp2 values(3);insert into temp2 select * from temp2;--复制delete from temp2 where id=1; 发现 该表对于的数据文件没有变小定期执行 optimize table temp2 发现表大小变化,碎片整理完毕