Reorder join for sqlite performance reasons