MySql索引優化案例

2021-10-01 15:56:06 字數 3950 閱讀 5470

-- 建立新錶

create table article(

id int unsigned not null primary key auto_increment,

author_id int unsigned not null,

category_id int unsigned not null,

views int unsigned not null,

comments int unsigned not null,

title varchar(255) not null,

content text not null

)character set utf8 collate utf8_general_ci;

--插入資料

insert into article(`author_id`,`category_id`,`views`,`comments`,`title`,`content`) values

(1,1,1,1,'1','1'),

(2,2,2,2,'2','2'),

(1,1,3,3,'3','3');

--查詢category_id為1且comments大於1的情況下,views最多的article_id

explain select * from article where category_id=1 and comments>1 order by views desc limit 1;

[外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳(img-vq5nea3u-1577071571703)(1d74cfa08a514d56904670b06b6d5421)]

type 為 all,且出現檔案內排序(using filesort),也沒有索引(沒建)

-- 建立索引

create index idx_article_views on article(`views`);

-- 再分析

explain select * from article where category_id=1 and comments>1 order by views desc limit 1;

*************************** 1. row ***************************

id: 1

select_type: ******

table: article

partitions: null

type: index

possible_keys: null

key: idx_article_views

key_len: 4

ref: null

rows: 1

filtered: 33.33

extra: using where; backward index scan

1 row in set, 1 warning (0.00 sec)

-- 刪除索引

drop index idx_article_views on article;

backward index scan 是反向掃瞄,反向掃瞄的效能也是可以的

-- 建表

create table class(

id int unsigned not null primary key auto_increment,

card int unsigned not null

)character set utf8 collate utf8_general_ci;

create table book(

bookid int unsigned not null auto_increment primary key,

card int unsigned not null

)character set utf8 collate utf8_general_ci;

-- 向兩個表中插入隨機資料,各插入20行資料

insert into class(card) values(round(rand()*100));

insert into book(`card`) values(round(rand()*100));

explain select * from class inner join book on class.card=book.card;

*************************** 1. row ***************************

id: 1

select_type: ******

table: class

partitions: null

type: all

possible_keys: null

key: null

key_len: null

ref: null

rows: 20

filtered: 100.00

extra: null

*************************** 2. row ***************************

id: 1

select_type: ******

table: book

partitions: null

type: all

possible_keys: null

key: null

key_len: null

ref: null

rows: 20

filtered: 10.00

extra: using where; using join buffer (block nested loop)

rand()函式產生的是0到1之間的隨機數

round()表示向下取證

-- 建立索引

create index idx_cc on class(card);

explain select * from book left join class on class.card=book.card\g

*************************** 1. row ***************************

id: 1

select_type: ******

table: book

partitions: null

type: all

possible_keys: null

key: null

key_len: null

ref: null

rows: 20

filtered: 100.00

extra: null

*************************** 2. row ***************************

id: 1

select_type: ******

table: class

partitions: null

type: ref

possible_keys: idx_cc

key: idx_cc

key_len: 4

ref: demo.book.card

rows: 1

filtered: 100.00

extra: using index

2 rows in set, 1 warning (0.00 sec)

這裡建立1個索引就可以了,為什麼呢?

驅動表的概念,mysql中inner join指定了連線條件時,滿足查詢條件的記錄行數少的表為驅動表;如未指定查詢條件,則掃瞄行數少的為驅動表。

left join 寫在左邊的是驅動表,join後的是被驅動表

三個表的關聯與雙表是類似的,以小表驅動大表

MySQL索引優化案例

開發同學或多或少會遇到系統響應慢的問題,除了業務系統本身的問題外,常常會遇到sql查詢慢的問題,這篇文章結合實際案例分析mysql innodb儲存引擎的索引優化,這篇文章不會介紹b 樹的知識點,如果需要了解聚集索引和輔助索引特點的同學可以參考這篇文章,這篇文章主要會介紹三星索引和icp優化.首先是...

MySQL 索引優化案例

建表 create table article id int unsigned notnull primary keyauto increment author id int unsigned notnull category id int unsigned notnull views int un...

MySQL索引優化(索引三表優化案例)

建表sql phone book表建立索引 1 保證被驅動表的join欄位已經被索引 被驅動表 join 後的表為被驅動表 需要被查詢 2 left join 時,選擇小表作為驅動表,大表作為被驅動表。但是 left join 時一定是左邊是驅動表,右邊是被驅動表 3 inner join 時,my...