0

We are getting result proper but its taking too much time to execute.Can we optimise query which will take less time to execute.

SELECT c.client_id
FROM client_master c
INNER JOIN user_visit_record u ON u.client_id = c.client_id
WHERE c.dept_id ='1'
  AND c.branch_id='1'
  AND c.client_status IN('Hot','Warm','Cold','Quotation')
  AND u.next_followup_date < '2017-06-01'
  AND u.visit_id IN
    (SELECT max(visit_id)
     FROM user_visit_record
     WHERE client_id=c.client_id)

Thanks in advance.

5
  • 1
    Check indexes for visit_id field and client_id. Commented Jun 1, 2017 at 8:05
  • We didnt used indexing Commented Jun 1, 2017 at 8:11
  • Probably you get lot more speed if you create indexes for the relevant columns. Commented Jun 1, 2017 at 8:51
  • how can we do indexing in this query..can you please send.? Commented Jun 1, 2017 at 9:13
  • Index are at table level, not query level. You need to create index on the table. Example: CREATE INDEX user_visit_record_ix ON user_visit_record (visit_id);. For more info, check mysql manual for indexes here: dev.mysql.com/doc/refman/5.7/en/create-index.html Commented Jun 1, 2017 at 9:23

3 Answers 3

1

Here is the optimized query:

SELECT c.client_id 
FROM client_master c 
JOIN (SELECT * FROM user_visit_record u ORDER BY u.visit_id DESC) AS t ON t.client_id = c.client_id 
WHERE c.dept_id ='1'   
AND c.branch_id='1'   
AND c.client_status IN('Hot','Warm','Cold','Quotation')   
AND t.next_followup_date < '2017-06-01' 
GROUP BY t.clientid
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks You @Kruti Patel ..But this query retrieves wrong result.
Can we get another solutions for this.??
0

I have Used * in query, you can do is select only required columns,one way of optimising

And MySQL has several existing solutions, check this Article

SELECT c.client_id
    FROM 
        (SELECT *
        FROM client_master
        WHERE dept_id ='1'
                AND branch_id='1'
                AND IN('Hot','Warm','Cold','Quotation')) AS c
    INNER JOIN 
        (SELECT *
        FROM user_visit_record
        WHERE next_followup_date < '2017-06-01'
                AND visit_id IN 
            (SELECT max(visit_id)
            FROM user_visit_record
            WHERE client_id=c.client_id)) AS u
            ON u.client_id = c.client_id

Comments

0

Try this :

SELECT client_id FROM 
  (
    SELECT client_id 
      FROM client_master 
        WHERE dept_id ='1' AND branch_id='1' AND client_status IN('Hot','Warm','Cold','Quotation')
  ) AS c

  NATURAL JOIN

  (
    SELECT client_id
      FROM user_visit_record 
        INNER JOIN (
          SELECT client_id, max(visit_id) as max_visit 
            FROM user_visit_record
              GROUP BY client_id
        ) AS max_user_visit 
            ON (user_visit_record.client_id = max_user_visit.client_id) 
              AND (user_visit_record.visit_id = max_visit)
        WHERE next_followup_date < '2017-06-01'
  ) AS u

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.