搜尋
首頁資料庫mysql教程mysql索引长度tips innodb和myisam引擎_MySQL

bitsCN.com

mysql索引长度tips innodb和myisam引擎

 

由于开发人员对索引认识不深或忽略,还有版本不同等问题,在生产环境中创建表失败,引发了一些问题。归纳了一下

    测试环境

 

       mysql> select version();

+------------+

| version()  |

+------------+

| 5.5.31-log | 

+------------+

1 row in set (0.01 sec)

   innodb 引擎

mysql> CREATE TABLE `meta_topic_scan` (   `domain` varchar(257) NOT NULL,   `topic_name` varchar(200) NOT NULL,   `topic_url` varchar(200) NOT NULL,   `topic_pv` int(11) DEFAULT'0',   `topic_uv` int(11) DEFAULT '0',   PRIMARY KEY (`domain`,`topic_url`) ) ENGINE=innodb  DEFAULT CHARSET=utf8;

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

 

innodb 单列索引长度不能超过767 bytes,联合索引限制是3072 bytes 。对于创建innodb的组合索引中,如果各个列中的长度有单个超过767 bytes,也会创建失败;

 

myisam 引擎 

创建复合索引:

 

mysql> CREATE TABLE `meta_topic_scan` (

    ->   `domain` varchar(200) NOT NULL,

    ->   `topic_name` varchar(200) NOT NULL,

    ->   `topic_url` varchar(200) NOT NULL,

    ->   `topic_pv` int(11) DEFAULT '0',

    ->   `topic_uv` int(11) DEFAULT '0',

    ->   PRIMARY KEY (`domain`,`topic_name`,`topic_url`)

    -> ) ENGINE=myisam DEFAULT CHARSET=utf8 ;

ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

创建单列索引:

mysql> CREATE TABLE `meta_topic_scan` (

    ->   `domain` varchar(334) NOT NULL,

    ->   `topic_name` varchar(200) NOT NULL,

    ->   `topic_url` varchar(200) NOT NULL,

    ->   `topic_pv` int(11) DEFAULT '0',

    ->   `topic_uv` int(11) DEFAULT '0',

    ->   PRIMARY KEY (`domain`)

    -> ) ENGINE=myisam DEFAULT CHARSET=utf8 ;

ERROR 1071 (42000): Specified key was too long; max key length is 1000 bytes

由此可知:myisam 单列索引长度、所创建的复合索引长度和都不能超过1000 bytes,否则会报错,创建失败。

另外不同字符集占用不同字节:latin一个字符占1 bytes,utf8存储一个字符占3 bytes, gbk存储一个字符2 bytes

扩展: innodb复合索引长度为什么是3072 

 我们知道InnoDB一个page的默认大小是16k。由于是Btree组织,要求叶子节点上一个page至少要包含两条记录(否则就退化链表了)。

 

        所以一个记录最多不能超过8k。

        又由于InnoDB的聚簇索引结构,一个二级索引要包含主键索引,因此每个单个索引不能超过4k (极端情况,pk和某个二级索引都达到这个限制)。

         由于需要预留和辅助空间,扣掉后不能超过3500,取个“整数”就是(1024*3)。 

 

单列索引限制

 

         上面有提到单列索引限制767,起因是256×3-1。这个3是字符最大占用空间(utf8)。但是在5.5以后,开始支持4个字节的uutf8。255×4>767, 于是增加了一个参数叫做 innodb_large_prefix。

 

         这个参数默认值是OFF。当改为ON时,允许列索引最大达到3072。

 

               如下效果(5.5):


mysql索引长度tips innodb和myisam引擎_MySQL


bitsCN.com
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何在MySQL中刪除或修改現有視圖?如何在MySQL中刪除或修改現有視圖?May 16, 2025 am 12:11 AM

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。 whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

MySQL視圖:我可以使用哪些設計模式?MySQL視圖:我可以使用哪些設計模式?May 16, 2025 am 12:10 AM

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

在MySQL中使用視圖的優點是什麼?在MySQL中使用視圖的優點是什麼?May 16, 2025 am 12:09 AM

查看InMysqlareBeneForsImplifyingComplexqueries,增強安全性,確保dataConsistency,andOptimizingPerformance.1)他們simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

如何在MySQL中創建一個簡單的視圖?如何在MySQL中創建一個簡單的視圖?May 16, 2025 am 12:08 AM

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

MySQL創建用戶語句:示例和常見錯誤MySQL創建用戶語句:示例和常見錯誤May 16, 2025 am 12:04 AM

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

在MySQL中使用視圖的局限性是什麼?在MySQL中使用視圖的局限性是什麼?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

確保您的MySQL數據庫:添加用戶並授予特權確保您的MySQL數據庫:添加用戶並授予特權May 14, 2025 am 12:09 AM

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素會影響我可以在MySQL中使用的觸發器數量?哪些因素會影響我可以在MySQL中使用的觸發器數量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。