Choosing Column Data Type
http://www.itpub.net/thread-1938117-1-1.html
1. CHAR vs. VARCHAR2
It's always a good idea to choose VARCHAR2, because CHAR appends spaces to the string shorter than
defined length wasting storage. There's no storage or performance advantage in using CHAR. The only
"benefit" of CHAR is to save typing. If you have many VARCHAR2 fields and a few string types of
length 1 (e.g. columns sex, employee_status), CHAR(1) stands out when you describe the table in
Sqlplus. Since it's not possible to waste storage for CHAR(1), that's the only time it makes sense
to use CHAR. Benefits are as trivial as that. (Why is CHAR a data type in Oracle at all? Maybe
required by some SQL or RDBMS standard.)
2. CLOB vs. VARCHAR2
LOB is more cumbersome to manipulate than plain VARCHAR2, and (maybe up to the recent SecureFiles
LOBs) less performant. Its internal storage and management mechanism is quite different from those
of other data types. Unless your data is definitely longer than 4000 bytes, use VARCHAR2.
3. NUMBER precision and scale
If there's no requirement on precision or scale, there's no need to specify that. It's not uncommon
to see developers constantly requesting for expansion of the precision of NUMBER (and width of
VARCHAR2 as well). It's an unnecessary hassle.
4. LONG, LONG RAW
There was report of better performance for LONG/LONG RAW than CLOB/BLOB many years ago. If it was
true, it probably no longer is, especially when you decide to use SecureFiles LOB, which becomes
default in 12c. Due to numerous restrictions on LONG/LONG RAW, never use them. Consider LOB, and if
the data is text and shorter than 4000 bytes, use VARCHAR2.
5. BINARY_DOUBLE, BINARY_FLOAT
It's possible to get better performance by storing data as these types if your app does floating
point calculation a lot.
6. DATE vs. TIMESTAMP
If your data must be accurate down to millisecond, use TIMESTAMP. Otherwise use DATE. The storage
for TIMESTAMP is 11 bytes (when millisecond part of the data is not 0 i.e. is present) while that
for DATE is 7.
Appendix: Store data in files or database?
Store file data on the file system. A relational database is best used to store relational data,
including metadata about the files, so that they can be joined and cross-referenced to other data.
The data in files don't belong to this category. Secondly, RDBMS backup has a higher standard on
consistency and is generally more resource intensive. Leaving files on the file system makes
backup easier, and restore way much easier and faster. Lastly, LOB is more cumbersome to manipulate
than plain varchar2 and number types, and (maybe up to the recent SecureFiles LOBs) less performant.
The benefit of storing file data inside the database is that you can bring the data anywhere in one
go, and in case of RAC, you don't need to worry about which node is used to store the files, if
they must be on the database server. But the first benefit is too trivial. The second is
irrelevant in most cases since the files are often accessed through a Web server, the natural
location for those files.
1. CHAR vs. VARCHAR2
It's always a good idea to choose VARCHAR2, because CHAR appends spaces to the string shorter than
defined length wasting storage. There's no storage or performance advantage in using CHAR. The only
"benefit" of CHAR is to save typing. If you have many VARCHAR2 fields and a few string types of
length 1 (e.g. columns sex, employee_status), CHAR(1) stands out when you describe the table in
Sqlplus. Since it's not possible to waste storage for CHAR(1), that's the only time it makes sense
to use CHAR. Benefits are as trivial as that. (Why is CHAR a data type in Oracle at all? Maybe
required by some SQL or RDBMS standard.)
2. CLOB vs. VARCHAR2
LOB is more cumbersome to manipulate than plain VARCHAR2, and (maybe up to the recent SecureFiles
LOBs) less performant. Its internal storage and management mechanism is quite different from those
of other data types. Unless your data is definitely longer than 4000 bytes, use VARCHAR2.
3. NUMBER precision and scale
If there's no requirement on precision or scale, there's no need to specify that. It's not uncommon
to see developers constantly requesting for expansion of the precision of NUMBER (and width of
VARCHAR2 as well). It's an unnecessary hassle.
4. LONG, LONG RAW
There was report of better performance for LONG/LONG RAW than CLOB/BLOB many years ago. If it was
true, it probably no longer is, especially when you decide to use SecureFiles LOB, which becomes
default in 12c. Due to numerous restrictions on LONG/LONG RAW, never use them. Consider LOB, and if
the data is text and shorter than 4000 bytes, use VARCHAR2.
5. BINARY_DOUBLE, BINARY_FLOAT
It's possible to get better performance by storing data as these types if your app does floating
point calculation a lot.
6. DATE vs. TIMESTAMP
If your data must be accurate down to millisecond, use TIMESTAMP. Otherwise use DATE. The storage
for TIMESTAMP is 11 bytes (when millisecond part of the data is not 0 i.e. is present) while that
for DATE is 7.
Appendix: Store data in files or database?
Store file data on the file system. A relational database is best used to store relational data,
including metadata about the files, so that they can be joined and cross-referenced to other data.
The data in files don't belong to this category. Secondly, RDBMS backup has a higher standard on
consistency and is generally more resource intensive. Leaving files on the file system makes
backup easier, and restore way much easier and faster. Lastly, LOB is more cumbersome to manipulate
than plain varchar2 and number types, and (maybe up to the recent SecureFiles LOBs) less performant.
The benefit of storing file data inside the database is that you can bring the data anywhere in one
go, and in case of RAC, you don't need to worry about which node is used to store the files, if
they must be on the database server. But the first benefit is too trivial. The second is
irrelevant in most cases since the files are often accessed through a Web server, the natural
location for those files.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24237320/viewspace-1813928/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20220708]PLSQL – choosing the BEST data type.txtSQL
- ORA-14060: data type or length of a table partitioning column may not be changed
- HITSC_4_Data Type and Type Checking
- Rust的變數型別__Data typeRust變數型別
- SP2-0678: Column or attribute type can not be displayed by SQL*PlusSQL
- Choosing Composite IndexesIndex
- HITSC_6_Abstract Data Type (ADT)
- Default Data Type Mappings Used by Oracle SQL DeveloperAPPOracleSQLDeveloper
- Choosing between HttpSession and Stateful session beanHTTPSessionBean
- Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0Framework
- mysql5.5列資料型別data type_測試MySql資料型別
- Choosing a driver model for developing a USB client driverdevclient
- python json讀取資料:ValueError: Extra data: line 77 column 2 - line 16485 column 1 (char 1159 - 227243)PythonJSONError
- SAP UI5 資料型別(data type) 學習筆記UI資料型別筆記
- oracle資料型別data type與儲存空間大小(一)Oracle資料型別
- oracle資料型別data type與儲存空間大小(二)Oracle資料型別
- Error: no such columnError
- a type "complete deletion of data target contents process cannot follow process" Excute Infopacakge
- Codeforces Round #246 (Div. 2) A. Choosing Teams
- Column Monitoring
- Index column size too large. The maximum column size is 767 bytesIndex
- Hibernate 註解@Column(nullable = false) 和 @Column(unique=true)NullFalse
- Oracle 12C 新特性之擴充套件資料型別(extended data type)Oracle套件資料型別
- java 反射 報錯:Attempt to get java.lang.Integer field "..." with illegal data type conversion to intJava反射
- variable: Type 與 Type variable
- cannot convert (type interface {}) to type int: need type assertion
- CSS column-spanCSS
- CSS column-gapCSS
- CSS column-ruleCSS
- Oracle Column Group StatisticsOracle
- MAX or MIN of Indexed ColumnIndex
- [PT]Column Histogram StatisticsHistogram
- Codeforces 219D Choosing Capital for Treeland:Tree dpAPI
- Index column size too large. The maximum column size is 767 bytes.Index
- Value Type vs Reference Type in SwiftSwift
- CSS column-widthCSS
- 1709 - Index column size too large. The maximum column size is 767 bytes.Index
- [Err] 1709 - Index column size too large. The maximum column size is 767 bytes.Index