mysql全程 MySQL索引使用全程分析

2021-10-17 22:45:03 字數 2931 閱讀 8545

建立2張使用者表user、user2,表結構相同,但user表使用innodb儲存引擎,而user2表則使用 myisam儲存引擎。

複製** **如下:

-- table "user" ddl

create table `user` (

`id` int(11) not null auto_increment,

`name` varchar(50) default null,

`email` varchar(100) default null,

`age` tinyint(4) default null,

`nickname` varchar(50) default null,

primary key (`id`),

unique key `email` (`email`),

key `name` (`name`),

key `age` (`age`)

) engine=innodb default charset=utf8;

-- table "user2" ddl

create table `user2` (

`id` int(11) not null auto_increment,

`name` varchar(50) default null,

`email` varchar(100) default null,

`age` tinyint(4) default null,

`nickname` varchar(50) default null,

primary key (`id`),

unique key `email` (`email`),

key `name` (`name`),

key `age` (`age`)

) engine=myisam auto_increment=131610 default charset=utf8;

分別插入10w條測試資料到表user & user2。

複製** **如下:

$example = array(

'@qq.com',

'@sina.com.cn',

'@163.com',

'@126.com',

'@gmail.com',

'@yahoo.com',

'@live.com',

'@msn.com',

'@cisco.com',

'@microsoft.com',

'@ibm.com',

$con = mysql_connect("localhost", "root", "your_mysql_password");

mysql_select_db("index_test", $con);

//新增10w測試資料到表 user & user2

for($i=0; $i<100000; $i++)

$temp = md5(uniqid());

$name = substr($temp, 0, 16);

$email = substr($temp, 8, 12).$example[array_rand($example, 1)];

$age = rand(18, 99);

$nickname = substr($temp, 16, 16);

mysql_query("insert into user(name,email,age,nickname) values('$name','$email',$age,'$nickname')");

mysql_query("insert into user2(name,email,age,nickname) values('$name','$email',$age,'$nickname')");

mysql_close($con);

echo 'success';

對索引的使用分析

圖1explain select * from user2 where id>100 \g;

圖2user 表中的資料和 user2 表中的資料是一樣的,索引結構也是一樣的,只不過它們的儲存引擎不同。在圖1中,查詢用到了primary主鍵索引,而查詢優化器預估的結果大概在65954行左右(實際是131513);在圖2中,查詢卻沒有使用索引,而是全表掃瞄了,返回的預估結果在131608行(實際是131509)。

explain select * from user where id>100 and age>50 \g;

圖3explain select * from user where id>100 and age=50 \g;

圖4explain select * from user2 where id>100 and age>50 \g;

圖5explain select * from user2 where id>100 and age=50 \g;

圖6

mysql索引使增刪變慢 Mysql索引優化

2.mysql索引優化 2.1.explain執 計畫分析 2.2.索引命中策略 分析 2.3.索引分析總結 2.4.資料庫出現問題後如何死 而不 僵 資料庫卡頓情景 解決思路 sql及索引 質 的sql,避免索引失效 資料庫表結構 正規化,至少要符合3nf 系統配置mysql,linux 硬體1 ...

mysql如何使用索引 MySQL是如何使用索引的

閒扯 很多時候我們面對很慢的查詢的時候會一籌莫展,這個時候大部分人都會很自然的想到建索引這條路。事實上索引確實是個很好的優化方式,乙個良好的索引能夠提公升不止一倍的效率,還能帶來併發能力的提公升。但是索引也不是萬能的,不然的話我大可以給一張表的所有列上都加上索引,但是基本上所有的dba都會有一條認知...

mysql 索引 手冊 MySQL 索引

mysql 索引 mysql索引的建立對於mysql的高效執行是很重要的,索引可以大大提高mysql的檢索速度。打個比方,如果合理的設計且使用索引的mysql是一輛蘭博基尼的話,那麼沒有設計和使用索引的mysql就是乙個人力三輪車。索引分單列索引和組合索引。單列索引,即乙個索引只包含單個列,乙個表可...