QUERY PLAN
Limit (cost=309.42..309.47 rows=20 width=51)
-> Sort (cost=309.42..309.77 rows=140 width=51)
Sort Key: s.created_at DESC
-> Subquery Scan on s (cost=302.89..305.69 rows=140 width=51)
-> HashAggregate (cost=302.89..304.29 rows=140 width=51)
Group Key: pip.description
-> Append (cost=188.49..302.19 rows=140 width=51)
-> HashAggregate (cost=188.49..189.61 rows=112 width=52)
Group Key: pip.description
-> Hash Left Join (cost=92.83..187.87 rows=123 width=52)
Hash Cond: (pip.purchase_invoice_id = pci.id)
-> Seq Scan on tbl_purchase_invoice_products pip (cost=0.00..94.72 rows=123 width=48)
Filter: (description ~~* '%2%'::text)
-> Hash (cost=60.70..60.70 rows=2570 width=12)
-> Seq Scan on tbl_purchase_contract_invoices pci (cost=0.00..60.70 rows=2570 width=12)
-> GroupAggregate (cost=110.69..111.18 rows=28 width=45)
Group Key: pip_1.description
-> Sort (cost=110.69..110.76 rows=29 width=45)
Sort Key: pip_1.description
-> Hash Left Join (cost=71.99..109.98 rows=29 width=45)
Hash Cond: (pip_1.sales_invoice_id = pci_1.id)
-> Seq Scan on tbl_sales_invoice_products pip_1 (cost=0.00..37.91 rows=29 width=41)
Filter: (description ~~* '%2%'::text)
-> Hash (cost=45.33..45.33 rows=2133 width=12)
-> Seq Scan on tbl_sales_contract_invoices pci_1 (cost=0.00..45.33 rows=2133 width=12)