Este é um erro no SQL Server. Se uma coluna é excluída de uma tabela com um índice columnstore clusterizado e, em seguida, uma nova coluna é adicionada com o mesmo nome, ela parece estar usando a coluna excluída antiga para o predicado. Aqui está o MVCE:
Esse script começa com 10000linhas com statusIdof 1e statusId2of 5- depois solta a statusIDcoluna e renomeia statusId2para statusId. Portanto, no final, todas as linhas devem ter um statusIdde 5.
Mas a consulta a seguir atinge o índice não agrupado ...
select *
from example
where statusId = 1
and total <= @filter
and barcode = @barcode
and id2 = @id2
... e retorna 2linhas (com o selecionado statusIddiferente do implícito na WHEREcláusula) ...
+-------+---------+------+-------+----------+
| id | barcode | id2 | total | statusId |
+-------+---------+------+-------+----------+
| 5 | 5 | NULL | 5.00 | 5 |
| 10005 | 5 | NULL | 5.00 | 5 |
+-------+---------+------+-------+----------+
... enquanto este acessa o columnstore e retorna corretamente 0
select count(*)
from example
where statusId = 1
MVCE
/*Create table with clustered columnstore and non clustered rowstore*/
CREATE TABLE example
(
id INT IDENTITY(1, 1),
barcode CHAR(22),
id2 INT,
total DECIMAL(10,2),
statusId TINYINT,
statusId2 TINYINT,
INDEX cci_example CLUSTERED COLUMNSTORE,
INDEX ix_example (barcode, total)
);
/* Insert 10000 rows all with (statusId,statusId2) = (1,5) */
INSERT example
(barcode,
id2,
total,
statusId,
statusId2)
SELECT TOP (10000) barcode = row_number() OVER (ORDER BY @@spid),
id2 = NULL,
total = row_number() OVER (ORDER BY @@spid),
statusId = 1,
statusId2 = 5
FROM sys.all_columns c1, sys.all_columns c2;
ALTER TABLE example
DROP COLUMN statusid
/* Now have 10000 rows with statusId2 = 5 */
EXEC sys.sp_rename
@objname = N'dbo.example.statusId2',
@newname = 'statusId',
@objtype = 'COLUMN';
/* Now have 10000 rows with StatusID = 5 */
INSERT example
(barcode,
id2,
total,
statusId)
SELECT TOP (10000) barcode = row_number() OVER (ORDER BY @@spid),
id2 = NULL,
total = row_number() OVER (ORDER BY @@spid),
statusId = 5
FROM sys.all_columns c1, sys.all_columns c2;
/* Now have 20000 rows with StatusID = 5 */
DECLARE @filter DECIMAL = 5,
@barcode CHAR(22) = '5',
@id2 INT = NULL;
/*This returns 2 rows from the NCI*/
SELECT *
FROM example WITH (INDEX = ix_example)
WHERE statusId = 1
AND total <= @filter
AND barcode = @barcode
AND id2 = @id2;
/*This counts 0 rows from the Columnstore*/
SELECT COUNT(*)
FROM example
WHERE statusId = 1;
Também levantei um problema no portal de comentários do Azure :
E para qualquer pessoa que encontre isso, a reconstrução do Índice de armazenamento de colunas em cluster corrige o problema:
alter index cci_example on example rebuild
A reconstrução da CCI corrige apenas os dados existentes. Se novos registros forem adicionados, o problema surgirá novamente nesses registros; atualmente, a única correção conhecida para a tabela é recriá-la completamente.