DB2 database performance: How to Select Char or varchar

Source: Internet
Author: User
Recent project adjustments mainly involve changes to the length of database fields. In DB2, the only way to modify the length of a char field seems to be to delete the table and recreate it. Therefore, the question of which varchar and Char are used has been a headache. I read some information from the Internet and give a reply to this complicated question: SQL Server Performance Tuning for SQL Server developers
[Http://www.databasejournal.com/features/mssql/article.php/1466951] Choose the appropriate data types
While you might think that this topic shocould be under database design, I have decided to discuss it here because Transact-SQL is used to create the physical tables that were designed during the earlier database design stage.

Choosing the appropriate data types can affect how quickly SQL server can select, insert, update, and delete data, and choosing the most optimum data type is not always obvious. here are some suggestions you shoshould implement when creating physical SQL Server tables to help ensure optimum performance.

  • Always choose the smallest data type you need to hold the data you need to store in a column. for example, if all you are going to be storing in a column are the numbers 1 through 10, then the tinyint data type is more appropriate that the int data type. the same goes for char and varchar data types. don't specify more characters for character columns that you need. this allows SQL Server to store more rows in its data and index pages, costs the amount of I/O needed to read them. also, it forces CES The amount of data moved from the server to the client, which redirects network traffic and latency.

  • If the text data in a column varies greatly in length, use a varchar data type instead of a char data type. although the varchar data type has slightly more overhead than the char data type, the amount of space saved by using varchar over char on variable length columns can reduce I/O, improving overall SQL server performance.

  • Don't use the nvarchar or nchar data types unless you need to store 16-bit character (UNICODE) data. they take up twice as much space as varchar or char data types, increasing server I/O overhead.

  • If you need to store large strings of data, and they are less than 8,000 characters, use a varchar data type instead of a text data type. text data types have extra overhead that drag down performance.

  • If you have a column that is designed to hold only numbers, use a numeric data type, such as integer, instead of a varchar or char data type. numeric data types generally require less space to hold the same numeric value as does a character data type. this helps to reduce the size of the columns, and can boost performance when the columns is searched (where clause) or joined to another column.

Forum: Char or varchar?
[Http://archives.postgresql.org/pgsql-sql/2001-03/msg005?php]

There is * No * performance advantage of char (n) over varchar (n ).
If anything, there is a performance lossage due to extra disk I/O
(Because all those padding blanks take space, and time to read ).
 
My advice is to use char (n) when that semantically describes your data (ie, truly fixed-width data, like us postal codes), or varchar (N) when that semantically describes your data (ie, variable-width with a hard Upper Bound), or text when that semantically describes your data (ie, variable width with no specific upper bound ). worrying about performance differences is a waste of time, because there aren't any.

If the text data in a column varies greatly in length, use a varchar
Data Type instead of a char data type. although the varchar data type has slightly more overhead than the char data type, the amount of space saved by using varchar over char on variable length columns can reduce I/O, improving overall SQL server performance.

Several other people at the session who were familiar with
Performance effects of using char. varchar confirmed my advice. one person said his team was charged with deploying an application that used SQL Server. after deploying the application, the team found that it has med terribly. upon inspecting the database, team members discovered that all the fields were varchar. they changed the fields to Char, and the application now performs fine.

Here's the advice from IBM in from the DB2 admin guide SC96-9003) Copyright IBM Corp. 1982,199 9 "choosing Char or varchar: varchar
> Saves DASD space, but costs a 2-byte overhead for each value and the additional processing required for varying-length records. thus, char is preferable to varchar, unless the space saved by the use of varchar is significant. the savings are not significant if the maximum length is small or the lengths of the values do not have a significant variation. in general, do not define a column as varchar (n) unless n is at least 18. (consider, also, using data compression if your main concern is DASD savings. see "compressing data in a table space or partition" in topic 2.6.2 for more information .)

If you use varchar, do not specify a maximum length that is greater
Than necessary. Although varchar saves space in a table space, it
Does not save space in an index, because index records are padded with blanks to the maximum length. note participating ly the restrictions on columns of strings longer than 255 bytes; for example, they cannot be indexed. these restrictions are listed in Chapter 3 of DB2 SQL reference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.