在不知道業務欄位長度的情況下,將欄位型別設定為varchar,和varchar(N)相比,有什么區別,對查詢的性能有影響嗎?
uj5u.com熱心網友回復:
varchar表示接收任何長度的字串,在pg中和text型別類似,最大存盤1G的資料uj5u.com熱心網友回復:
https://www.postgresql.org/docs/current/datatype-character.htmlvarchar性能更好:
Tip
There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead.
uj5u.com熱心網友回復:
這三種型別之間沒有性能差異,除了使用空白填充型別時增加了存盤空間,以及在存盤到長度限制的列時檢查長度時增加了一些額外的CPU周期。雖然字符(n)在其他一些資料庫系統中具有性能優勢,但在PostgreSQL中卻沒有這種優勢;事實上,字符(n)通常是這三種方法中最慢的,因為它需要額外的存盤成本。在大多數情況下,應該使用文本或字符變化。uj5u.com熱心網友回復:
varchar(N)最大的意義不在于性能在于約束實作一下功能1.約束資料質量,比如身份證號欄位
2.避免某個情緒不穩定程式員或者用戶因為某種神乎其技的操作,往user表的name列每一行的插一本《戰爭與和平》進去。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/16721.html
標籤:PostgreSQL
