I have got 2 tables.
First table is for my objects (~1,000 rows).
Table Name: object
ID | name
1 | Dummy object 1
2 | Dummy object 2
3 | Dummy object 3
...
724 | Dummy object 724
...
etc.
Second table is for object events (~200,000 rows)
Table Name: events
ID | ID_OBJ | ID_MES | description | timestamp
1 | 3 | 3071 | Test event | 2017-01-28 12:00:01
2 | 4 | 3001 | Doors opened | 2017-01-28 13:00:04
3 | 3 | 3002 | Doors closed | 2017-01-28 13:33:45
...
etc...
When I'm using this query, it takes around 10 seconds to load data from tables:
SELECT
o.ID, o.name, e.timestamp AS last_event
FROM
object AS o
LEFT JOIN
events AS e
ON
e.ID_OBJ = o.ID
AND
(SELECT MAX(ID) FROM events WHERE events.ID_OBJ = o.ID AND ID_MES != 3071)
GROUP BY
o.ID
I need to get list of my objects, including the timestamp from last event, but I don't need include events with ID_MES 3071.
When I remove "AND ID_MES != 3071" from my query, it works really fast. Anyone know a solution for this problem to increase the speed?