mysql day12 索引

explain select customer_id from customers where state = 'CA';
select count(*) from customers;
create index idx_state on customers (state);-- 减少查询次数a

explain select customer_id from customers where points >= 1000;
create index idx_points on customers (points);

show indexes in customers;
analyze table customers; -- 更精确的索引PRIMARY

create index idx_lastname on customers (last_name(20));
select 
	count(distinct left(last_name,1)),-- 25,姓氏的首字母,count(left(last_name,1))有重复值
	count(distinct left(last_name,5)),-- 966 
    count(distinct left(last_name,10)) -- 996
from customers;
-- 选择这个表的记录数

-- 全文索引,通过某个名词查询相关内容
use sql_blog;
select *
from posts
where title like '%react redux%' or
body like '%react redux%'; -- 包含'%react redux%'的标题或正文/顺序一致

create fulltext index idx_title_body on posts (title,body);

select *, match(title,body) against('react redux') -- 相关性得分
from posts
-- where match(title,body) against('react redux'); -- 可以找到按任意顺序排列的'%react redux%'
-- where match(title,body) against('react -redux' in boolean mode);-- 得到包含react但不含redux
-- where match(title,body) against('react -redux +form' in boolean mode); -- 包含form
where match(title,body) against('"handling a form"' in boolean mode);-- ""返回标题或正文包含handling a form

-- 复合索引
use sql_store;
show indexes in customers;
create index idx_state_points on customers (state,points);-- 复合索引,通过变量查找 
explain select customer_id from customers -- 两个可能键索引idx_state,idx_points,最终用idx_state/一个索引,必须扫描所有CA的顾客再查看他们的积分
where state = 'CA' and points > 1000;

drop index idx_points on customers;-- 删除索引

-- 复合索引中的顺序
explain select customer_id
from customers
-- use index (idx_lastname_state) -- 40,use index (键名)
use index (idx_state_lastname)
where state like 'A' and last_name like 'A%';

drop index idx_lastname_state on customers;


select 
	count(distinct state),-- 48
	count(distinct last_name)-- 996
from customers;

create index idx_lastname_state on customers(last_name,state); -- 40,先找到姓氏为A的/很多条记录,再找到加州
create index idx_state_lastname on customers(state,last_name); -- 7,先找到州,再找到姓氏

-- 当索引无效
-- 1
explain select customer_id from customers -- 1010
where state = 'CA' or points > 1000;

create index idx_points on customers (points);-- 640
explain
	select customer_id from customers
	where state = 'CA'
	union 
	select points from customers
	where points > 1000;
-- 2
explain select customer_id from customers
where points+10 > 2010; -- 1010,在表达式points+10中用了列就无法以最优的方式搜索

explain select customer_id from customers
where points > 2000; -- 336

-- 使用索引排序
explain select customer_id from customers -- order by state-- 1010,按顺序读取并扫描整个索引

-- order by first_name; -- Using filesort,外部排序,费时
-- show status like 'last_query_cost'; -- 查询成本1112

-- order by state;
-- show status like 'last_query_cost';-- 查询成本102

-- order by state, points desc;-- (a,b) 在中间加一列会导致全表扫描
-- show status like 'last_query_cost';-- 查询成本1112,出现Using filesort,先用州然后按积分升序排列

-- order by points;-- 出现Using filesort
-- show status like 'last_query_cost';-- 查询成本1112

where state = 'CA' -- 缩小范围到某个州
order by points;
show status like 'last_query_cost'; -- 查询成本11,Using where; Using index,没有出现Using filesort

-- (a,b)
-- a
-- a,b
-- a desc,b desc

-- 覆盖索引:包含所有满足查询需要的数据的索引,包含where,order by,select的列

-- 
create user qss@127.0.0.1;-- 本机ip地址
create user qss identified by '1234'; -- 密码

select * from mysql.user;-- 获取用户列表

drop user qss;

set password for qss = '1234';
set password = '1234';-- 给当前登陆的用户设置密码

-- 授予权限
-- 1.web/desktop application
create user moon_app identified by '1234';
grant select, insert, update, delete, execute -- 授予权限
on sql_store.* -- 指定数据库,sql_store.*里的所有表,.customers指定这个表
to moon_app; -- 指定用户,如果这个用户账号有主机,ip地址和域名都得写

-- 2.admin
grant all
-- on sql_store.*; -- sql_store里的所有表
on *.* -- 所有数据库中的所有表
to qss;

-- 查看权限
show grants for qss;
-- 或通过导航板


    

Guess you like

Origin blog.csdn.net/weixin_47776194/article/details/121314981