1
SET @acounts = 0;
SET @bcounts = 0;
SET @cur_year = '2015';
SET @userId = "m00087654";

SELECT *, (@acounts :=@acounts+1) as id FROM (
SELECT distinct  userId,cid  FROM `TABLE`
WHERE user_id = @userId AND ntype = 'parta'   AND date_from <>  date_to AND  YEAR(ndate) = @cur_year 
ORDER BY ndate, cid) AS a;

Time taken 1.22 sec

 SELECT *, (@acounts :=@acounts+1) as id FROM (
SELECT distinct  userId,cid  FROM `TABLE`
WHERE user_id = "m00087654" AND ntype = 'parta'   AND date_from <>  date_to AND  YEAR(ndate) = @cur_year 
ORDER BY ndate, cid) AS a;

Time taken 0.0022 sec

when i put user_id = @userId then it take more time comparing user_id = "m00087654"

Any clue why taking more time ?

3
  • What happens when you use SET @cur_year = 2015; (int instead of string)? Commented Feb 26, 2018 at 12:32
  • no differance for year. Only prob when using user_id = @userId Commented Feb 26, 2018 at 12:39
  • only prob in user_id = @userId Commented Feb 26, 2018 at 12:41

0

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.