PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > Char /Varchar
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Char /Varchar

Réponse
 
LinkBack Outils de la discussion
Vieux 12/09/2008, 17h49   #1
Iain Sharp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Char /Varchar


We are looking at changing our database structure, which predominantly
uses char to use varchar, if this is deemed appropriate.

char stores 'extra' data to the tune of the trailing spaces in the
fields, is this correct?

varchar stored 'extra' data to the tune of the length of the data in
the field?

So, there is presumably one breakpoint where the likely number of
trailing spaces is > 4/8 bytes (for the storage of the length?)

i.e. char(5) best left to itself, and char(255) best converted to
varchar(255)?

Are there other performance/storage considerations?

Is this liable to be worth the effort?

Regards,

Iain
  Réponse avec citation
Vieux 12/09/2008, 18h38   #2
Kalen Delaney
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Char /Varchar

Hi Iain

You are close.But, trailing spaces have really nothing to do with it.
Consider only the actual data the column will hold.

CHAR columns do not have to have trailing spaces to use a lot of space. A
CHAR can have 'abc' and nothing else, but if it is stored in a CHAR(100)
column, SQL Server will still use the whole width. A VARCHAR will only use
the space for the data it contains, which might actually include some
trailing spaces, depending on what you inserted.

The 'extra space' needed for VARCHAR is two extra bytes per row for each
VARCHAR column. Since the length of the column is different in each row,
each row has to keep track of the length. So it is worse that worthless to
create a VARCHAR(1) or VARCHAR(2).

Usually, if the maximum size is 10 bytes or less, VARCHAR is not worth the
trouble because in addition to the two extra bytes, there is extra CPU time
to figure out how long each column is.

If the maximum length is larger, you really need to know your data. How many
characters is each column likely to hold? The rule of thumb I use is that if
80% of the rows will use 80% or more of the maximum length, I make the
column CHAR. So if you defined a column to be VARCHAR(100), but if most of
your data is 80-100 bytes, I'd go with CHAR.

--
HTH
Kalen Delaney, SQL Server MVP
www.InsideSQLServer.com
www.SQLTuners.com


"Iain Sharp" <iains@pciltd.co.uk> wrote in message
news:ni3lc4pm76a87771k3uia7b2464jh3uv8t@4ax.com...
>
> We are looking at changing our database structure, which predominantly
> uses char to use varchar, if this is deemed appropriate.
>
> char stores 'extra' data to the tune of the trailing spaces in the
> fields, is this correct?
>
> varchar stored 'extra' data to the tune of the length of the data in
> the field?
>
> So, there is presumably one breakpoint where the likely number of
> trailing spaces is > 4/8 bytes (for the storage of the length?)
>
> i.e. char(5) best left to itself, and char(255) best converted to
> varchar(255)?
>
> Are there other performance/storage considerations?
>
> Is this liable to be worth the effort?
>
> Regards,
>
> Iain



  Réponse avec citation
Vieux 13/09/2008, 13h32   #3
Gert-Jan Strik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Char /Varchar

Iain Sharp wrote:
>
> We are looking at changing our database structure, which predominantly
> uses char to use varchar, if this is deemed appropriate.
>
> char stores 'extra' data to the tune of the trailing spaces in the
> fields, is this correct?
>
> varchar stored 'extra' data to the tune of the length of the data in
> the field?
>
> So, there is presumably one breakpoint where the likely number of
> trailing spaces is > 4/8 bytes (for the storage of the length?)
>
> i.e. char(5) best left to itself, and char(255) best converted to
> varchar(255)?
>
> Are there other performance/storage considerations?
>
> Is this liable to be worth the effort?
>
> Regards,
>
> Iain


More rows on a page will increase performance, so yes, it could very
well be worth the effort. This also applies to index pages.

Another consideration is the behavior with respect to Inserts, Update
and Deletes. Let's take Updates. If a CHAR's value is changed, it can be
replaced in place. If a VARCHAR's short value is replaced with a larger
value, then the row might no longer fit on the page, causing a page
split, and with that potentially worse performance. This will also
happen to the column's index (when applicable).

If you have few Updates of the CHAR columns, then I would not worry
about that.

--
Gert-Jan
SQL Server MVP
  Réponse avec citation
Vieux 15/09/2008, 10h54   #4
Iain Sharp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Char /Varchar


Thanks for both these responses. Time to go check out existing
database usage for the columns concerned.

Iain

On Sat, 13 Sep 2008 13:32:20 +0200, Gert-Jan Strik
<sorry@toomuchspamalready.nl> wrote:

>Iain Sharp wrote:
>>
>> We are looking at changing our database structure, which predominantly
>> uses char to use varchar, if this is deemed appropriate.
>>
>> char stores 'extra' data to the tune of the trailing spaces in the
>> fields, is this correct?
>>
>> varchar stored 'extra' data to the tune of the length of the data in
>> the field?
>>
>> So, there is presumably one breakpoint where the likely number of
>> trailing spaces is > 4/8 bytes (for the storage of the length?)
>>
>> i.e. char(5) best left to itself, and char(255) best converted to
>> varchar(255)?
>>
>> Are there other performance/storage considerations?
>>
>> Is this liable to be worth the effort?
>>
>> Regards,
>>
>> Iain

>
>More rows on a page will increase performance, so yes, it could very
>well be worth the effort. This also applies to index pages.
>
>Another consideration is the behavior with respect to Inserts, Update
>and Deletes. Let's take Updates. If a CHAR's value is changed, it can be
>replaced in place. If a VARCHAR's short value is replaced with a larger
>value, then the row might no longer fit on the page, causing a page
>split, and with that potentially worse performance. This will also
>happen to the column's index (when applicable).
>
>If you have few Updates of the CHAR columns, then I would not worry
>about that.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10568 seconds with 12 queries