mysql經典案例分析

來源:互聯網
上載者:User

標籤:add   index   efault   span   覆蓋   isa   為什麼   案例   lte   

問題:

create table A (
id varchar(64) primary key,
ver int,
...
)
在id,ver上有聯合索引,10000條資料
為什麼select id from A order by id特別慢?
而select id from A order by id,ver非常快
我的表有幾個很長的欄位 varbinary(3000)

推斷: 1. 2句sql都用到了索引覆蓋,如果myisam引擎2句sql應該都很快, 推斷用的是innodb引擎2. order by id ,innodb 引擎聚簇儲存了每列的值,因為有幾個很長的欄位,1個塊存不了很多行資料,導致塊比較多,使用id主鍵時,要跨好多小檔案塊,導致效率不高。3. order by id,ver. 使用的是二級索引,innodb引擎二級索引都是存的 聚簇索引的地址指向聚簇索引,因此不帶資料,索引檔案比較小輕便,記憶體中也能使用,所以速度快。
create table t7 (
id char(64) primary key,
var int not null default 0,
str1 varchar(3000) not null,
str2 varchar(3000) not null,
str3 varchar(3000) not null,
str4 varchar(3000) not null
key `idvar` (id,var)
)engine=myisam charset=utf8;

create table t8 (
id char(64) primary key,
var int not null default 0,
str1 varchar(3000) not null,
str2 varchar(3000) not null,
str3 varchar(3000) not null,
str4 varchar(3000) not null
)engine=innodb charset=utf8;
alter table t7 add index idver(id,var)

結論: innodb 大欄位(char)主鍵 造成大量分裂, 正好發揮的是innodb的劣勢
如果沒有這麼長大欄位的列 ,差距也不會很大
alter table t8 drop column st1;alter table t8 drop column st2;alter table t8 drop column st3;

mysql經典案例分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.