Why is the length of oracle varchar2 limited?

 

The difference between char, varchar and varchar2 in oracle is as follows
     1. The length of CHAR is fixed, while the length of VARCHAR2 can be changed. For example, to store the string "abc", for CHAR (20), it means that the characters you store will occupy 20 bytes (including 17 null characters), The same VARCHAR2 (20) only occupies 3 bytes of length, 20 is only the maximum value, when the characters you store are less than 20, they are stored according to the actual length. 
     2. CHAR is slightly more efficient than VARCHAR2. 
     3. Currently VARCHAR is a synonym for VARCHAR2. The industry standard VARCHAR type can store empty strings, but oracle does not, although it reserves the right to do so in the future. Oracle has developed a data type VARCHAR2, which is not a standard VARCHAR. It changes the feature that varchar columns in the database can store empty strings to store NULL values. If you want backward compatibility, Oracle recommends using VARCHAR2 instead of VARCHAR.

When should I use CHAR and when should I use varchar2? 
    CHAR and VARCHAR2 are a pair of contradictory unity, and the two are complementary. 
    VARCHAR2 saves space than CHAR, and is slightly less efficient than CHAR, that is, in order to achieve efficiency, a certain amount of space must be sacrificed, which is We often say in database design 'exchange space for efficiency'. 
    Although VARCHAR2 saves space than CHAR, if a VARCHAR2 column is frequently modified, and the length of the modified data is different each time, this will cause the phenomenon of 'Row Migration', which will cause redundant I/O. It should be avoided in database design and adjustment. In this case, it is better to use CHAR instead of VARCHAR2.

    Spaces are also automatically filled in char, because when you insert a char field, spaces are automatically filled, but spaces are not deleted after select

 

There is a question here, since varchar2 is variable, what is the point of the length limit? 

 

I saw a view on the Internet: the following three main benefits:

1. Due to database limitations (see Logical Database Limits), the total length of each indexed field cannot exceed 75% * the database block size minus some overhead Length, due to limitations, the length of the field also limits the size of the index field:
SQL> create table x(
  2 a varchar2(2000),
  3 b varchar2(2000),
  4 c varchar2(2000),
  5 d varchar2(2000 ),
  6 e varchar2(2000));
table created.
SQL> create table y(
  2 a varchar2(10),
  3 b varchar2(10),
  4 c varchar2(10),
  5 d varchar2(10),
  6 e varchar2(10));
The table has been created.
SQL> create index y_idx on y(a,b,c,d); The
index has been created.
SQL> create index x_idx on x(a,b,c,d);
create index x_idx on x(a,b,c,d)                 
Error on line 1:
ORA-01450: maximum keyword length exceeded (6398)

 


2. The field length can play a certain role as a constraint. For example, a field length is required to be 10 bytes and must be set to varchar2(10).
3. If ARRAY FETCH is used, the memory required by the client to fetch data is based on the length of the defined field, so a large field will require a large amount of memory. For example, for 10 VARCHAR2(4000) fields, if I want to take 100 rows, then 4000*10*100 memory is needed, which is about 4M, but if it is defined as VARCHAR2(10), then 10*10*100 memory is needed.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326171112&siteId=291194637