Monday, March 19, 2012

choosing the right datatype

It was hard to tell if this was the correct forum, but here goes:
My browser-driven Intranet App has 3 fields where users can type in a very
long description. It has a size of 5000, and is a varchar. People will
typically type in alphanumeric characters in it. (For the web-savvy, the
users types into a Textarea).
A co-worker tells me I should change the datatype from varchar to text. I
had it at varchar specifically because I understood that varchar will only
take up what disc space that it needs to. I asked him for an explanation of
why to go to text. He replied:
"There is an 8k limitation on a row when you use standard datatypes. You
have 3 columns with 5000 characters which pretty much doubles the amount of
allowed storage in a row. I'm surprised you haven't run into any problems.
Text datatypes are stored separately and allow up to 2GB."
Is this correct? Under my described scenario, which would be best?I would stick with Varchar. Varchar can hold up to 8,000 characters.
You're not storing more than 8,000 characters, so I don't see the point of
switching to a Text field. Also, if you use a text field, unless you
specify that you're storing the data "in row", it will only store a pointer
to the separate page or pages to access your string. You also can't
directly reference a text column in a WHERE clause. Unless you're storing
unusually large amount of characters (defined as 8,001+ characters) per row
in this column there's no real point to going with text.
"middletree" <middletree@.htomail.com> wrote in message
news:eDwvTxXKFHA.2688@.TK2MSFTNGP15.phx.gbl...
> It was hard to tell if this was the correct forum, but here goes:
> My browser-driven Intranet App has 3 fields where users can type in a very
> long description. It has a size of 5000, and is a varchar. People will
> typically type in alphanumeric characters in it. (For the web-savvy, the
> users types into a Textarea).
> A co-worker tells me I should change the datatype from varchar to text. I
> had it at varchar specifically because I understood that varchar will only
> take up what disc space that it needs to. I asked him for an explanation
> of
> why to go to text. He replied:
> "There is an 8k limitation on a row when you use standard datatypes. You
> have 3 columns with 5000 characters which pretty much doubles the amount
> of
> allowed storage in a row. I'm surprised you haven't run into any
> problems.
> Text datatypes are stored separately and allow up to 2GB."
> Is this correct? Under my described scenario, which would be best?
>|||He is right, but the limit for a row is 8060 and not 8k.
Example:
use northwind
go
create table t (
colA varchar(8000),
colB varchar(8000),
colC varchar(8000)
)
go
insert into t values(replicate('a', 8000), replicate('b', 8000),
replicate('c', 8000))
go
drop table t
go
Result:
Server: Msg 511, Level 16, State 1, Line 2
Cannot create a row of size 24015 which is greater than the allowable
maximum of 8060.
The statement has been terminated.
AMB
"middletree" wrote:

> It was hard to tell if this was the correct forum, but here goes:
> My browser-driven Intranet App has 3 fields where users can type in a very
> long description. It has a size of 5000, and is a varchar. People will
> typically type in alphanumeric characters in it. (For the web-savvy, the
> users types into a Textarea).
> A co-worker tells me I should change the datatype from varchar to text. I
> had it at varchar specifically because I understood that varchar will only
> take up what disc space that it needs to. I asked him for an explanation o
f
> why to go to text. He replied:
> "There is an 8k limitation on a row when you use standard datatypes. You
> have 3 columns with 5000 characters which pretty much doubles the amount o
f
> allowed storage in a row. I'm surprised you haven't run into any problems
.
> Text datatypes are stored separately and allow up to 2GB."
> Is this correct? Under my described scenario, which would be best?
>
>|||It is, generally, correct. However, there are perfomance and usage penaltie
s
for using Text datatype, that make it worthwhile t oavoid them if possible..
.
What are the three fields used for? An Alternative which MAY be worth
investigating, is putting these three fields in another table...
Assuming your existing Table has a PK called PKID,
Create Table Comments
(PKID Integer Not Null
WhichField TinyInt Not Null,
Description VarChar(5000),
CONSTRAINT Comment_PK PRIMARY KEY (PKId, WhichField)
)
-- (The WHichField column identifies which one of the web page's Description
fields this is for... 1,2, or 3)
"middletree" wrote:

> It was hard to tell if this was the correct forum, but here goes:
> My browser-driven Intranet App has 3 fields where users can type in a very
> long description. It has a size of 5000, and is a varchar. People will
> typically type in alphanumeric characters in it. (For the web-savvy, the
> users types into a Textarea).
> A co-worker tells me I should change the datatype from varchar to text. I
> had it at varchar specifically because I understood that varchar will only
> take up what disc space that it needs to. I asked him for an explanation o
f
> why to go to text. He replied:
> "There is an 8k limitation on a row when you use standard datatypes. You
> have 3 columns with 5000 characters which pretty much doubles the amount o
f
> allowed storage in a row. I'm surprised you haven't run into any problems
.
> Text datatypes are stored separately and allow up to 2GB."
> Is this correct? Under my described scenario, which would be best?
>
>|||Hey, 'tree. If it matters, "text" columns can be a pain to deal with in DW,
depending on what you're trying to do of course. The default recordset
options don't always work, you've got to watch your field order, etc.
I know it's not strictly database relevant, but I know you use DW, so it
might be relevant for you.
"middletree" <middletree@.htomail.com> wrote in message
news:eDwvTxXKFHA.2688@.TK2MSFTNGP15.phx.gbl...
> It was hard to tell if this was the correct forum, but here goes:
> My browser-driven Intranet App has 3 fields where users can type in a very
> long description. It has a size of 5000, and is a varchar. People will
> typically type in alphanumeric characters in it. (For the web-savvy, the
> users types into a Textarea).
> A co-worker tells me I should change the datatype from varchar to text. I
> had it at varchar specifically because I understood that varchar will only
> take up what disc space that it needs to. I asked him for an explanation
of
> why to go to text. He replied:
> "There is an 8k limitation on a row when you use standard datatypes. You
> have 3 columns with 5000 characters which pretty much doubles the amount
of
> allowed storage in a row. I'm surprised you haven't run into any
problems.
> Text datatypes are stored separately and allow up to 2GB."
> Is this correct? Under my described scenario, which would be best?
>|||Actually, I own DW, but tend to hand-code about 95% of my stuff. I hang out
at the DW forum to ask and answer questions about code, not DW. And to
discuss various theological and political topics ;)
Hope you haven't left the DW forums for good.
"CMBergin" <NoHarvestForYou@.NoSpam.org> wrote in message
news:OX2lEUYKFHA.436@.TK2MSFTNGP09.phx.gbl...
> Hey, 'tree. If it matters, "text" columns can be a pain to deal with in
DW,
> depending on what you're trying to do of course. The default recordset
> options don't always work, you've got to watch your field order, etc.
> I know it's not strictly database relevant, but I know you use DW, so it
> might be relevant for you.
> "middletree" <middletree@.htomail.com> wrote in message
> news:eDwvTxXKFHA.2688@.TK2MSFTNGP15.phx.gbl...
very
I
only
> of
You
> of
> problems.
>|||For a while, yes. Probably not for good though.
I'd elaborate, but the "on-topic" rules here seem quite a bit stricter.
"middletree" <middletree@.htomail.com> wrote in message
news:%23cpX5hZKFHA.572@.tk2msftngp13.phx.gbl...
> Actually, I own DW, but tend to hand-code about 95% of my stuff. I hang
out
> at the DW forum to ask and answer questions about code, not DW. And to
> discuss various theological and political topics ;)
> Hope you haven't left the DW forums for good.
>
> "CMBergin" <NoHarvestForYou@.NoSpam.org> wrote in message
> news:OX2lEUYKFHA.436@.TK2MSFTNGP09.phx.gbl...
> DW,
> very
the
text.
> I
> only
explanation
> You
amount
>

No comments:

Post a Comment