Distinct uses SQL; on the back-end, it's a really simple expression that the server can evaluate quickly. Looking for distinct values in a single field with that many records should not take long at all.
Another suggestion: try limiting the fields and geometry coming in through the FeatureSet function. That will cut back on the potential data being transferred by the server, and usually speeds things up.
Can you try just returning the distinct values and seeing what it looks like?
var portal = Portal('portal')
var fs = FeatureSetByPortalItem(portal, "itemid", 0, ['fieldname'], false)
return Distinct(fs, "fieldname")
Also, any chance this layer is public? Testing the expression against the real data would be helpful. I can write a similar expression against my own data which works just fine.
- Josh Carlson
Kendall County GIS