Hi Couchbase Gurus,
Need your help badly, for optimizing my Analytics Query.
Information:
So, I have these number of documents in my bucket, which I use in my query:
product-aggregate
: 1959780
product
: 651
// unique product documents.
[ { Id: product::0010, name: ProductName01 productCode: 0010, type: product }, ... ]
// product-aggregate document
[ { product: product::0010, qtyShipped: 5, sales: 65.89, type: ‘product-aggregate’ }, { product: product::0010, qtyShipped: 15, sales: 75.50, type: ‘product-aggregate’ }, { product: product::0011, qtyShipped: 11, sales: 12.45, type: ‘product-aggregate’ }, ... ]
Query Execution Time:
The Query:
SELECT META(p).id, p.name, p.productCode, p.rank, p.unit, p.price, totals.totalSales AS totalSales, totals.totalShipped AS totalShipped FROM `company_analytics`.`products` AS p LET totals = ( SELECT SUM(pa.sales) AS totalSales, SUM(pa.qtyShipped) AS totalShipped FROM `company_analytics`.`product-aggregates` AS pa WHERE META(p).id = pa.product AND pa.periodAt BETWEEN '2020-01-01T06:00:00.000Z' AND '2020-08-28T23:25:53.178Z' AND pa.entityId IN [ "territory::0000", "territory::0001", "territory::0002", "territory::0003", "territory::0004", "territory::0005", … 40+ more ] )[0] ORDER BY totalShipped DESC LIMIT 50