简体   繁体   中英

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). But sometimes size of image url will be more than 200 which causes failure in Insertion/Updation.

To avoid this can I keep length of VARCHAR to some large value like 1000 or 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

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
  2. Engine: InnoDb

Simply make the column TEXT (64KB max) and hope that you never exceed that.

You won't be able to include the column in a regular index. But you should probably consider FULLTEXT and MATCH for searching.

For VARCHAR here are several cutoffs for various reasons: 40, 191, 255, 767, 3072, and maybe others. 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. This should not be a concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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