您的位置:首页 > 数据库 > MySQL

MySql

2016-03-16 11:10 531 查看
全文检索:将数据库存储的整篇文章或整本书中任意信息内容查询出来.InnoDB从1.2开始支持全文检索.

倒排索引:和B+索引一样,也是一种索引结构,在辅助表中存储了单词与单词所在的一个文档或多个文档位置的映射.

通常全文检索使用倒排索引实现;倒排索引通常使用关联数组实现:

①.inverted file index:{单词,单词所在文档的ID}

②.full invertedindex: {单词,(单词所在的ID,在具体文档中的位置)}

DocumentIDText
1sima daxia
2hello world daxia
3nine days
inverted file index:

Number
TextDocumentsNumberTextDocuments
1sima14world2
2daxia1,25nine3
3hello26days3
full invertedindex:存储的文档id和在文档中的位置信息

NumberText
DocumentsNumberTextDocuments
1sima(1,1)4world(2,2)
2daxia(1,2),(2,3)5nine(3,1)
3hello(2,1)6days(3,2)
倒排索引需要将word放在一张表中,称之为辅助表(Auxiliary Table),innodb共有6张辅助表.

FTS Index Cache:是一个红黑树结构,根据(word,list)进行排序,即对全文索引的更新后还存在FTS Index Cache中,

而对应的辅助表却还没有更新;innodb会批量的对辅助表进行更新.当对全文检索进行查询时,Auxiliary Table首先会将在

FTS Index Cache 中对应的word字段合并到 Auxiliary Table 中,然后再进行查询。

用户可以查看指定倒排序辅助表中分词的信息:
<span style="white-space:pre">		</span>SET GLOBAL innodb_ft_aux_table='db_name/table_name'

也可以将该句放到my.cnf[mysqld]配置文件中.查看方法:

SELECT * FROM information_schema.innodb_ft_index_table
FTS Document ID:为了支持全文检索,必须有一个列与word对应,这个列被命名为FTS_DOC_ID,类型必须是 BIG INT UNSIGNED NOT NULL;

建立倒排序索引:

create table hh(title varchar(10),content varchar(500));
create fulltext index idx_content on hh(content);
查询时:select * from hh match(content) against ("zuo");


stopword:不需要建立索引的单词,在information_schema.innodb_ft_default_stopword中有默认的36个单词,也可以通过innodb_ft_server_stopword_table来定义;

create table user_stopword(value varchar(30));
set global innodb_ft_server_stopword_table="sima/user_stopword"
全文检索通过MATCH() AGAINST()支持,MATCH指定需要被查询的列(建立索引的列),AGAINST表示需要那种方法进行查询.
查询模式:

Natural Language(default):表示查询带有指定word的文档.

select * from hh match(content) against ("zuo" in natural language mode);

该查询的结果是按相关性(非负的浮点数字,0表示没有相关性)进行排序的,相关性最高的在顶端.

查询相关性:select title,content ,match(content) against("hao" in natural language mode) as relevance from hh;

对于全文检索:需要考虑几个因素:查询的word在stopword表中,忽略该字符串的查询;

查询的word的字符长度是否在区间[innodb_ft_min_token_size,innodb_ft_max_token_size]中

Boolean模式:字符串前后有不同的含义;

+ : 表示该word必须存在

- : 表示该word必须不存在;

no operator: 表示该word是可选的;

@distance:表示查询的多个单词之间距离是否在distance之内;

> : 表示出现该单词时增加相关性;select title,content,match(content) against('>haha' in boolean mode) from hh;如果有haha,那么相关性将增加

< : 表示出现该单词时降低相关性;match(content) against('<haha' in boolean mode)

~ : 表示允许出现该单词;

* : 表示以该单词开头的单词;

" : 表示短语.

MySQL备份:mysqldump:对于备份整个数据库,加上--add-drop-database 和--databases 一起使用,这样导出的文件存在创建数据库的语句;

mysqldump --single-transaction --add-drop-database --databases sima > sima.sql :sima是一个数据库.

--hex-blob:将binary,varbinary,blog,bit列类型的备份为16进制的数;

select into [column1],[column2].. into outfile '/root/backup.txt' from table_name

此时文件所在的目录必须是mysql:mysql权限的.

恢复操作:mysql -uroot -p < sima.sql

source sima.sql

存储程序:被保存到了服务端,所以只需要创建时通过网络传递一次,以后就不再需要传递对应的语句.建议都使用BEGIN END

存储函数|存储过程|触发器|事件

语句分隔符:会与语句块中的产生冲突,因此有必要自定义语句分隔符,delimiter $|&....表示将$,&定义为语句分隔符;

delimiter $;
create procedure show_times()
begin
select 'local time is: ',current_timestamp;
select 'UTC time is: ',utc_timestamp;
end$
delimiter;
函数:可以返回值,所以可以用在表达式中

delimiter $
create function select_by_birth(p_birth int)
returns int   #声明了返回值类型,那么函数体至少要包含一条返回语句
reads sql data
begin
return(select count(*) from hh where birth=p_birth);
end$
调用时和调用內建函数一样;可以select select_by_birth(12)

存储过程:没有返回值,不能用在表达式中.

delimiter $;
create procedure show_times()
begin
select 'local time is: ',current_timestamp;
select 'UTC time is: ',utc_timestamp;
end$
delimiter;
通过call来进行调用:call show_times();

存储过程有三种类型的参数:IN ,OUT ,INOUT

IN(default) :调用者把一个参数传递给过程,过程可以对该值进行修改,但是过程执行完成之后,调用者是不能感知的.

OUT : 过程把一个值赋给OUT 参数,这个值在返回后由调用者访问.

INOUT: 允许调用者向过程传递一个值,然后返回一个值.

delimiter $
create procedure count_sex(out p_male int,out p_female int)
begin
select count(*) from program where sex = 'M' into p_male;
select count(*) from program where sex='F' into p_female;
end$
调用:call count_sex(@male_count,@female_count); //必须传递两个变量

select @male_coutn,@female;

触发器:与特定数据表关联的存储过程;当相应的数据表发生update,insert,delete时会被执行;

create trigger grigger_name [before|after] [insert|update|delete] on table_name for each row trigger_body;

before|after : 表示是在操作的前还是后触发语句

[insert|update|delete]: 表示那种操作可以触发该动作.

在触发器的语句体里面(trigger_body)中,NEW.col_name可以引用由insert|update插入或修改的新数据列.而对应的OLD.col_name

则是可以调用由delete|update修改或删除的老的数据行中的某一列.

delimiter $
create trigger sima_trigger before update on program
for each row
begin
if NEW.birth < 0 then
set NEW.birth = 0;
elseif NEW.birth > 100 then
set NEW.birth=100;
end if
end$
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: