Rewritten the query to allow mysql to use indexes

On a system with heavy logging, without this the login procedure took
too long.
Merge pull request #121 from rkojedzinszky/master

Rewrote the logState() sql query to allow mysql to use indexes