问题
I'm designing a database, since I'm not yet confortable with the data types I was wondering the following :
If I don't care about the length of a vector of characters, is it appropriate to use varchar ? (even if the strings are expected to be quite short ?)
Until now I've been using text (varchar) when I needed to store really long strings but reading the documentation and asking questions around I heard postgresql does not store the extra-bytes if the maximum-length is supplied and the stored string's length is lesser than it. I guess the maximum-length is used in case the distant device can't afford large memory space. Could one agree or explain that ?
回答1:
From the docs:
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.
来源:https://stackoverflow.com/questions/16525701/using-varchar-over-varcharn-with-postgresql