1. 程式人生 > >SQL語句優化,索引,檢視,觸發器,儲存過程,函式等。

SQL語句優化,索引,檢視,觸發器,儲存過程,函式等。

    一,SQL優化

        主要解決海量資料操作時的全表搜尋,所以減少不必要的全表搜尋是SQL優化的主要目的,下面總結一下常用的優化有哪些:

        1,避免在where條件中使用!=或者<>,這樣會是的查詢放棄索引而進行全域性掃描

        2,避免在where條件中使用is null使用 預設值替換null,如預設值為0    

        select id from table_name where column is null; 
        select id from table_name where column = 0; 

        3,儘量在where條件,order by中建立索引進行操作

        4,儘量用exits代替in使用

        select name from a where id in (select id from b);
        select name form a where exists (select 1 from b where a.id = b.id); 
        5,避免使用or關鍵字 用union all 代替
        select name from a where id = 1 or id =2;
        select name from a where id = 1
        union all
        select name form a where id = 2;

        6,儘量不使用like "%***%" 會全表搜尋,可以用and like "***%"代替

        7,count(*), count(column), count(primary key),  count(1) 其中前兩者是一樣的後兩個效率較高

        8,避免在where條件中對欄位進行表示式操作,同樣儘量少對欄位不要使用函式操作

        select id from a where num/2 = 10;
        select id from a where num = 10*2;
        9,在使用索引欄位作為條件時,如果該索引是組合索引,那麼必須使用到該索引中的第一個欄位作為條件時才能保證系統使用該索引,否則該索引將不會被使 用,並且應儘可能的讓欄位順序與索引順序相一致。
        create index a_index on a (id, name);
        select id from a where id > 10 and order by name; 

    二,索引

索引是一張表的一個目錄,SQL查詢資料時先使用索引進行定位,這樣很大的提高查詢效率,索引一般使用在where條件中,索引分為五種

        1,主鍵索引

            每個表預設有一個主鍵索引作用在表的主鍵上面,

            a. 建立索引

       create table table_name (
           id int auto_increment primary key, -- 有一個預設索引作用在主鍵上
           name varchar(20)
       )
       或者
       create table table_name (
           id int auto_increment,
           name varchar(20)
       );
       alter table_name add primary key(id); --設定主鍵 預設建立一個主鍵索引

            b. 使用索引

       select name from table_name where id = 10;

        2,唯一索引

            唯一索引保證列唯一

            a. 建立索引

       create table table_name (
           id int auto_increment primary key, -- 有一個預設索引作用在主鍵上
           name varchar(20) not null,
           email varchar(20) not null unique
       );
       -- 在eamil列上建立唯一索引
       create unique index t_email_index on table_name (email);

            b. 使用索引

       select id from table_name where email = '[email protected]';

        3,普通索引

            a. 建立索引

       create table table_name (
           id int auto_increment primary key, -- 有一個預設索引作用在主鍵上
           name varchar(20) not null,
           email varchar(20) not null unique
       );
       -- 在name列上建立索引
       create index t_email_index on table_name (name);

            b. 使用索引

       select id, email from table_name where name like '張%';
       -- 注意 前置%是不使用索引的
       select id, email from table_name where name like '%三'

        4,組合索引

            組合索引是在一個表的多列上面建立索引

            a. 建立索引

       create index index_name on table_name (name, email); -- 在name,email上面建立索引 (組合唯一)

            b. 使用索引

       select id from table_name where name = '張三' and eamil = '[email protected],com';  

            注意:組合索引,靠後面的列不能單獨使用索引, 如一下使用索引無效

       select id from table_name where email = '[email protected]';

        5,全文索引

        檢視,刪除索引都是通用的語句

         a. 檢視索引

     show index from table_name;    

         b. 刪除索引

     drop index index_name on table_name;

    三,檢視

檢視實際是一個虛擬表,和普通表的功能一樣,通常是對多表查詢資料的一個封裝,這樣在使用時只需要從檢視中取即可。

        1. 建立檢視

    create view view_name as
    select a.id, a.name, a.email, b.num from table_name a 
    left join table_name1 b 
    on a.id = b.a_id;

    2. 使用檢視

    select * from view_name;

  3. 檢視檢視

            a. 檢視資料庫中所有檢視            

       show table status where comment = 'view';

            b. 檢視某一個檢視

       show create view view_name;
       或者
       describe view_name;

        4. 刪除檢視

       drop view view_name;

        5. 修改檢視(建議刪除重建)

       alter view view_name as sql;

        注意:

    // 檢視不可以建立索引,也不能關聯觸發器和預設值    // 檢視可以使用order by    // 修改檢視也是對錶的資料的修改,刪除檢視時不會刪除表內的資料    // 檢視支援巢狀,也就是可以把根據檢視檢索出來的東西來建立新的檢視

    四,觸發器

觸發器就是在一個操作執行後/前,觸發另一個操作 保證資料完整性。

        a. 建立觸發器sql格式

    create trigger trigger_name after/before insert/delete/update on table_name for each row
    begin
      sql 
    end

        b. 檢視觸發器

    show triggers;

        c. 刪除觸發器

    drop trigger trigger_name;

  練習:隨便建立的表用於測試觸發器

show tables;
show triggers;

drop table student;
drop table stu_class;
drop trigger stu_class_insert;

-- 觸發器的使用
-- 目的:學生選課標的變動 更新學生表中的num欄位
CREATE TABLE student (
    id bigint(11) PRIMARY KEY comment '學號',
    name VARCHAR(20),
    class_num INT COMMENT '學生選課數'
) COMMENT='學生表';
CREATE TABLE stu_class (
    id bigint(11) PRIMARY KEY,
    name VARCHAR(20),
    stu_id bigint(11) COMMENT '學生表id'
) comment = '學生選課表';

insert into student values(10011, '張三', 0);
insert into student values(10012, '李四', 0);

select * from student;
select * from stu_class;

-- 學生選課後學生選課數自動+1、
DELIMITER $
create trigger stu_class_insert 
after insert on stu_class
for each row
begin
    update student set class_num = +1 where id = new.stu_id;
end$
DELIMITER $

insert into stu_class values(1, '課程一', 10011);

最後結果:

        

    五,儲存過程

儲存過程:我們知道簡單的SQL語句是不能進行復雜的邏輯盤判斷和操作的,儲存過程類似於函式(方法),簡單的說儲存過程是為了完成某個資料庫中的特定功能而編寫的語句集合,該語句集包括SQL語句(對資料的增刪改查)、條件語句和迴圈語句等。

use test;

-- 檢視現有的儲存過程
select name from mysql.proc where db = 'test' and type='procedure'

-- 建立儲存過程
DELIMITER $$
create procedure pro_name (in stuId bigint)
begin
	select * from stu_class where stu_id = stuId;
end$$
DELIMITER $$

-- 檢視儲存過程的定義語句
show create procedure pro_name;

-- 呼叫儲存過程
call pro_name(10012);

-- 刪除儲存過程
drop procedure pro_name;

  儲存過程優點:

            1,儲存過程增強了SQL語句的靈活性,支援複雜的邏輯判斷和控制語句,運算能力。

            2,儲存過程只在建立時進行編譯,在之後呼叫不會再進行編譯,加快了操作效率,而傳統的SQL每次都會進行編譯。

            3,支援系統管理員分配許可權,只有授權後的使用者才有許可權訪問,增強了資料的安全性。

    六,函式

-- 顯示所有函式
select name from mysql.proc where db = 'test' and type='function'

-- 建立函式
DELIMITER $$
create function fun_name(a int, b int)
returns int
begin
	declare sum int default 0;
    set sum = a + b;
	return sum;
end$$
DELIMITER $$

-- 檢視指定函式定義語句
show create function fun_name;

-- 刪除函式
drop function fun_name;

-- 呼叫函式
select fun_name(1, 2);