professional-programming/antipatterns/database-antipatterns.md

29 lines
1.1 KiB
Markdown
Raw Normal View History

2020-07-21 09:02:44 +02:00
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
2020-07-21 09:37:15 +02:00
## Table of Contents
2020-07-21 09:02:44 +02:00
- [Database anti-patterns](#database-anti-patterns)
- [Using `VARCHAR` instead of `TEXT` (PostgreSQL)](#using-varchar-instead-of-text-postgresql)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2020-07-21 09:14:24 +02:00
# Database anti-patterns
2020-07-21 09:02:44 +02:00
2020-07-21 09:14:24 +02:00
## Using `VARCHAR` instead of `TEXT` (PostgreSQL)
2020-07-21 09:02:44 +02:00
Unless you absolutely restrict the width of a text column for data consistency
reason, don't do it.
This
[benchmark](http://www.depesz.com/2010/03/02/charx-vs-varcharx-vs-varchar-vs-text/)
shows that there's fundamentally no difference in performance between
`char(n)`, `varchar(n)`, `varchar` and `text`. Here's why you should pick
`text`:
2020-07-21 09:14:24 +02:00
- `char(n)`: takes more space than necessary when dealing with values shorter
2020-07-21 09:02:44 +02:00
than n.
2020-07-21 09:14:24 +02:00
- `varchar(n)`: it's difficult to change the width.
- `varchar` is just like `text`.
- `text` does not have the width problem that `char(n)` and `varchar(n)` and
2020-07-21 09:02:44 +02:00
has a cleaner name than `varchar`.