Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Performance / July 2008

Tip: Looking for answers? Try searching our database.

Storing Integer values in varchar datatype

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
doverj - 24 Jun 2008 20:08 GMT
This is a pretty basic question and I think I know the answer but want to
make sure I'm getting it right.  If you store a value with data integer into
a column that is datatype varchar, is there overhead?  I'm just wondering if
I have a generic table that stores various values with different datatypes
into a single column if I will have a big loss in performance.

Thanks.
TheSQLGuru - 24 Jun 2008 21:49 GMT
Yes, you do, especially for integer types.  They process VERY efficiently
through the CPUs in modern computers.  Storage size is another savings,
which translates into less IO which is very important for performance as
well.  Another issue is that you will (or SHOULD anyway) convert the field
to it's appropriate type before using it in a query, where clause, join,
etc.  Another performance hit.

Just don't do it.  Use the most appropriate/correct datatype at all times
(design, schema, code, etc).

Signature

Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net

> This is a pretty basic question and I think I know the answer but want to
> make sure I'm getting it right.  If you store a value with data integer
[quoted text clipped - 5 lines]
>
> Thanks.
doverj - 24 Jun 2008 22:04 GMT
Okay, I agree with you in general practice, however, what I'm wresting with
now has to do with auditing.  

Someone has suggested to me that for an audit that is populated through
triggers after data changes that I use multiple tables, one for each datatype.

To clarify, if TableA having data1 (int), data2 (string) is updated the
change from data1 would go to one table housing only integer values while
data2 changes would be stored in a second table.

I understand what they're getting at but it still seems less efficient then
having a single audit table with change values being all stored in a varchar.
If I'm not mistaken an integer is padded with one or two bytes when stored as
a varchar, but this still seems like it would be less then having multiple
tables where I'd likely need id's for each table which ultimately would be
more expensive.

Further, having it broken out this way seems to needlessly complicate things.

I apprecaite your input.

Thanks.

>Yes, you do, especially for integer types.  They process VERY efficiently
>through the CPUs in modern computers.  Storage size is another savings,
[quoted text clipped - 11 lines]
>>
>> Thanks.
Erland Sommarskog - 24 Jun 2008 23:36 GMT
> I understand what they're getting at but it still seems less efficient
> then having a single audit table with change values being all stored in
> a varchar. If I'm not mistaken an integer is padded with one or two
> bytes when stored as a varchar, but this still seems like it would be
> less then having multiple tables where I'd likely need id's for each
> table which ultimately would be more expensive.

Since an audit table is being written to far more often than being read
from, it seems like a good tradeoff to use general data types to store
all sorts of data. varchar is good for the keys. For the rest, you may be
interested in sql_variant or xml.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

TheSQLGuru - 28 Jun 2008 15:11 GMT
Why don't you make a straight-forward audit system (or purchase one such as
ApexSQL Audit or other third-party vendors) by having audit tables that
duplicate the base table structures and have the necessary additional fields
required such as username, change-date, etc?

Signature

Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net

> Okay, I agree with you in general practice, however, what I'm wresting
> with
[quoted text clipped - 41 lines]
>>>
>>> Thanks.
Robert Klemme - 05 Jul 2008 18:53 GMT
> Okay, I agree with you in general practice, however, what I'm wresting with
> now has to do with auditing.  
>
> Someone has suggested to me that for an audit that is populated through
> triggers after data changes that I use multiple tables, one for each datatype.

Well, you could use one table with multiple columns for the data.  You
can even ensure only one of them is not null via constraints.

I do not know your auditing requirements (i.e. what data do you need
especially for querying) but another option would be to have a table
with timestamp and varchar which contains a descriptive statement about
the change.  It all depends.

Kind regards

    robert
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.