I have a table defined as follows:
CREATE TABLE [dbo].[T](
[FileID] [bigint] NOT NULL,
[Line] [nvarchar](250) NULL
) ON [PRIMARY]
FileID is a foreign key, and the table has > 7,000,000 records
Doing a select count(1) on this table causes a memory spike of 1.5GB+. Any workaround for this?
If you don’t need to block all your users while you’re getting a count, and can accept minor differences due to transactions currently occurring, then this is much better:
Otherwise Marc is right – put an index on the
FileIDcolumn and SQL Server will choose to scan that index instead of scanning the entire table.Is there really no index on this table? There are a few use cases where no clustered index makes sense, but I don’t suspect this is one of them. While creating a foreign key can help the optimizer, it does not create an index (though that is a common misconception).