Take a look at this sqlfiddle i made http://sqlfiddle.com/#!9/4b903/2/0. This is the simplified version of my database. I have a million records now in the history and the problem is that the query seems too slow. Sometimes it takes a minute to get the result. I'm not really good in this sql stuff. I guess there some columns that I need to index but Im not sure what are those.
