How can I get better performance in running the following query?
WITH cte AS
(
SELECT
CustomerId
,DocumentNumber
,IsCash
,date
FROM Table1
WHERE date > '2022-05-21' AND date < '2022-06-21'
)
SELECT DISTINCT
Customerid
FROM cte A
WHERE EXISTS
(
SELECT
1
FROM cte B ON A.DocumentNumber = B.DocumentNumber
WHERE B.IsCash = 1
)
AND A.IsCash = 0
Table1 stores data in daily basis and by this query I want to retrieve data of last month.
IsCash hast bit datatype, and because of bit datatype, I don't create index on it. Should I do that?
I just have nonclustered index on date and in execution plan index seek happen, but it is so slow.