Question

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.

Était-ce utile?

La solution

Your query has some group by problems (actually, you should not have any group by clauses), and you should convert it to a join:

SELECT
  c.id,
  c.name,
  c.securityid
FROM c_contact c
LEFT JOIN c_monitoring m ON m.securityid = c.securityid
  AND m.ended is null
WHERE c.securityid != ''
AND m.securityid IS NULL

See SQLFiddle

I also tidied up the query a little with aliases.

Autres conseils

Case was solved by indexing table fields

  • c_contact.securityid
  • c_monitoring.started
  • c_monitoring.ended
  • c_monitoring.securityid

Query takes now ~200ms

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top