简体   繁体   English

将 VARCHAR 列长度保持在某个较大的值以避免任何 DB Upsert 失败是一种好习惯吗

[英]Is it good practice to keep VARCHAR column length to some large value to avoid any DB Upsert failures

We have E-Commerce Products table in our Production Database, which has image url column as VARCHAR(200).我们的生产数据库中有电子商务产品表,其中图像 url 列为 VARCHAR(200)。 But sometimes size of image url will be more than 200 which causes failure in Insertion/Updation.但有时图像 url 的大小会超过 200,这会导致插入/更新失败。

To avoid this can I keep length of VARCHAR to some large value like 1000 or 2000?.为避免这种情况,我可以将 VARCHAR 的长度保持在 1000 或 2000 之类的较大值吗? By doing this will it affect my query performance by any chance?这样做会影响我的查询性能吗?

Also I'm using Hibernate in my Application to Connect to Mariadb另外我在我的应用程序中使用 Hibernate 连接到 Mariadb

Failing with these errors因这些错误而失败

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement

Caused by: java.sql.SQLException: Data too long for column 'url' at row 1

Note笔记

  1. Database: Mariadb 10.4.2数据库:Mariadb 10.4.2
  2. Engine: InnoDb引擎:InnoDb

Simply make the column TEXT (64KB max) and hope that you never exceed that.只需创建TEXT列(最大 64KB)并希望您永远不会超过该列。

You won't be able to include the column in a regular index.您将无法将该列包含在常规索引中。 But you should probably consider FULLTEXT and MATCH for searching.但是您可能应该考虑使用FULLTEXTMATCH进行搜索。

For VARCHAR here are several cutoffs for various reasons: 40, 191, 255, 767, 3072, and maybe others.对于VARCHAR ,由于各种原因,这里有几个截止值:40、191、255、767、3072,也许还有其他。 The depend on row format, version number, character set, etc. Some of those numbers are bytes, some are characters.取决于行格式、版本号、字符集等。其中一些数字是字节,一些是字符。

TEXT and VARCHAR each have a short (1-4 bytes) "length" field plus the number of bytes necessary for the data. TEXTVARCHAR每个都有一个短的(1-4 字节)“长度”字段加上数据所需的字节数。 This should not be a concern.这不应该是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM