views:

265

answers:

1

Is using a VARCHAR2 (1 BYTE) any less efficient than using CHAR(1 BYTE)?

Is using a VARCHAR2 (2000 BYTE) any less efficient than using CHAR(1 BYTE), if I never put any value longer than one character in the field?

** By efficient, I meant efficient in both time (searching) and space (storing).

+5  A: 

Depending on your character set, CHAR(1 BYTE) may not be able to store any particular character, so there may be differences between VARCHAR2(2000 BYTE) and CHAR(1 BYTE) if we're talking about multi-byte characters.

More generally, in SQL (the PL/SQL rules are a bit different) a VARCHAR2(2000 BYTE) isn't going to consume more storage than a CHAR(1 BYTE) if you are storing just 1 byte of data (I assume "efficient" translates primarily to storage efficiency). A VARCHAR2(2000 BYTE) will frequently cause a client application to allocate a 2000 byte buffer to hold the contents of the field, since the client application doesn't know the actual data size in advance, which may cause the client application to use excessive amounts of RAM. That may or may not translate into a real issue, though. Most client applications aren't hurting for RAM and most result sets aren't returning millions of rows to the client, so wasting a couple k on a couple hundred rows may not be a big deal.

There is an AskTom thread that goes into more detail, particularly regarding the PL/SQL rules.

Justin Cave
How is the length of a VARCHAR2 entry stored? Is the string null-terminated (like in C) or is there a length value? Would this length determination lower the efficiency?
Steven
Both CHAR and VARCHAR2 columns have a length attribute in the block header. So you add 2 bytes of storage for the length field whether we're talking about a VARCHAR2(2000) or a CHAR(1).
Justin Cave