speed up the sqlite query (dont include thread model...) and add indices