mysql11---主键普通全文索引

Posted 672530440

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql11---主键普通全文索引相关的知识,希望对你有一定的参考价值。

1.1主键索引添加
当一张表,把某个列设为主键的时候,则该列就是主键索引
create table aaa
(id int unsigned primary key auto_increment ,
name varchar(32) not null defaul ‘’);
这是id 列就自动是主键索引.
如果你创建表时,没有指定主键索引,也可以在创建表后,在添加, 指令:
alter table 表名 add primary key (列名);
举例: 
create table bbb (id int , name varchar(32) not null default ‘’);
alter table bbb add primary key (id);
1.2普通索引
一般来说,普通索引的创建,是先创建表,然后在创建普通索引
比如:
create table ccc(
id int unsigned,
name varchar(32)
)
create index 索引名 on 表 (列1,列名2);
1.3创建全文索引
全文索引,主要是针对对文件,文本的检索, 比如文章, 全文索引针对MyISAM有用.
创建 :
CREATE TABLE articles (
       id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
       title VARCHAR(200),
       body TEXT,
       FULLTEXT (title,body)                 #title和body的复合全文索引
     )engine=myisam charset utf8;

INSERT INTO articles (title,body) VALUES
     (mysql Tutorial,DBMS stands for DataBase ...),
     (How To Use MySQL Well,After you went through a ...),
     (Optimizing MySQL,In this tutorial we will show ...),
     (1001 MySQL Tricks,1. Never run mysqld as root. 2. ...),
     (MySQL vs. YourSQL,In the following database comparison ...),
     (MySQL Security,When configured properly, MySQL ...);

如何使用全文索引:
错误用法:
select * from articles where body like%mysql%’; 【不会使用到全文索引】
证明:
explain  select * from articles where body like%mysql%+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | articles | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    6 |    16.67 | Using where |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------+-------------+
正确的用法是:
select * from articles where match(title,body) against(‘database’); 【可以,possible_keys,key+----+-------------+----------+------------+----------+---------------+-------+---------+-------+------+----------+-------------+
| id | select_type | table    | partitions | type     | possible_keys | key   | key_len | ref   | rows | filtered | Extra       |
+----+-------------+----------+------------+----------+---------------+-------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | articles | NULL       | fulltext | title         | title | 0       | const |    1 |      100 | Using where |
+----+-------------+----------+------------+----------+---------------+-------+---------+-------+------+----------+-------------+
mysql> select * from articles where match(title,body) against(database);
+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |
+----+-------------------+------------------------------------------+
? 说明:
1.在mysql中fulltext 索引只针对 myisam生效
2.mysql自己提供的fulltext针对英文生效----->sphinx (coreseek) 技术专门处理中文全文索引
3.使用方法是 match(字段名..) against(关键字)

4.全文索引一个 叫 停止词,因为在一个文本中,创建索引是一个无穷大的数,因此,对一些常用词和字符,就不会创建(常用词你我他太多了,即使查询出来也没有多大意思和匹配度),这些词,称为停止词.全文里面建索引是很困难的,如果把每一个字都建成索引难度很高,因为字符太多了,所以只会针对生僻词(关键词)建索引,否则每个词都建索引,量就很大了,
mysql> select  match(title,body) against(database) from articles ;
+--------------------------------------------------------+
| match(title,body) against(database) |
+--------------------------------------------------------+
|                    0.6554583311080933 |  (第一条记录里面可能匹配到database的概率是0.65545833)
|                                     0 |
|                                     0 |
|                                     0 |
|                    0.6626645922660828 |
|                                     0 |
+--------------------------------------------------------+
mysql> select  match(title,body) against(da) from articles ;
+---------------------------------+
| match(title,body) against(da) |
+---------------------------------+
|                               0 |(da就没有建索引)
|                               0 |
|                               0 |
|                               0 |
|                               0 |
|                               0 |
+---------------------------------+

 

以上是关于mysql11---主键普通全文索引的主要内容,如果未能解决你的问题,请参考以下文章

mysql主键普通索引唯一索引和全文索引的比较

mysql索引知识点总结

MySQL 索引

MySQL索引分类

「进阶」MySQL中如何使用索引

mysql创建全文索引