mysql三表關聯加索引 mysql 三表索引優化

2021-10-19 18:50:31 字數 3554 閱讀 7737

建表語句

create table if not exists `phone`(

`phoneid` int(10) unsigned not null auto_increment,

`card` int(10) unsigned not null,

primary key(`phoneid`)

)engine = innodb;

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

insert into phone(card) values(floor(1 + (rand() * 20)));

使用explain解讀sql

mysql> explain select * from class left join book on class.card=book.card left join phone on book.card = phone.card;

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |

| 1 | ****** | class | null | index | null | y | 4 | null | 24 | 100.00 | using index |

| 1 | ****** | book | null | ref | y | y | 4 | test.class.card | 1 | 100.00 | using index |

| 1 | ****** | phone | null | all | null | null | null | null | 20 | 100.00 | using where; using join buffer (block nested loop) |

3 rows in set, 1 warning (0.00 sec)

可以看到phone表的type為all, extra中顯示使用了連線緩衝區區。

建立phone表的card索引

alter table `phone` add index z(`card`);

再用explain檢視sql語句 mysql> explain select * from class left join book on class.card=book.card left join phone on book.card = phone.card;

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |

| 1 | ****** | class | null | index | null | y | 4 | null | 24 | 100.00 | using index |

| 1 | ****** | book | null | ref | y | y | 4 | test.class.card | 1 | 100.00 | using index |

| 1 | ****** | phone | null | ref | z | z | 4 | test.book.card | 1 | 100.00 | using index |

注意rows: 掃瞄的行數

其實可以做更好的優化: mysql> explain select * from phone left join book on phone.card=book.card left join class on book.card = class.card;

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |

| 1 | ****** | phone | null | index | null | z | 4 | null | 20 | 100.00 | using index |

| 1 | ****** | book | null | ref | y | y | 4 | test.phone.card | 1 | 100.00 | using index |

| 1 | ****** | class | null | ref | y | y | 4 | test.book.card | 1 | 100.00 | using index |

3 rows in set, 1 warning (0.00 sec)

思想: 用小表驅動大表, 能更好地減少行掃瞄次數。

結論:索引最好設定在需要經常查詢的字段中

盡可能減少join語句中的nestedloop的迴圈總數

永遠用小結果集驅動大的結果集

mysql給表的字段加索引

1 新增普通索引 alter table table name add index index name column 2 新增主鍵索引 alter table table name addprimary key column 3 新增唯一索引 unique alter table table na...

mysql索引框架 MySQL架構和MySQL索引

1.mysql架構 1.1邏輯架構圖 1.1.1connection pool 連線池 管理緩衝使用者連線,執行緒處理等需要快取的需求。負責監聽對mysql server的各種請求,接收連線請求,所有連線請求到執行緒管理模組。每乙個連線上mysql server的客戶端請求都會被分配 或建立 乙個連...

mysql某字典加部分索引 MySQL索引

1.什麼是索引 索引是對資料庫表中一列或多列的值進行排序的一種結構,使用索引可快速訪問資料庫表中的特定資訊.sql索引在資料庫優化中佔非常大的比例,乙個好的索引設計,可以讓你的效率提高幾十甚至幾百倍.2.深入淺出理解索引 其實,我們的漢語字典的正文本身就是乙個聚集索引。比如,我們要查 安 字,就會很...