Performance Consideration: Distributing rows in multiple tables and concentrating all rows in a single table.
Hey.
I need to record information about each step that is performed in an application in SQL DB. There are certain tables, I want the log to be associated with: Product - should be logged when the product was created, etc. Order is the same as above Delivery is the same, etc. etc.
Data will often need to be retrieved.
I have few ideas on how to do this:
- Enter the log table that will contain the columns for all these tables, and then when I want to present the data in the user interface for a specific Product, select * from the log, where LogId = Product.ProductId. I know that it can be fun to have many colonies, but I have the feeling that the performance will be better. On the other hand, this table will have a huge number of rows.
- Have a lot of log tables for each type of log (ProductLogs, OrderLogs, etc.) I really don't like this idea, since it is incompatible and has many tables with the same structure, it makes no sense, but (?) It can be faster. when you are looking in a table with fewer rows (wrong?).
- According to statement No. 1, I could make a second many-to-one table that would have the LogId, TableNameId and RowId columns and would refer to the log line to many rows of the table in the database, than the UDF would have for retrieving data (for example, log id 234 belongs to the Customer table at CustomerId 345 and to the Product table, where productId = RowId); I think this is the best way to do this, but then again, maybe a huge number of lines, will this slow down the search? or here's how it should be done, what to say? ...
Example No. 3 in the above list:
CREATE TABLE [dbo].[Log]( [LogId] [int] IDENTITY(1,1) NOT NULL, [UserId] [int] NULL, [Description] [varchar](1024) NOT NULL, CONSTRAINT [PK_Log] PRIMARY KEY CLUSTERED ( [LogId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Log] WITH CHECK ADD CONSTRAINT [FK_Log_Table] FOREIGN KEY([UserId]) REFERENCES [dbo].[Table] ([TableId]) GO ALTER TABLE [dbo].[Log] CHECK CONSTRAINT [FK_Log_Table] --------------------------------------------------------------------- CREATE TABLE [dbo].[LogReference]( [LogId] [int] NOT NULL, [TableName] [varchar](32) NOT NULL, [RowId] [int] NOT NULL, CONSTRAINT [PK_LogReference] PRIMARY KEY CLUSTERED ( [LogId] ASC, [TableName] ASC, [RowId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO ALTER TABLE [dbo].[LogReference] WITH CHECK ADD CONSTRAINT [FK_LogReference_Log] FOREIGN KEY([LogId]) REFERENCES [dbo].[Log] ([LogId]) GO ALTER TABLE [dbo].[LogReference] CHECK CONSTRAINT [FK_LogReference_Log] --------------------------------------------------------------------- CREATE FUNCTION GetLog ( @TableName varchar(32), @RowId int ) RETURNS @Log TABLE ( LogId int not null, UserId int not null, Description varchar(1024) not null ) AS BEGIN INSERT INTO @Log SELECT [Log].LogId, [Log].UserId, [Log].Description FROM [Log] INNER JOIN LogReference ON [Log].LogId = LogReference.LogId WHERE (LogReference.TableName = @TableName) AND (LogReference.RowId = @RowId) RETURN END GO
source share