sto usando un server v12 in Azure database SQL, e ho la seguente tabella:Come velocizzare query corrente con indice
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) )
sto che esegue la query: (query AGGIORNATO)
`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`
E ho le seguenti indexs (tutto ASC):
idx_WebsiteId_VisitDate_VisitorId idx_WebsiteId_VisitDate idx_VisitorId idx_Interest
Il problema è che la mia query restituisce righe di 18K aproximaly e impiega 5 secondi, l'intera tabella ha record di 8,8 milioni, e se espando un po 'i dati il tempo aumenta molto, quindi, quale sarebbe l'indice migliore per questa query ? Cosa mi manca?
Come ti aspetti che i dati risultanti siano simili? Non sono sicuro di come le funzioni aggregate facciano molto se stai raggruppando per VisitorID. –
Ho aggiornato la query in modo che sia possibile ottenere qualcosa di più semplice, desidero per interesse il numero totale di visitatori unici –
La chiave è WebsiteId? Vedo che stai filtrando il WebsiteId aggiungendo WebsiteId = @websiteid – User2012384