Our products' database contains an audit trail table:
CREATE TABLE gn_AuditTable( gn_ObjectId int NULL, gn_Action smallint NULL, gn_Time datetime NULL, gn_UserId int NULL, gn_Login varbinary(16) NULL, gn_ExtraObjectId int NULL, gn_ExtraInfo int NULL ) ON [PRIMARY]
We add rows to this table with a simple
insert statement - no stored procedures:
insert into gn_AuditTable ( gn_ObjectId,gn_Time,gn_UserId,gn_Login,gn_Action,gn_ExtraObjectId,gn_ExtraInfo ) values ( ?,?,?,?,?,?,? )
passing the values of the columns as parameters to the query; there are also no triggers.
The problem is that sometimes this insert is super-slow - taking in some cases more than 30 seconds and timing-out.
We have multiple installations of the same product, and we are experienceing the problem in some - but not all - of them.
The audit table can be quite big - up to some million of rows. The very slow insert happens with databases that have big tables (quite naturally) - but we have other databases with simliar number of rows that run fine, and even the databases experiencing the problem have it intermittently, in most cases the insert is reasonably fast (sub-second).
The table has 6 non-clustered, non-unique indexes:
Having all these indexes could cause the problem, but again, we have the same indexes everywhere and the problem only somewhere / sometimes.
We had the insert timing-out at least once on a test database with only one user, so the problem does not appear to be related to the overall load but to the insert and the table themselves.
We used to have a clustered index on
gn_ObjectId, but we got rid of it as an attempt to solve this problem.
gn_ObjectId is not sequential and so we thought that it is a poor choice for a clustered index, forcing SQL Server to re-order rows on inserts. Probably
gn_Time - that is always increasing - would be a better choice.
There is no primary key because there is no column or combination of columns that is 'naturally' unique - we would have to add an id column, and it seems just overhead.
Does anyone have any suggestion at what we could look at to diagnose the problem? What is an
insert doing that can take so much time - intermittently?