I have a request as follows:
SELECT COUNT(Id) FROM Table
The table contains 33 million records - it contains the primary key in Id and other indexes.
The request takes 30 seconds.
The actual execution plan shows that it uses clustered index scanning.
We analyzed the table and found that it was not fragmented using the first query shown in this link: http://sqlserverpedia.com/wiki/Index_Maintenance .
Any ideas as to why this request is so slow and how to fix it.
Table definition:
CREATE TABLE [dbo].[DbConversation]( [ConversationID] [int] IDENTITY(1,1) NOT NULL, [ConversationGroupID] [int] NOT NULL, [InsideIP] [uniqueidentifier] NOT NULL, [OutsideIP] [uniqueidentifier] NOT NULL, [ServerPort] [int] NOT NULL, [BytesOutbound] [bigint] NOT NULL, [BytesInbound] [bigint] NOT NULL, [ServerOutside] [bit] NOT NULL, [LastFlowTime] [datetime] NOT NULL, [LastClientPort] [int] NOT NULL, [Protocol] [tinyint] NOT NULL, [TypeOfService] [tinyint] NOT NULL, CONSTRAINT [PK_Conversation_1] PRIMARY KEY CLUSTERED ( [ConversationID] 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
One thing that I noticed is that the database will grow in units of 1 MB.
This is a living system, so we limited ourselves to what we can play with - any ideas?
UPDATE:
OK - we improved performance in the question of interest by adding new non-clustered indexes to the corresponding columns so that it does not become critical.
SELECT COUNT is still slow - tried it with NOLOCK hints - no difference.
We all think that this is due to the fact that Autogrowth is set to 1 MB, and not to a larger number, but was surprised that it has this effect. Could MDF fragmentation on disk be a possible cause?