I am using v12 server in an Azure SQL database and I have the following table:
CREATE TABLE [dbo].[AudienceNiches]( [Id] [bigint] IDENTITY(1,1) NOT NULL, [WebsiteId] [nvarchar](128) NOT NULL, [VisitorId] [nvarchar](128) NOT NULL, [VisitDate] [datetime] NOT NULL, [Interest] [nvarchar](50) NULL, [Gender] [float] NULL, [AgeFrom18To24] [float] NULL, [AgeFrom25To34] [float] NULL, [AgeFrom45To54] [float] NULL, [AgeFrom55To64] [float] NULL, [AgeFrom65Plus] [float] NULL, [AgeFrom35To44] [float] NULL, CONSTRAINT [PK_AudienceNiches] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) )
I execute this query: (UPDATED REQUEST)
`select a.interest, count(interest) from ( select visitorid, interest from audienceNiches WHERE WebsiteId = @websiteid AND VisitDate >= @startdate AND VisitDate <= @enddate group by visitorid, interest) as a group by a.interest`
And I have the following indexes (all ASC):
idx_WebsiteId_VisitDate_VisitorId idx_WebsiteId_VisitDate idx_VisitorId idx_Interest
The problem is that my query returns 18K aproximaly rows and takes 5 seconds, the whole table has 8.8M records, and if I expand the data a bit, the time increases, so what would be the best index for this query? What am I missing?