Snowy
Snowy

Reputation: 6132

SQL Server Filtered Index on PK Index/Constraint?

I have a table with zero rows now but will grow to 4-5 million in a year. biglogid is using a "COMB GUID" to keep the table/index from fragmenting. When rows are added, rowstate will have a value of 1. However, when the rows are not needed, they will be set to null (nothing can be deleted except by yearly cleanup, business rule). Since this means that there will eventually be more null than non-null columns, rowstate column is made Sparse.

A select from the table wold be "select logtext from biglog where biglogid=...... and rowstate is not null" to only get a value if it is active. If that is the only real query run against the table, is there value in making the PK a filtered index? And if so, what is the TSQL syntax for that?

Thanks.

CREATE TABLE biglog(
    biglogid [uniqueidentifier] NOT NULL,
    logtext [varchar](400) NOT NULL,
    rowstate [tinyint] SPARSE  NULL,
 CONSTRAINT biglogpk PRIMARY KEY CLUSTERED 
(
    biglogid ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 85) ON [PRIMARY]
) ON [PRIMARY]

Upvotes: 1

Views: 168

Answers (1)

Umut Derbentoğlu
Umut Derbentoğlu

Reputation: 1196

You can't define filtered index on PK. Is the following index useful for you?

CREATE NONCLUSTERED INDEX MyIndex
    ON biglog(logtext)
    WHERE rowstate IS NOT NULL ;

Upvotes: 3

Related Questions