Turns out that for a really big DataI array (293318219 elements), the above query takes too long (especially the sort). I found the following to be more optimized, as it reduces the results significantly before the most time-consuming sort operation (using the first uniq(unpack(.., i)) pair).