Table space used too heavy

时间:2016-10-20 13:13:50

标签: sql-server diskspace

I have a SQL Server database where space used is too heavy for a log table from a particular application.

Analizing disk usage appears that disk usage for primary key index is what is consuming space.

Primary key is consuming 200k for each row, which appears to be too much. On different instances of the same application the same primary key uses 20k to 40k for each row.

What can be done to reduce disk usage? It is a log table, we can rebuild/truncate etc. (actually it has been done already) There is no concern on keeping data.

CREATE TABLE [dbo].[logs](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [user] [varchar](50) NULL,
    [table] [varchar](50) NULL,
    [operation] [varchar](10) NULL,
    [query] [varchar](max) NULL,
    [date] [datetime] NOT NULL,
    [post] [varchar](max) NULL,
    [script_name] [varchar](50) NULL,
    [query_string] [varchar](1024) NULL,
    [user_agent] [varchar](200) NULL,
 CONSTRAINT [PK_logs] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SELECT
    OBJECT_NAME(i.OBJECT_ID) AS TableName,
    i.name AS IndexName,
    i.index_id AS IndexID,
    8 * SUM(a.used_pages) AS 'Indexsize(KB)',
    ck.total,
    8 * SUM(a.used_pages) / ck.total AS 'Indexsize(KB)/row'
FROM sys.indexes AS i
JOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id
JOIN sys.allocation_units AS a ON a.container_id = p.partition_id
JOIN (SELECT COUNT(*) AS total FROM logs) ck ON 1 = 1
WHERE OBJECT_NAME(i.OBJECT_ID) = 'logs'
GROUP BY i.OBJECT_ID,i.index_id,i.name,ck.total
ORDER BY OBJECT_NAME(i.OBJECT_ID),i.index_id

0 个答案:

没有答案