V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
miniyao
V2EX  ›  MySQL

“不建议 MySQL 使用 Text 类型”,对于 Text 内容比较短小也不能用吗?

  •  1
     
  •   miniyao · 2020-11-24 17:48:48 +08:00 · 6479 次点击
    这是一个创建于 1242 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一般知道 longtext 和大的 blob 放在 MySQL 里会影响性能。

    如果只是放一些短小 tinytext 、text 也会影响性能吗?

    比如有个字段大约在 300~500 个字符,那要么用 varchar(512),或者直接用 text,这两种方式,性能差别会很大吗?
    29 条回复    2023-04-24 17:14:11 +08:00
    warcraft1236
        1
    warcraft1236  
       2020-11-24 17:51:50 +08:00
    别的不知道,就知道 text 是没法整体加索引的,也是前多少个字符加到索引里边
    xuanbg
        2
    xuanbg  
       2020-11-24 17:54:58 +08:00
    既然 varchar 够用,就不要用 text 了。
    RedisMasterNode
        3
    RedisMasterNode  
       2020-11-24 18:02:11 +08:00   ❤️ 2
    InnoDB 里面决定 TEXT 和 Blob 类型是否要外联(也就是放到外部空间)依据是一个页中是否能够存放下 2 行数据,如果不可以,Text 和 Blob 就会外联,否则就会存在行内,这时和 varchar 等没有什么差异。按照你的描述如果只有 300-500 个字符,这时候 TEXT 会表现得和 varchar ( 500 )类似。

    大部分使用 TEXT 和 BLOB 的时候都是要存储好几 KB 、MB 的内容,可以考虑清楚自己要存什么东西再定

    对于索引,我猜可以通过索引字段内容的开头 xxx 字符处理,可以自行尝试一下
    sagaxu
        4
    sagaxu  
       2020-11-24 18:13:11 +08:00 via Android
    那 mysql 干嘛要有 text 类型?
    wnanbei
        5
    wnanbei  
       2020-11-24 18:16:02 +08:00
    `VARCHAR` 大于某些数值的时候,其会自动转换为 `text`,大概规则如下:

    - 大于 varchar(255) 变为 `TINYTEXT`
    - 大于 varchar(500) 变为 `TEXT`
    - 大于 varchar(20000) 变为 `MEDIUMTEXT`

    在某个地方看到的
    wnanbei
        6
    wnanbei  
       2020-11-24 18:23:54 +08:00
    我个人感觉用 varchar 不用 text 主要原因是 varchar 可以灵活设置长度,语义清晰的同时避免长度超长。varchar 底层就是 text 的话,性能应该是没有区别的。
    Hanggi
        7
    Hanggi  
       2020-11-24 18:27:24 +08:00
    换 Postgresql 好了
    keepeye
        9
    keepeye  
       2020-11-24 18:42:01 +08:00
    我个人习惯超过 255 的长度,都用 text
    wd
        10
    wd  
       2020-11-24 18:44:22 +08:00 via iPhone
    varchar 可以限制最大字符数,超了是没法存的。但是 text 没有限制,如果出了异常是有可能存超出你想象的数据的。所以对于已知是长度有限的数据,建议用 varchar 。
    dorothyREN
        11
    dorothyREN  
       2020-11-24 18:59:24 +08:00
    换 PG 吧,无脑 text
    qwerthhusn
        12
    qwerthhusn  
       2020-11-24 19:00:38 +08:00
    对于 Text 内容比较短小时不建议使用,大的时候就可以正常用了
    love
        13
    love  
       2020-11-24 19:43:50 +08:00
    很多人的性能瓶颈都是想象出来的,TEXT 的性能会产生性能瓶颈?你试过了嘛
    titititi
        14
    titititi  
       2020-11-24 20:52:35 +08:00
    正好今天看的一篇关于 Text 类型的文章

    https://juejin.cn/post/6898479206087262222
    raaaaaar
        15
    raaaaaar  
       2020-11-24 20:58:47 +08:00 via Android
    就我拿那 web 接口的使用,根本不可能有啥瓶颈
    guisheng
        16
    guisheng  
       2020-11-24 21:04:09 +08:00 via iPhone
    我不接受它的建议。
    loading
        17
    loading  
       2020-11-24 21:07:54 +08:00 via Android   ❤️ 1
    不要提前优化,先把 app 写出来再说。
    loading
        18
    loading  
       2020-11-24 21:08:29 +08:00 via Android
    有一本书,我没看,书名就够了。
    《要搞定,不要完美》
    Flymachine
        19
    Flymachine  
       2020-11-25 09:35:07 +08:00   ❤️ 1
    @wd 你一说这,我想到了日本斗鱼之前对直播弹幕长度没有限制。让 NGA 老哥搞出了 16G 核弹弹幕。能让内存 16G 以下的观看弹幕的电脑全部撑炸。据说还能搞出 32G 的弹幕。不过现在 BUG 修复了。应该就是这块的问题
    wnanbei
        20
    wnanbei  
       2020-11-25 09:51:23 +08:00
    @RedisMasterNode 看我 5L 的回复
    RedisMasterNode
        21
    RedisMasterNode  
       2020-11-25 10:00:21 +08:00
    @wnanbei varchar 和 text 不会转互
    no1xsyzy
        22
    no1xsyzy  
       2020-11-25 10:24:54 +08:00   ❤️ 1
    @loading https://tutorial.ponylang.io/
    The Pony Philosophy: Get Stuff Done
    wnanbei
        23
    wnanbei  
       2020-11-25 14:24:11 +08:00
    @RedisMasterNode 嗯?

    For a column that has a data type of VARCHAR or one of the TEXT types, changes the data type as necessary to ensure that the new column is long enough to store as many characters as the original column. For example, a TEXT column has two length bytes, which store the byte-length of values in the column, up to a maximum of 65,535. For a TEXT column, each character requires a single byte, so the column can store up to 65,535 characters. If the column is converted to , each character might require up to three bytes, for a maximum possible length of 3 × 65,535 = 196,605 bytes. That length does not fit in a TEXT column's length bytes, so MySQL converts the data type to MEDIUMTEXT, which is the smallest string type for which the length bytes can record a value of 196,605. Similarly, a VARCHAR column might be converted to MEDIUMTEXT. CONVERT TO CHARACTER SETlatin1utf8

    https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
    wnanbei
        24
    wnanbei  
       2020-11-25 14:26:41 +08:00
    @RedisMasterNode 还有

    Specifying the attribute for a character data type causes the column to be created as the corresponding binary data type: CHAR becomes BINARY, VARCHAR becomes VARBINARY, and TEXT becomes BLOB. For the ENUM and SET data types, this does not occur; they are created as declared. Suppose that you specify a table using this definition: CHARACTER SET binary

    https://dev.mysql.com/doc/refman/8.0/en/silent-column-changes.html
    RedisMasterNode
        25
    RedisMasterNode  
       2020-11-25 14:44:36 +08:00
    @wnanbei 请注意这部分规则触发的条件,varchar 类型不会在你进行数据改动时转换为 text,这部分操作时在 create/alter table 时触发的,并且条件较为严苛

    In some cases, MySQL silently changes column specifications from those given in a CREATE TABLE or ALTER TABLE statement.
    - If strict SQL mode is not enabled, a VARCHAR column with a length specification greater than **65535** is converted to TEXT.

    而在数据类型确认下来后,如果没有 alter table,只对数据进行改动,数据类型是不会变化的
    wnanbei
        26
    wnanbei  
       2020-11-26 09:26:33 +08:00
    @RedisMasterNode 既然不管在 create 语句还是 alter 语句的时候都会静默更改类型,那么这不就是等价吗
    RedisMasterNode
        27
    RedisMasterNode  
       2020-11-26 10:08:17 +08:00
    @wnanbei 嗯,那这个是我理解错了,我以为你说的是 varchar 在使用过程中会根据长度转 text,这个是我整错了
    DEVN
        28
    DEVN  
       2020-12-17 07:58:44 +08:00 via iPhone
    谁又会把 text 和 varchar 做比较呢?
    rebeccaMyKid
        29
    rebeccaMyKid  
       361 天前
    @wd
    @Flymachine
    啊? 最长的 LONGTEXT 也最多存 4GB 啊,你哪里看到的是 unlimited 的?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   922 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:43 · PVG 04:43 · LAX 13:43 · JFK 16:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.