Unix Technical Forum

Data type in audit record

This is a discussion on Data type in audit record within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I want my application to audit any data changes (update, insert, delete) made by the users. Rather than ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:47 PM
WombatDeath@gmail.com
 
Posts: n/a
Default Data type in audit record

Hi,

I want my application to audit any data changes (update, insert,
delete) made by the users. Rather than have an audit table mirroring
each user table, I'd prefer to have a generic structure which can log
anything. This is what I've come up with:

TABLE: audit_record
*audit_record_id (uniqueidentifier, auto-assign, PK) - unique
idenfiier of the audit record
table_name (varchar) - name of the table where the action (insert/
update/delete) was made
pk_value (varchar) - primary key of the changed record. If the PK
itself has changed, this will store the old value.
user_id (varchar) - user who changed the record
date (datetime) - date/time at which the change was made
action (int) - 0, 1 or 2 (insert, update, delete)

TABLE: audit_column
*audit_record_id (uniqueidentifier, composite PK) - FK to
cdb_audit_record table
*column_name (varchar, composite PK) - name of the column with changed
data
new_value (text?) - value after the change

So every column which changes has its new value logged individually in
the audit_column table. However, I'm not sure what data type the
new_value column should have. The obvious answer (to me) is text, as
that can handle any necessary data type with the appropriate
conversion (we don't store any binary data). However, this table is
going to grow to millions of records and I'm not sure what the
performance implications of a text column will be, particularly given
that the actual data stored in it will almost always be tiny.

Any thoughts/recommendations/criticism would be greatly appreciated.

Thanks
Alex

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:47 PM
WombatDeath@gmail.com
 
Posts: n/a
Default Re: Data type in audit record

Sorry for replying to myself - I forgot to state that I'm using SQL
Server 2000 standard edition.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:47 PM
Ed Murphy
 
Posts: n/a
Default Re: Data type in audit record

WombatDeath@gmail.com wrote:

> I want my application to audit any data changes (update, insert,
> delete) made by the users. Rather than have an audit table mirroring
> each user table, I'd prefer to have a generic structure which can log
> anything. This is what I've come up with:
>
> TABLE: audit_record
> *audit_record_id (uniqueidentifier, auto-assign, PK) - unique
> idenfiier of the audit record
> table_name (varchar) - name of the table where the action (insert/
> update/delete) was made
> pk_value (varchar) - primary key of the changed record. If the PK
> itself has changed, this will store the old value.
> user_id (varchar) - user who changed the record
> date (datetime) - date/time at which the change was made
> action (int) - 0, 1 or 2 (insert, update, delete)
>
> TABLE: audit_column
> *audit_record_id (uniqueidentifier, composite PK) - FK to
> cdb_audit_record table
> *column_name (varchar, composite PK) - name of the column with changed
> data
> new_value (text?) - value after the change
>
> So every column which changes has its new value logged individually in
> the audit_column table. However, I'm not sure what data type the
> new_value column should have. The obvious answer (to me) is text, as
> that can handle any necessary data type with the appropriate
> conversion (we don't store any binary data). However, this table is
> going to grow to millions of records and I'm not sure what the
> performance implications of a text column will be, particularly given
> that the actual data stored in it will almost always be tiny.
>
> Any thoughts/recommendations/criticism would be greatly appreciated.


Do you actually have anything (or any reasonable prospect of having
anything in future) for which NVARCHAR(4000) wouldn't be good enough?

Whatever you do, I strongly recommend keeping tabs on how quickly it
grows, showing that trend information to the client, and (1) narrow it
down to the tables that really need an audit trail and/or (2) come up
with a sane archive-and-purge schedule.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:47 PM
WombatDeath@gmail.com
 
Posts: n/a
Default Re: Data type in audit record

On Mar 30, 3:42 pm, Ed Murphy <emurph...@socal.rr.com> wrote:
> WombatDe...@gmail.com wrote:
> > I want my application to audit any data changes (update, insert,
> > delete) made by the users. Rather than have an audit table mirroring
> > each user table, I'd prefer to have a generic structure which can log
> > anything. This is what I've come up with:

>
> > TABLE: audit_record
> > *audit_record_id (uniqueidentifier, auto-assign, PK) - unique
> > idenfiier of the audit record
> > table_name (varchar) - name of the table where the action (insert/
> > update/delete) was made
> > pk_value (varchar) - primary key of the changed record. If the PK
> > itself has changed, this will store the old value.
> > user_id (varchar) - user who changed the record
> > date (datetime) - date/time at which the change was made
> > action (int) - 0, 1 or 2 (insert, update, delete)

>
> > TABLE: audit_column
> > *audit_record_id (uniqueidentifier, composite PK) - FK to
> > cdb_audit_record table
> > *column_name (varchar, composite PK) - name of the column with changed
> > data
> > new_value (text?) - value after the change

>
> > So every column which changes has its new value logged individually in
> > the audit_column table. However, I'm not sure what data type the
> > new_value column should have. The obvious answer (to me) is text, as
> > that can handle any necessary data type with the appropriate
> > conversion (we don't store any binary data). However, this table is
> > going to grow to millions of records and I'm not sure what the
> > performance implications of a text column will be, particularly given
> > that the actual data stored in it will almost always be tiny.

>
> > Any thoughts/recommendations/criticism would be greatly appreciated.

>
> Do you actually have anything (or any reasonable prospect of having
> anything in future) for which NVARCHAR(4000) wouldn't be good enough?
>
> Whatever you do, I strongly recommend keeping tabs on how quickly it
> grows, showing that trend information to the client, and (1) narrow it
> down to the tables that really need an audit trail and/or (2) come up
> with a sane archive-and-purge schedule.


Yeah, unfortunately we do have several tables with a column of type
text. These generally don't hold anything close to 4000 chars but
there's nothing actually preventing them from doing so. But...if
there's no tidier option I think I may just truncate to 4000 and be
done with it. We're not auditing to fulfil legal obligations or
anything nasty like that so I don't think it will be a problem.

Your point about maintenance is well taken. I've specified that the
application's auditing must be configurable on an entity-by-entity
basis, and every so often we'll archive away any old data for fast-
changing entities.

Thanks very much for your input!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 02:47 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Data type in audit record

(WombatDeath@gmail.com) writes:
> I want my application to audit any data changes (update, insert,
> delete) made by the users. Rather than have an audit table mirroring
> each user table, I'd prefer to have a generic structure which can log
> anything. This is what I've come up with:
>
> TABLE: audit_record
> *audit_record_id (uniqueidentifier, auto-assign, PK) - unique
> idenfiier of the audit record
> table_name (varchar) - name of the table where the action (insert/
> update/delete) was made
> pk_value (varchar) - primary key of the changed record. If the PK
> itself has changed, this will store the old value.
> user_id (varchar) - user who changed the record
> date (datetime) - date/time at which the change was made
> action (int) - 0, 1 or 2 (insert, update, delete)
>
> TABLE: audit_column
> *audit_record_id (uniqueidentifier, composite PK) - FK to
> cdb_audit_record table
> *column_name (varchar, composite PK) - name of the column with changed
> data
> new_value (text?) - value after the change
>
> So every column which changes has its new value logged individually in
> the audit_column table. However, I'm not sure what data type the
> new_value column should have. The obvious answer (to me) is text, as
> that can handle any necessary data type with the appropriate
> conversion (we don't store any binary data). However, this table is
> going to grow to millions of records and I'm not sure what the
> performance implications of a text column will be, particularly given
> that the actual data stored in it will almost always be tiny.


That is not going to be fun in SQL 2000. In SQL 2005 you could build a
generic audit solution on the xml data type.

I would recommend that you research the market for audit products. I
know for instance that ApexSQL has a something they call SQLAudit
if memory serves.


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

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 02:49 PM
--CELKO--
 
Posts: n/a
Default Re: Data type in audit record

>> I want my application to audit any data changes (update, insert, delete) made by the users. Rather than have an audit table mirroring each user table, I'd prefer to have a generic structure which can log anything. <<

Any chance you might post DDL instead of your personal pseudo-code?
And I hope you know that auto-numbering is not a relational key.
Finally, Google "EAV design flaw" for tens of thousands of words on
why this approach stinks. There is no such magical shape shifting
table in RDBMS. Data Versus metadata, etc.? Freshman database
course, 3rd week of the quarter?

While you might like this kludge your accountants and auditors will
not. NEVER keep audit trails on the same database or even the same
hardware as the database.

>> Any thoughts/recommendations/criticism would be greatly appreciated. <<


Look at third party tools that follow the law and get a basic dat
modeling book.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:47 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com