Samantha J T Star
Samantha J T Star

Reputation: 32758

How efficient is it to access a single row in by an indexed GUID column in SQL Server?

I previously asked a question about having a clustered index on a GUID column. I received several replies and some useful comments. Here's my table:

CREATE TABLE [dbo].[Question] (
    [QuestionId]       INT              IDENTITY (1, 1) NOT NULL,
    [Text]             NVARCHAR (4000)  NULL,
    [GlobalId]         UNIQUEIDENTIFIER DEFAULT (newid()) NULL,
    CONSTRAINT [PK_Question] PRIMARY KEY CLUSTERED ([QuestionId] ASC),
);

I have another question related to this. If I create a non-clustered index on the GlobalId then would this be an efficient way to look up the row for a single one row look up only. I know it might seem strange to not be simply looking up by QuestionId but I would just like to know if for a table with say 4,000 rows, would it be much slower to do a single row look up by GlobalId ?

Upvotes: 1

Views: 31

Answers (1)

usr
usr

Reputation: 171178

Using a Guid instead of an int adds a rather small, constant factor to the time required to do the lookup.

The most important property of an index is that it allows O(log N) seek time. A scan requires O(N). This property still holds.

I'd worry more about space usage and fragmentation than about seek throughput.

Measure the difference.

Upvotes: 2

Related Questions