0

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.

1
  • If you solved the problem, add the solution (the indexes used) as an answer. Commented Jan 14, 2014 at 10:47

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@AngularAddict Huh? I got 2 rows. Are you sure you've refreshed the page? This is the fiddle link: sqlfiddle.com/#!2/e97cf/34
Quess I had cached page. Works. Thanks for this.
0

Case was solved by indexing table fields

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

Query takes now ~200ms

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.