Revert "Revert "Improve query time by an order of magnitude""

This reverts commit e92e48272a5bb98c725c3d42589c58a28dc35227.