今天技術(shù)討論群里 “一切隨遇而安”同學(xué)看書時(shí)出現(xiàn)一個(gè)疑問(wèn),一個(gè)MySQL的表中到底可以有多少個(gè)字段?帶著這個(gè)疑問(wèn),我們展開了探討,也接著討論了一個(gè)單字段長(zhǎng)度的問(wèn)題。 1. 官方文檔說(shuō)明官方文檔的內(nèi)容如下,主要意思是字段個(gè)數(shù)限制達(dá)不到理想的4096個(gè),且和字段類型有關(guān),innodb引擎的字段上限是1017,。 2. 測(cè)試表字段數(shù)限制2.1 測(cè)試innodb引擎表因官方文檔介紹了innodb表字段限制是1017,因此可以寫程序進(jìn)行模擬。思路如下: a) 創(chuàng)建一張1個(gè) char(1) 類型的innodb表 b) 循環(huán)往該表新增字段 直至報(bào)錯(cuò) 我使用的是python 腳本進(jìn)行測(cè)試,腳本如下: #!/usr/bin/python # coding=utf-8 import pymysql as mdb import os sor_conn = mdb.connect(host='127.0.0.1',port=3306,user='root',passwd='123456') sor_cur = sor_conn.cursor() v_sql_d = "drop table if exists test.test_c ;" # 為了程序重復(fù)執(zhí)行,添加判斷 sor_cur.execute(v_sql_d) sor_conn.commit() v_sql_c = "create table test.test_c(c1 char(1)) engine=innodb;" sor_cur.execute(v_sql_c) sor_conn.commit() v_id=2 while v_id<50000: v_sql_add_c = " alter table test.test_c add c%d char(1);"%(v_id) try: sor_cur.execute(v_sql_add_c) sor_conn.commit() except mdb.Error,e: v_cnt = v_id - 1 print "Mysql Error %d: %s" % (e.args[0], e.args[1]) print "MySQL has a limit of %d" %(v_cnt) break v_id = v_id + 1 sor_conn.close() 運(yùn)行結(jié)果如下: [root@testdb python_pro]# python test_column.py Mysql Error 1117: Too many columns MySQL has a limit of 1017 ![]()
因此,官方文檔中介紹的MySQL innodb引擎表最多有1017個(gè)字段。
2.2 測(cè)試MYISAM引擎表因?yàn)镸ySQL中另一種MYISAM引擎的表在MySQL5.7版本之前也是非常重要的存儲(chǔ)引擎,只是后續(xù)版本使用越來(lái)越少,但是 還是有必要測(cè)試一番。 程序思路與測(cè)試innodb是均一致,只是將表的引擎進(jìn)行修改,如下: #!/usr/bin/python # coding=utf-8 import pymysql as mdb import os import datetime import time sor_conn = mdb.connect(host='127.0.0.1',port=3306,user='root',passwd='123456') sor_cur = sor_conn.cursor() v_sql_d = "drop table if exists test.test_c ;" sor_cur.execute(v_sql_d) sor_conn.commit() v_sql_c = "create table test.test_c(c1 char(1))engine=MYISAM ;" sor_cur.execute(v_sql_c) sor_conn.commit() v_id=2 while v_id<50000: v_sql_add_c = " alter table test.test_c add c%d char(1);"%(v_id) try: sor_cur.execute(v_sql_add_c) sor_conn.commit() except mdb.Error,e: v_cnt = v_id - 1 print "Mysql Error %d: %s" % (e.args[0], e.args[1]) print "MySQL has a limit of %d" %(v_cnt) break v_id = v_id + 1 sor_conn.close() 運(yùn)行結(jié)果如下: [root@testdb python_pro]# python test_column.py Mysql Error 1117: Too many columns MySQL has a limit of 2598 也就是說(shuō)MySQL中MyISAM引擎表最多可以存2598個(gè)字段。
3. 測(cè)試字段長(zhǎng)度限制大家都知道的一個(gè)知識(shí)是在MySQL中一行除了blob及text類的大字段之外,其余字段的長(zhǎng)度之和不能超過(guò)65535,那么這個(gè)是確定的么,因此再次做一次測(cè)試。 3.1 測(cè)試UTF8字符集創(chuàng)建一個(gè)只有一個(gè)字段的表,字段長(zhǎng)度為65535 結(jié)果居然報(bào)錯(cuò)了,提示最大長(zhǎng)度只能是21845,也就是65535/3的量, /* 測(cè)試單字段長(zhǎng)度 上限*/ CREATE TABLE test_c1( c1 VARCHAR(65535) ) ENGINE=INNODB CHARACTER SET utf8; /* 執(zhí)行結(jié)果 */ 錯(cuò)誤代碼: 1074 Column length too big for column 'c1' (max = 21845); use BLOB or TEXT instead 但是改為21845依舊報(bào)錯(cuò),原因你仔細(xì)品(提示varchar) CREATE TABLE test_c1( c1 VARCHAR(21845) ) ENGINE=INNODB CHARACTER SET utf8; /* 執(zhí)行結(jié)果依舊報(bào)錯(cuò) */ 錯(cuò)誤代碼: 1118 Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs 那,在減小一位試試 CREATE TABLE test_c1( c1 VARCHAR(21844) ) ENGINE=INNODB CHARACTER SET utf8; /* 終于成功了*/ 查詢:create table test_c1( c1 varchar(21844) ) engine=innodb character set utf8 共 0 行受到影響 有圖有真相
3.2 測(cè)試latin字符集因?yàn)閡tf8編碼占3位,因此最大長(zhǎng)度只能是21845(-1),那么latin字符集是不是就能達(dá)到65535了 測(cè)試如下 CREATE TABLE test_c1( c1 VARCHAR(65535) ) ENGINE=INNODB CHARACTER SET latin1 /* 結(jié)果依舊失望 */ 錯(cuò)誤代碼: 1118 Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs 在想想上面的情況,一直減下去,發(fā)現(xiàn)65532即可正常(原因你繼續(xù)品就明白了) CREATE TABLE test_c1( c1 VARCHAR(65532) ) ENGINE=INNODB CHARACTER SET latin1; /* 終于成功了 */ <n>查詢:create table test_c1( c1 varchar(65532) ) engine=innodb character set latin1 共 0 行受到影響 給真相
3. 小結(jié)實(shí)踐出真知,任何人說(shuō)的知識(shí)點(diǎn)都要思考,必要的時(shí)候自己檢驗(yàn)一番。 表字段限制
表字段長(zhǎng)度限制
在此知識(shí)給個(gè)匆忙的小結(jié),其中原因不懂的可以查看官方文檔,也是詳細(xì)的測(cè)試,也可以加群一起討論。
|
|