need performance to use aggregate N1QL

54 views Asked by At

I have a query like below

select sum(ARRAY_SUM(DailyCampaignUsage.`data`[*].cost)) revenue from 
Inheritx DailyCampaignUsage   WHERE DailyCampaignUsage._type='DailyCampaignUsage'

it is taking 12.3s

here count(ARRAY_SUM(DailyCampaignUsage.data[*].cost)) it is 51k

How I can improve it's performance ?? I have index like below

CREATE INDEX `abc` ON `Inheritx`(`_type`) USING GSI
1

There are 1 answers

1
geraldss On BEST ANSWER

Use a covering partial index.

CREATE INDEX idx_covering ON Inheritx(_type, data[*].cost) WHERE _type = 'DailyCampaignUsage';

select sum(ARRAY_SUM(DailyCampaignUsage.`data`[*].cost)) revenue
from Inheritx DailyCampaignUsage USE INDEX ( idx_covering )
WHERE DailyCampaignUsage._type='DailyCampaignUsage'