MySQL in the end to the number of fields

Today, technical discussion group where "all go with the flow" occurs when students read a question, a MySQL table in the end may have a number of fields? With this question, we have started to explore, and then discussed the issue of a single field length.

1. Official documentation

Content official document as follows, the main point is less than ideal field to limit the number of 4096, and the type of field and upper limit field innodb engine is 1017 ,.

 2. Test table limit the number of fields

2.1 innodb engine test table

Because official documentation describes innodb table field limit is 1017, so you can write programs for simulation. Ideas are as follows:

a) create a one char (1) Type of innodb table

b) To the circular table until a new error field

I'm using python test script, the script is as follows:

# ! / Usr / bin / Python 
# Coding. 8 = UTF- 
Import pymysql AS MDB
 Import OS 

sor_conn = mdb.connect (Host = ' 127.0.0.1 ' , Port = 3306, = User ' the root ' , the passwd = ' 123456 ' ) 
sor_cur = sor_conn.cursor () 

v_sql_d = " drop IF EXISTS test.test_c Table; "    # for the program is repeated, adding determination 

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()

Results are as follows:

[python_pro the testdb the root @] # Python test_column.py 
Mysql Error 1117 : Too MANY Columns 
the MySQL has A limit of 1017 

in the client SQLyog manual validation result is the same
 

 

Therefore, MySQL innodb engine table described in official documents with up to 1017 fields.

 

 

2.2 MYISAM engine test table

Because MySQL in another table MYISAM engine is also very important before MySQL5.7 storage engine version, but subsequent versions use less and less, but still need to test something.

Innodb ideas and test procedures are all the same, only the table of engine modifications, as follows:

#!/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()

Results are as follows:

[root@testdb python_pro]# python test_column.py 
Mysql Error 1117: Too many columns
MySQL has a limit of 2598

That engine in MySQL MyISAM table can store up to 2598 fields.

 

3. Test field length limit

We all know that a knowledge of MySQL in a row in addition to a large blob and text fields like the length of the rest of the field and not more than 65,535, then this is to determine what, so does one test again.

3.1 Test UTF8 character set

Creating a only one table field length field is actually the result of an error 65535, suggesting that only the maximum length is 21845, which is the amount of 65535/3,

/ *   Test single field length limit * / 
the CREATE   TABLE   test_c1 ( 
C1 VARCHAR ( 65535 ) 
) ENGINE = INNODB the CHARACTER  the SET UTF8;
 / * execution result * / 
Error Code: 1074 
the Column length TOO Big for  column  ' C1 ' ( max  =  21845 ) ; use BLOB or  TEXT INSTEAD

But instead 21845 is still being given, because you carefully goods (prompt varchar)

CREATE  TABLE  test_c1(
c1  VARCHAR(21845) 
) ENGINE=INNODB CHARACTER SET utf8;

/* 执行结果依旧报错 */
错误代码: 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

That, in a reduced Try

The CREATE   TABLE   test_c1 ( 
c1   VARCHAR ( 21844 ) 
) ENGINE = INNODB CHARACTER  the SET utf8;
 / * finally succeeded * / 
query: the Create  the Table test_c1 (c1 VARCHAR ( 21844 )) Engine = InnoDB Character  the SET utf8 

Total 0 rows affected

Map with the truth

 

 3.2 Test latin character set

Because utf8 encoding three bits, and therefore can only be a maximum length of 21845 (-1), then the latin character set is not able to reach a 65535

Tests are as follows

CREATE  TABLE  test_c1(
c1  VARCHAR(65535) 
) ENGINE=INNODB CHARACTER SET latin1
/* 结果依旧失望 */
错误代码: 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

In the case of the above think, has been cut down, it can normally find 65532 (reason for you to continue product will understand)

The CREATE   TABLE   test_c1 ( 
C1   VARCHAR ( 65532 ) 
) ENGINE = INNODB the CHARACTER  the SET latin1;
 / * finally succeeded * / 
< n- > Query: Create  Table test_c1 (C1 VARCHAR ( 65532 )) Engine = InnoDB Character  SET latin1 

Total 0 row affected

To the truth

 

3. Summary

Practice makes perfect, anyone who says knowledge points have to think, when necessary, test yourself something.

Field limit

 

Field length limit

 

 In this knowledge to a hasty summary, do not understand the reason you can view the official documentation, but also to discuss in detail the test, you can also add group.

 

Guess you like

Origin www.cnblogs.com/gjc592/p/12609153.html