--------------------------------------------------------------------注:如果你對python感興趣,我這有個學習Python基地,里面有很多學習資料,感興趣的+Q群:895817687--------------------------------------------------------------------- 存儲過程與事務使用的舉例delimiter //
create procedure p5(-- 創(chuàng)建存儲過程p5out p_return_code tinyint
)
begin-- 這里表示如果捕獲到異常,則執(zhí)行下面set p_return_code =1并且回滾操作declare exit handler for sqlexceptionbegin-- ERRORset p_return_code =1;rollback;end;-- 這里表示如果捕獲sql警告,則執(zhí)行下面set p_return_code =2并且回滾操作declare exit handler for sqlwarningbegin-- WARNINGSset p_return_code =2;rollback;end;start transaction;# 開始啟用事務update user set balance=900 where id=1;update user set balance=1010 where id=2;update user set balance=1090 where id=3;commit;# 如果沒有出現異?;蛘呔婢蜁^續(xù)執(zhí)行提交語句-- successset p_return_code =0;-- 代表執(zhí)行成功
end //
delimiter ;
# if條件語句
delimiter //
CREATE PROCEDURE proc_if ()
BEGINdeclare i int default 0;if i = 1 THENSELECT 1;ELSEIF i = 2 THENSELECT 2;ELSESELECT 7;END IF;END //
delimiter ;
# while循環(huán)
delimiter //
CREATE PROCEDURE proc_while ()
BEGINDECLARE num INT ;SET num =0;WHILE num <10 DOSELECTnum ;SET num = num +1;END WHILE ;END //
delimiter ;
-- 覆蓋索引
select name from user where name ='sgt';
此語句叫做覆蓋索引
只在輔助索引的葉子節(jié)點中就找到了我們想要的數據記錄-- 非覆蓋索引
select age from user where name ='sgt';
此語句叫非覆蓋索引,雖然查詢的時候用的是name索引字段,但是要查詢的是age字段。
以下是了解內容,有興趣可以測試下
索引的測試
#1. 準備表
create table s1(idint,
name varchar(20),
gender char(6),
email varchar(50));#2. 創(chuàng)建存儲過程,實現批量插入記錄
delimiter $$ #聲明存儲過程的結束符號為$$
create procedure auto_insert1()
BEGINdeclare i int default 1;while(i<3000000)doinsert into s1 values(i,'jason','male',concat('jason',i,'@oldboy'));set i=i+1;end while;
END$$ #$$結束
delimiter ;#重新聲明 分號為結束符號#3. 查看存儲過程
show create procedure auto_insert1\G #4. 調用存儲過程
call auto_insert1();# 表沒有任何索引的情況下
select *from s1 where id=30000;# 避免打印帶來的時間損耗
select count(id)from s1 where id=30000;
select count(id)from s1 where id=1;# 給id做一個主鍵
alter table s1 add primary key(id);# 速度很慢select count(id)from s1 where id=1;# 速度相較于未建索引之前兩者差著數量級
select count(id)from s1 where name ='jason'# 速度仍然很慢"""
范圍問題
"""# 并不是加了索引,以后查詢的時候按照這個字段速度就一定快
select count(id)from s1 where id>1;# 速度相較于id = 1慢了很多
select count(id)from s1 where id>1andid<3;
select count(id)from s1 where id>1andid<10000;
select count(id)from s1 where id!=3;alter table s1 drop primary key;# 刪除主鍵 單獨再來研究name字段
select count(id)from s1 where name ='jason';# 又慢了create index idx_name on s1(name);# 給s1表的name字段創(chuàng)建索引
select count(id)from s1 where name ='jason'# 仍然很慢!!!"""
再來看b+樹的原理,數據需要區(qū)分度比較高,而我們這張表全是jason,根本無法區(qū)分
那這個樹其實就建成了“一根棍子”
"""
select count(id)from s1 where name ='xxx';# 這個會很快,我就是一根棍,第一個不匹配直接不需要再往下走了
select count(id)from s1 where name like 'xxx';
select count(id)from s1 where name like 'xxx%';
select count(id)from s1 where name like '%xxx';# 慢 最左匹配特性# 區(qū)分度低的字段不能建索引
drop index idx_name on s1;# 給id字段建普通的索引
create index idx_id on s1(id);
select count(id)from s1 where id=3;# 快了
select count(id)from s1 where id*12=3;# 慢了 索引的字段一定不要參與計算drop index idx_id on s1;
select count(id)from s1 where name='jason'and gender ='male'andid=3and email ='xxx';# 針對上面這種連續(xù)多個and的操作,mysql會從左到右先找區(qū)分度比較高的索引字段,先將整體范圍降下來再去比較其他條件
create index idx_name on s1(name);
select count(id)from s1 where name='jason'and gender ='male'andid=3and email ='xxx';# 并沒有加速drop index idx_name on s1;# 給name,gender這種區(qū)分度不高的字段加上索引并不難加快查詢速度create index idx_id on s1(id);
select count(id)from s1 where name='jason'and gender ='male'andid=3and email ='xxx';# 快了 先通過id已經講數據快速鎖定成了一條了
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 慢了 基于id查出來的數據仍然很多,然后還要去比較其他字段drop index idx_id on s1create index idx_email on s1(email);
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 快 通過email字段一劍封喉
聯合索引
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 如果上述四個字段區(qū)分度都很高,那給誰建都能加速查詢# 給email加然而不用email字段
select count(id)from s1 where name='jason'and gender ='male'andid>3;# 給name加然而不用name字段
select count(id)from s1 where gender ='male'andid>3;# 給gender加然而不用gender字段
select count(id)from s1 where id>3;# 帶來的問題是所有的字段都建了索引然而都沒有用到,還需要花費四次建立的時間
create index idx_all on s1(email,name,gender,id);# 最左匹配原則,區(qū)分度高的往左放
select count(id)from s1 where name='jason'and gender ='male'andid>3and email ='xxx';# 速度變快