Mysql優化之深入了解存儲引擎,進行索引優化

比較兩種存儲引擎在數據庫中存儲方式:

MyIsam:仔細觀察的話會發現使用這種引擎的數據庫里面一般少說包含三個文件,**.frm ?,**.myi,(放索引) **.myd(放數據),通過索引(**.myi這個文件),定位數據在數據文件 在哪一行存放,這便會產生回行。如果沒有回行,也就是索引覆蓋,速度回非???/p>

InnoDb:也就是一個文件,索引和數據放在一塊,就是 聚簇索引一個壞處就是文件大啦,磁盤轉動,查找也就比較低啦。這樣也就產生了分頁的塊文件

創建表:里面包含主鍵索引和聯合索引,分別使用myisam引擎和innodb引擎

create table smth (
id int auto_increment ,
ver int(11) default null,
content varchar(1000) not null,
intro varchar(1000) not null,
primary key(id),
key idver(id,ver)

)engine = myisam default charset = utf8;

create table smth1 (
id int auto_increment ,
ver int(11) default null,
content varchar(1000) not null,
intro varchar(1000) not null,
primary key(id),
key idver(id,ver)

)engine = innodb default charset = utf8;
文件目錄分析如上:

Mysql優化之深入了解存儲引擎,進行索引優化

下面再創建連個存儲過程,進行插入一萬條數據

create procedure smthTest()
begin
declare num int default 1050;
while num set num := num +1;
insert into smth values (num ,num,’我是步’,’我是誰’);
end while ;

end;

create procedure smthTest1()
begin
declare num int default 0;
while num set num := num +1;
insert into smth1 values (num ,num,’我是步’,’我是誰’);
end while ;

end;

set profiling = 1; 顯示詳細信息

運行結果分析:

Mysql優化之深入了解存儲引擎,進行索引優化

語句分析結果:

mysql> explain?
?select id,ver,content from smth order by id; ?
+—-+————-+——-+——+—————+——+———+——+——+—————-+
| id | select_type | table | type | possible_keys | key ?| key_len | ref ?| rows | Extra ? ? ? ? ?|
+—-+————-+——-+——+—————+——+———+——+——+—————-+
| ?1 | SIMPLE ? ? ?| smth ?| ALL ?| NULL ? ? ? ? ?| NULL | NULL ? ?| NULL | 9946 | Using filesort |
+—-+————-+——-+——+—————+——+———+——+——+—————-+
1 row in set
mysql> explain
?select id,ver,content from smth order by id,ver;
+—-+————-+——-+——+—————+——+———+——+——+—————-+
| id | select_type | table | type | possible_keys | key ?| key_len | ref ?| rows | Extra ? ? ? ? ?|
+—-+————-+——-+——+—————+——+———+——+——+—————-+
| ?1 | SIMPLE ? ? ?| smth ?| ALL ?| NULL ? ? ? ? ?| NULL | NULL ? ?| NULL | 9946 | Using filesort |
+—-+————-+——-+——+—————+——+———+——+——+—————-+
1 row in set
mysql> explain
?select id,ver,content from smth1 order by id;
+—-+————-+——-+——-+—————+———+———+——+——+——-+
| id | select_type | table | type ?| possible_keys | key ? ? | key_len | ref ?| rows | Extra |
+—-+————-+——-+——-+—————+———+———+——+——+——-+
| ?1 | SIMPLE ? ? ?| smth1 | index | NULL ? ? ? ? ?| PRIMARY | 4 ? ? ? | NULL | 9932 | NULL ?|
+—-+————-+——-+——-+—————+———+———+——+——+——-+
1 row in set
mysql> explain select id
,ver,content from smth1 order by id,ver;
+—-+————-+——-+——+—————+——+———+——+——+—————-+
| id | select_type | table | type | possible_keys | key ?| key_len | ref ?| rows | Extra ? ? ? ? ?|
+—-+————-+——-+——+—————+——+———+——+——+—————-+
| ?1 | SIMPLE ? ? ?| smth1 | ALL ?| NULL ? ? ? ? ?| NULL | NULL ? ?| NULL | 9932 | Using filesort |
+—-+————-+——-+——+—————+——+———+——+——+—————-+

Mysql優化之深入了解存儲引擎,進行索引優化

mysql> explain
?select id from smth order by id;
+—-+————-+——-+——-+—————+———+———+——+——+————-+
| id | select_type | table | type ?| possible_keys | key ? ? | key_len | ref ?| rows | Extra ? ? ? |
+—-+————-+——-+——-+—————+———+———+——+——+————-+
| ?1 | SIMPLE ? ? ?| smth ?| index | NULL ? ? ? ? ?| PRIMARY | 4 ? ? ? | NULL | 9946 | Using index |
+—-+————-+——-+——-+—————+———+———+——+——+————-+
1 row in set
mysql> explain
?select id ?from smth order by id,ver;
+—-+————-+——-+——-+—————+——-+———+——+——+————-+
| id | select_type | table | type ?| possible_keys | key ? | key_len | ref ?| rows | Extra ? ? ? |
+—-+————-+——-+——-+—————+——-+———+——+——+————-+
| ?1 | SIMPLE ? ? ?| smth ?| index | NULL ? ? ? ? ?| idver | 9 ? ? ? | NULL | 9946 | Using index |
+—-+————-+——-+——-+—————+——-+———+——+——+————-+
1 row in set
mysql> explain
?select id from smth1 order by id;
+—-+————-+——-+——-+—————+———+———+——+——+————-+
| id | select_type | table | type ?| possible_keys | key ? ? | key_len | ref ?| rows | Extra ? ? ? |
+—-+————-+——-+——-+—————+———+———+——+——+————-+
| ?1 | SIMPLE ? ? ?| smth1 | index | NULL ? ? ? ? ?| PRIMARY | 4 ? ? ? | NULL | 9932 | Using index |
+—-+————-+——-+——-+—————+———+———+——+——+————-+
1 row in set
mysql> explain
?select id from smth1 order by id,ver;
+—-+————-+——-+——-+—————+——-+———+——+——+————-+
| id | select_type | table | type ?| possible_keys | key ? | key_len | ref ?| rows | Extra ? ? ? |
+—-+————-+——-+——-+—————+——-+———+——+——+————-+
| ?1 | SIMPLE ? ? ?| smth1 | index | NULL ? ? ? ? ?| idver | 9 ? ? ? | NULL | 9932 | Using index |
+—-+————-+——-+——-+—————+——-+———+——+——+————-+
1 row in set

? 版權聲明
THE END
喜歡就支持一下吧
點贊11 分享