This bug has been reported to the proper authorities, and this blog post is not to dunk on the Summer Intern responsible for column store indexes, but to warn you to not do this, because I don’t know what it will break under the covers.
If you read the documentation for column store indexes, it says that column store indexes can’t be created on persisted computed columns.
And that’s true. If we step through this script, creating the column store index will fail.
/*Bye Felicia*/ DROP TABLE IF EXISTS dbo.cci; GO /*Hi Felicia*/ CREATE TABLE dbo.cci(id int, fk int, sd date, ed datetime); GO /*COMPUTER!*/ ALTER TABLE dbo.cci ADD cc AS id * 2; ALTER TABLE dbo.cci ALTER COLUMN cc ADD PERSISTED; GO /*Rats*/ CREATE CLUSTERED COLUMNSTORE INDEX c ON dbo.cci; GO
The error message we get is pretty clear:
Msg 35307, Level 16, State 1, Line 76 The statement failed because column 'cc' on table 'cci' is a computed column. Columnstore index cannot include a computed column implicitly or explicitly.
80 Blocks from Tiffany’s
However, if we change the order of things a little bit, we’ll find that we can add a persisted computed column to a table with a clustered column store index on it.
/*Bye Felicia*/ DROP TABLE IF EXISTS dbo.cci; GO /*Hi Felicia*/ CREATE TABLE dbo.cci(id int, fk int, sd date, ed datetime, INDEX c CLUSTERED COLUMNSTORE); GO /*???*/ ALTER TABLE dbo.cci ADD cc AS id * 2; ALTER TABLE dbo.cci ALTER COLUMN cc ADD PERSISTED; GO /*With data?*/ INSERT dbo.cci ( id, fk, sd, ed ) VALUES ( 10, 10, GETDATE(), GETDATE() ); /*yepyepyepyepyepyep*/ SELECT c.* FROM dbo.cci AS c /*Eh?*/ ALTER INDEX c ON dbo.cci REBUILD; ALTER INDEX c ON dbo.cci REORGANIZE; DBCC CHECKTABLE('dbo.cci');
And everything seems to work. However, if we drop the column store index, it can’t be re-created.
The key here is that the computed column is added, and then the persisted attribute is added second. That is where an error should be thrown.
But what do I know? I’m just a bouncer.
Again, I’m telling you not to do this. I’m telling you that it might break things in gruesome and horrible ways.
I don’t think that this is a preview of upcoming support for persisted computed columns in clustered column store indexes.
Thanks for reading!
p.s. Really, don’t do this
p.p.s. If you do this you’re asking for trouble
p.p.p.s. This was fixed in CU12
If this is the kind of SQL Server stuff you love learning about, you’ll love my training. I’m offering a 75% discount on to my blog readers if you click from here. I’m also available for consulting if you just don’t have time for that and need to solve performance problems quickly.