I have this query what was discussion output in this thread: Mysql JOIN subquery
See current fiddle: http://sqlfiddle.com/#!2/e97cf/22
create table c_contact
(id INT,
name VARCHAR(20),
securityid INT
);
create table c_monitoring
(started DATE,
ended DATE DEFAULT NULL,
securityid INT
);
SELECT
c_contact.id,
c_contact.name,
c_contact.securityid
FROM c_contact
WHERE c_contact.securityid != ''
AND c_contact.securityid NOT IN
(select securityid
from c_monitoring
where ended is null
group by securityid
)
GROUP BY c_contact.id ;
How the hell I am going to optimize this query? I have 100.000 records in c_contact table and about 10.000 in c_monitoring table. Query takes > 30 secs with 127 result rows.
EDIT: Case was solved by indexing tables correctly.