473,471 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Varchar vs Char in 1st field of composite clustered index


Would it be OK to use varchar(5) instead of char(5) as the first field of a
composite clustered index?

My gut tells me that varchar would be a bad idea, but I am not finding much
information on this topic on this when I Google it.
Currently the field is Char(4), and there is a need to increase it to hold 5
characters.

TIA
Jul 23 '05 #1
2 2337
On Tue, 08 Feb 2005 03:17:31 GMT, Miss Livvy wrote:
Would it be OK to use varchar(5) instead of char(5) as the first field of a
composite clustered index?

My gut tells me that varchar would be a bad idea, but I am not finding much
information on this topic on this when I Google it.
Currently the field is Char(4), and there is a need to increase it to hold 5
characters.


Hi Miss Livvy,

It depends. What is the average length of the data? Does the column allow
NULLS? If so, how many of the rows will have NULL in this column?

Each varchar columns takes 2 additional bytes of storage to store the
actual length. If most values in this column will be 4 or 5 characters,
the storage size will increase to 6 or 7 bytes - you'll be better served
with using CHAR(5). If many values are 1 or 2 bytes, you'll save some
space by usign VARCHAR(5). Even more for empty strings (0 bytes + 2 bytes
for length) and NULLS (also 0 bytes + 2 for length -unused, in this case,
but still reserved).

Saving bytes, both in the leaf (data) pages of the clustered index and in
the root and intermediate pages (where only the indexed values are stored)
will result in more rows fitting in each page, thus less page reads and
better performance. Sure, the varying character of the column might
introduce some extra overhead for the processor - but with the difference
in speed between CPU and disk subsystem, this is irrelevant. I have yet to
witness a database application that leaves the disk susbsystem idle while
waiting for the CPU to finish it's computations. In my experience, the
bottleneck is always disk speed.

My advice: check the average number of bytes for this column, including
the 2 bytes overhead for length (+ 2 extra if this is the ONLY varying
length column in the table!). The pick the data type that takes the
smallest number of bytes.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #2
Hugo,
This is very helpful. Thanks.

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:lf********************************@4ax.com...
On Tue, 08 Feb 2005 03:17:31 GMT, Miss Livvy wrote:
Would it be OK to use varchar(5) instead of char(5) as the first field of acomposite clustered index?

My gut tells me that varchar would be a bad idea, but I am not finding muchinformation on this topic on this when I Google it.
Currently the field is Char(4), and there is a need to increase it to hold 5characters.


Hi Miss Livvy,

It depends. What is the average length of the data? Does the column allow
NULLS? If so, how many of the rows will have NULL in this column?

Each varchar columns takes 2 additional bytes of storage to store the
actual length. If most values in this column will be 4 or 5 characters,
the storage size will increase to 6 or 7 bytes - you'll be better served
with using CHAR(5). If many values are 1 or 2 bytes, you'll save some
space by usign VARCHAR(5). Even more for empty strings (0 bytes + 2 bytes
for length) and NULLS (also 0 bytes + 2 for length -unused, in this case,
but still reserved).

Saving bytes, both in the leaf (data) pages of the clustered index and in
the root and intermediate pages (where only the indexed values are stored)
will result in more rows fitting in each page, thus less page reads and
better performance. Sure, the varying character of the column might
introduce some extra overhead for the processor - but with the difference
in speed between CPU and disk subsystem, this is irrelevant. I have yet to
witness a database application that leaves the disk susbsystem idle while
waiting for the CPU to finish it's computations. In my experience, the
bottleneck is always disk speed.

My advice: check the average number of bytes for this column, including
the 2 bytes overhead for length (+ 2 extra if this is the ONLY varying
length column in the table!). The pick the data type that takes the
smallest number of bytes.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bill | last post by:
In an effort to improve the speed of queries against my main table, I'll be indexing a column whose data type is varchar(50). Would I be better off (better performance) if I changed the column's...
5
by: dmhendricks | last post by:
Greetings, I have a question. I work on some SQL2k/ASP.NET apps at work. My predacessor, who created the databases/tables seemed to have liked to use 'char' for all text fields. Is there a...
1
by: phronima | last post by:
Is that possible on SQL Server 2000 and onwards?
5
by: jim_geissman | last post by:
One table I manage has a clustered index, and it includes some varchar columns. When it is initially created, all the columns in the clustered index are populated, and then some of the longer...
22
by: jdokos | last post by:
Hello, I have a question about VARCHAR fields. Our application groups here are starting to use VARCHARs much more frequently. Even VARCHAR (2) to (9) length fields. They say this is because...
0
by: Medhatithi | last post by:
I have a table whose indexed column is a char(16) field. I am giving the schema. CREATE TABLE Emp(Empno char(16), Ename varchar(25), ...
4
by: Nick Chan | last post by:
all these while i've only used varchar for any string i heard from my ex-boss that char helps speed up searches. is that true? so there are these: 1) char with index 2) char without index...
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
2
by: ppuniversal | last post by:
Hi, I have a table in SQL Server 2000 with clustered index set on the Primary key. The primary key is a set of 5 fields(attributes). Now I want to add 1 more field into this clustered index as...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.