Search code examples
databaseoracle-databasedatabase-designvarcharvarray

Drawback of large LIMIT of VAR-types


Is there any drawback (except allowing the value or array to grow too large) to setting the max integer size of a VARCHAR or VARRAY to a value significantly larger than actually necessary (or setting it to the max size allowed)?


Solution

  • If you're talking about a column in a database table, it is advisable to set the maximum length of a VARCHAR2 to the minimum length allowable by the requirements - like any other constraint, it helps to use these inbuilt features of the database to ensure only valid data gets saved (e.g. if a surname with 1000 characters is input I'm pretty sure that will be wrong, and perhaps highlight a bug in a program somewhere).

    On the PL/SQL side, it may interest you to know that there may be memory (PGA) usage differences depending what size you declare your strings in your PL/SQL programs. Internally, there is a threshold at 2000 bytes, where the PL/SQL engine switches between two different memory allocation schemes. e.g. the following declaration:

    DECLARE v VARCHAR2(2000); BEGIN...
    

    will allocate 2000 bytes in the user's memory area, whereas:

    DECLARE v VARCHAR2(2001); BEGIN...
    

    will only allocate memory when a value is assigned, and will only allocate as much memory as is required to hold the value assigned to it.

    Oracle forum: "VARCHAR2 space allocation"