多表的查詢,建立索引


1,多表連接查詢

語法:select 表1 inner/left/right/ join 表2 on 表1.字段 =表2.字段;

1.1>外鏈接之內鏈接.

from department inner join employee onemployee.dep_id = department.id === from department, employee where employee.dep_id = department.id

 1.2>外鏈接之左連接

mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id
#這里是把department_name重命名為depart_name

 

1.3>外鏈接之右連接

select employee.id,employee.name,department.name
as depart_name from employee right join department
on employee.dep_id = department.id;

1.4>全外鏈接(顯示左右兩個表匹配成功的全部記錄)

在內連接的基礎上增加左邊和右邊沒有的結果,在mysql中是沒有着個指令的,只是一種外在的一種巧妙的方法

語法:

select * from employee left join department on employee.dep_id = department.id
          union
        select * from employee right join department on employee.dep_id = department.id
           ;

2,符合條件查詢

select employee.name,department.name from employee inner join department
  on employee.dep_id = department.id
  where age > 25;

select employee.id,employee.name,employee.age,department.name from employee,department
    where employee.dep_id = department.id
    and age > 25
    order by age asc;

3,子查詢

3.1>帶in的關鍵字查詢

 

#1,子查詢是將一個查詢語句嵌套在另一個查詢語句中,
#2,內層查詢語句的查詢結果,可以作為外層查詢語句提供查詢條件.
#3,子查詢中可以包含:in,not,in,any,all,exists 和 not existsde 等關鍵字
#4,還可以包括比較運算符:=,!=,,>,<等
#查詢平均年齡大於25歲以上的部門名字
select id,name from department
    where id in 
        (select dep_id from employee group by dep_id having avg(age) > 25);

#查看計數部員工姓名
select name from employee where dep_id in
(select id from department where name = "技術");

#查看不足1人的部門名
select name from department where id not in
(select dep_id from employee group by dep_id);

3.2>帶比較運算符的子查詢

##查詢大於部門內平均年齡的員工名、年齡
select name,age from employee where age > 
(select avg(age) from employee);

3.3>帶exists關鍵字的子查詢

select * from employee where exists
(select id from department where id= 200);

 4,索引

4.1>索引的介紹

#數據庫中專門用於幫助用戶快速查找數據的一種數據結構 .類似於字典中的目錄,查找字典內容時可以根據目錄查找到數據存放的位置,然后直接獲取內容    

4.2>索引的作用

約束數據並且快速查找數據

4.3>常見的幾種索引

-普通索引
-唯一索引

-主鍵索引

-聯合索引(多列 )

    
    -聯合主鍵索引

    -聯合唯一索引

    -聯合普通索引

4.3.1>無索引和有索引的區別以及建立的目的

#無索引:從前往后,一條一條的查詢
#有索引:創建索引的本質,就是創建額外的文件 (是某種格式的儲存,查詢的時候,先去格外的文件找,定好位置,然后再去原始表中直接查詢,但是創建索引越多 ,對硬盤消耗越大,空間換時間)
#建立索引的目的:
#a,額外的晚間保存特殊的數據結構
#b,查詢快 ,但是插入更新刪除依然慢
#c,創建索引后 必須命中 索引才有用

4.3.2>索引的種類

hash索引和Btree
(1)hash索引:查詢倒要塊,范圍比較慢,(不適合范圍查詢)
(2)btree索引:b+樹,層級越多,數據量指數級增長(innoDB默認支差這個)

4.4>索引的介紹

4.4.1>普通索引

#創建表 及普通索引
create table userinfo( nid int not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, index ix_name(name) );

普通索引的創建

  create index 索引的名字 on 表明(列名)

查看索引:

  show  index from 表名

刪除索引 :

  drop index 索引名 on 表名

4.4.2>唯一索引:加速查找和唯一約束

 

#創建 表及唯一索引
create table userinfo(
    id int not null auto_increment primary key,
    name varchar(32) not null,
    email varchar(64) not null,
    unique index ix_name(name)
);

創建唯一索引:

create unique index 索引名 on 表名(列名)

刪除唯一索引:

drop index 索引名 on 表名

4.4.3>主題索引:功能:加速查找和唯一約束

 create table userinfo(

                   id int not null auto_increment primary key,
                   name varchar(32) not null,
                   email varchar(64) not null,
                   unique  index  ix_name(name)
           )
          or

           create table userinfo(

                   id int not null auto_increment,
                   name varchar(32) not null,
                   email varchar(64) not null,
                   primary key(nid),
                   unique  index  ix_name(name)
         )

主鍵索引:

alter table 表名 add  primary key (列名)

刪除主鍵索引

alter  table 表名 drop primary key;

alter  table 表名 modify 列名 int, drop  primary  key

4.4.4>組合索引:組合索引是將n個列組合成一個索引

其應用場景為:頻繁的使用n列來進行查詢,如: where name = "alex" and "email" = "alex@qq.com".

聯合普通索引的創建:

create index 索引名 on 表名(列名1,列名2);

5,索引名詞 

 

覆蓋索引:在索引文件中直接獲取數據
    列如:
        select name from userinfo  where name = "alex50000";
索引合並 :把多個 單索引合並使用
    列如:
        select * from userinfo  where name ="alex13131" and id = 13131

 

6,正確使用索引

  數據庫中添加索引后確實會讓查詢速度起飛,但前提必須使用索引來命名的字段來查詢,如果不用,那么你給某一字段添加的字段的索引頁毫無意義.

使用索引的三要素:(1)創建索引  (2)命中索引  (3)正確使用索引

#創建表
create table userinfo(
    id int ,
    name varchar(20),
    gender char(6),
    email varchar(50)
);
#插入數據
delimiter $$ #聲明存儲存儲過程的結束符好為$$
create procedure auto_insert()
begin
    declare i int default 1;
    while(i<3000000)double
        insert into userinfo values(i,concat("alex",i),"male",concat("xuexue",i,"@qq.com"));
        set i = i + 1;
    end while;
end   #$$結束
delimiter; #重新聲明分號為結束符號

插入數據需要一定的時間,我沒有試出來...

查看存儲過程:show create procedure auto_insert1\g

調用存儲過程:call auto_insert1();

7,索引的最左前綴的特點:

#最左前綴的匹配:
create index in_name_email on userinfo(name,email);
    select * from userinfo where name = "xuexue";
    select * from userinfo where name = "xuexue" and  email="xuexue@qq.com";
    select * from userinfo where email = "xuexue@qq.com";
#組合使用組合索引,name和email組合索引查詢:
(1)name和email ----->使用索引
(2)name ----->使用索引
(3)email ----->不使用索引
對於同時搜索n個條件時,組合索引的性能好於單個索引
***********組合索引的性能>索引合並的性能********

8,索引的注意事項

(1)避免使用select * 這樣的模糊查詢
(2)count(1)或count(列)代替count(*)
(3)創建表時盡量使用插入代替varchar
(4)表的字段順序固定長度的字段優先
(5)組合索引代替多個單列索引(經常使用多個條件查詢)
(6)使用連接(join)代替子查詢
(7)聯表時注意條件類型需要一致
(8)盡量使用短索引
(9)索引散列(重復少)不適用於建立索引,列如:性別不適

9,分頁性能

第一頁:
select * from userinfo  limit 0,10;
第二頁:
select * from userinfo  limit 10,10;
第三頁:
select * from userinfo  limit 20,10;
第四頁:
select * from userinfo  limit 30,10;
...

性能優化

(1)只有上一頁和下一頁
    做一個記錄:記錄當前頁面的最大id或最小id
    下一頁:
    select  * from userinfo where id>max_id limit 10;
    上一頁:
    select  * from userinfo where id<min_id order by id desc limit 10;
(2)中間有頁碼的情況下
    select * from  userinfo where id  in(
                select id from (select * from userinfo where id>pre_max_id limit  (cur_max_id-pre_max_id)*10) as A order by A.id desc limit 10);    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM