MySQL資料庫中的索引

2021-10-05 08:32:18 字數 2251 閱讀 2838

建表時建立

create table mytable (

`name` varchar(32) ,

index index_mytable_name (`name`)

);

建表後,直接建立索引

create index index_mytable_name on mytable(name);
修改表結構

alter table mytable add index index_mytable_name (name);
建表的時候一起建立

create table mytable (

`name` varchar(32) ,

unique index_unique_mytable_name (`name`)

);

建表後,直接建立索引

create unique index index_mytable_name on mytable(name);
修改表結構

alter table mytable add unique index index_mytable_name (name);
建表的時候一起建立

create table mytable (

`id` int(11) not null auto_increment ,

`name` varchar(32) ,primary key (`id`)

);

修改表結構

alter table test.t1 add constraint t1_pk primary key (id);
建表的時候一起建立

create table mytable (

`id` int(11) ,

`name` varchar(32) ,index index_mytable_id_name (`id`,`name`)

);

建表後,直接建立索引

create index index_mytable_id_name on mytable(id,name);
修改表結構

alter table mytable add index index_mytable_id_name (id,name);
主要用來查詢文字中的關鍵字,而不是直接與索引中的值相比較。

fulltext索引跟其它索引大不相同,它更像是乙個搜尋引擎,而不是簡單的where語句的引數匹配。

fulltext索引配合match against操作使用,而不是一般的where語句加like。

它可以在create table,alter table ,create index使用,不過目前只有char、varchar,text 列上可以建立全文索引。

建立方法:

建表的時候一起建立

create table `article` (

`id` int(11) not null auto_increment ,

`title` char(250) not null ,

`contents` text null ,`create_at` int(10) null default null ,

primary key (`id`),fulltext (contents)

);

建表後,直接建立索引

create fulltext index index_article_contents on article(contents);
修改表結構

alter table article add fulltext index index_article_contents (contents);
使用全文索引:

select * from `表名` where match (`列名`,`可以一列可以多列`) against ('+文章裡應該包含的字串 -文章裡不應該包含的字串' in boolean mode);	-- boolean mode 可以用 + - 表示關鍵字必須包含或必須不包含
其他索引就正常select的時候,有索引就會自動用索引。

資料庫mysql索引 資料庫 mysql索引

mysql 索引 mysql索引的建立對於mysql的高效執行是很重要的,索引可以大大提高mysql的檢索速度。打個比方,如果合理的設計且使用索引的mysql是一輛蘭博基尼的話,那麼沒有設計和使用索引的mysql就是乙個人力三輪車。索引分單列索引和組合索引。單列索引,即乙個索引只包含單個列,乙個表可...

mysql資料庫中的索引

一 什麼是索引?索引用來快速地尋找那些具有特定值的記錄,所有mysql索引都以b 樹的形式儲存。如果沒有索引,執行查詢時mysql必須從第乙個記錄開始掃瞄整個表的所有記錄,直至找到符合要求的記錄。表裡面的記錄數量越多,這個操作的代價就越高。如果作為搜尋條件的列上已經建立了索引,mysql無需掃瞄任何...

MySQL資料庫中的索引

在mysql中索引 索引也是一張表,該錶儲存了主鍵與索引字段,並指向實體表的記錄。建立索引的原則 1 查詢頻率高,資料量大表 2查詢條件 where 後面的字段 3 使用唯一索引,區分度高 4 使用短的字段 5 使用最左字首 btree b 樹 innodb預設引擎使用的索引結構 hash r tr...