VARCHAR vs TEXT performance when data fits on row

前端 未结 4 1416
面向向阳花
面向向阳花 2020-12-30 10:40
mysql> desc temp1;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+         


        
相关标签:
4条回答
  • 2020-12-30 10:50

    let's use some tools

    Since the initial hunch (see below) was a miss, try running your query via MySQL Workbench in order to gather Query Performance Stats.


    initial hunch (no result)

    Just a thought:

    • TEXT column size on disk is 2 + N bytes where N is the length of the string
    • VARCHAR takes 1 + N bytes (for N ≤ 255) or 2 + N bytes (for 256 ≤ N ≤ 65535)

    Try extending the size of the text in the column above 256 characters and re-run your tests. Potentially they will run with performance more closely matched.

    Please also mind that the differences you post are expressed in microseconds per record, so there could be many OS events getting in the way or very simple if (TEXT) {do some additional IO or housekeeping} code path in the source.

    0 讨论(0)
  • 2020-12-30 11:04

    Your first case assumption is not correct. Based on Storage Requirements TEXT stores one more byte for 255 a than VARCHAR so for 33554432 records in your table you need 33554432 more bytes to load in memory and explains the time delay.

    That of course would not apply for 5000 a where based on the same documentation the size is the same L + 2 bytes. But i think the reason for that delay is described in Row Size Limits where it writes:

    The internal representation of a MySQL table has a maximum row size limit of 65,535 bytes, even if the storage engine is capable of supporting larger rows. BLOB and TEXT columns only contribute 9 to 12 bytes toward the row size limit because their contents are stored separately from the rest of the row.

    I think it is quite different to be part of the row data and to be stored separately(need some time to retrieve it from the location stored) and this explains the time delay.

    0 讨论(0)
  • 2020-12-30 11:05

    With respect to storage, InnoDB will handle VARCHAR and TEXT much the same when both stored inline. However, when fetching the data from InnoDB, the server will allocate space for all VARCHAR columns before query execution. While space for TEXT columns will only be allocated if they are actually read, where DYNAMIC memory allocation takes time.

    https://forums.mysql.com/read.php?24,645115,645164#msg-645164

    0 讨论(0)
  • 2020-12-30 11:13

    TEXT type will be always slower than VARCHAR because those types have different methods of storage. VARCHAR field stored in the table with all columns but TEXT stored differently. Each TEXT value is a separate object. It means if you want to do something with TEXT value MySQL will make additional operations to get that object.

    Quote from the official documentation:

    Each BLOB or TEXT value is represented internally by a separately allocated object. This is in contrast to all other data types, for which storage is allocated once per column when the table is opened.

    0 讨论(0)
提交回复
热议问题