1

The Query takes too long to respond ~ 40 minutes.

`SELECT c.consumer_id FROM consumer c
WHERE  c.active_flag = 'Y'
      AND ( c.frequency = 'Q' )
      AND c.consumer_id NOT IN(
  SELECT consumer_id
  FROM (SELECT s.consumer_id
        FROM transactions s
        WHERE ( s.cycle='2016-Q-2' ) and s.active_flag = 'Y' AND s.status <> 'Door Locked')
    AS subquery)`

I've also tried with NOT EXISTS and LEFT JOIN / IS NULL version of above query same time for both of them.

consumer table have

  • consumer_id varchar(12)
  • active_flag varchar(6)
  • frequency varchar (2)
  • 130000 Rows with where clause
  • Total Rows 160000
  • unique index on consumer_id
  • index on active_flag

transaction table have

  • consumer_id varchar(12)
  • active_flag varchar(6)
  • status varchar (20)
  • cycle varchar (13)
  • 108000 Rows with where clause
  • Total Rows 270000
  • index on consumer_id status and cycle

Server config

-16GB RAM -8 Core Intel(R) Xeon(R) CPU E5-4640 v2 @ 2.20GHz -MySQL 5.6.35

Explain return SQL QUERY EXPLAIN

I hope this helps. Thanks in advance.

UPDATE 1

consumer and transactions have one-to-many relation so consumer_id will repeat for every cycle.

1
  • Could you please add your JOIN query as well?
    – marijnz0r
    Feb 22, 2017 at 14:58

4 Answers 4

0

using left join you will get 'NULL' values

try with inner join may be it help you

SELECT c.consumer_id FROM consumer c
JOIN 
    (SELECT s.consumer_id as S_ID 
     FROM transactions s
     WHERE (s.cycle='2016-Q-2') and s.active_flag = 'Y' AND s.status <> 
    'Door Locked')
ON s.S_ID <> c.consumer_id
WHERE  c.active_flag = 'Y' AND ( c.frequency = 'Q' )
0

Inner Join did the trick for me

SELECT DISTINCT c.consumer_id FROM consumer c
INNER JOIN
    (SELECT DISTINCT consumer_id as sid from transactions where consumer_id not in (
  select consumer_id from transactions
  where cycle = '2016-Q-2' AND active_flag = 'Y' AND status != 'Door Locked'
)) as s
ON s.sid = c.consumer_id
WHERE  c.active_flag = 'Y' AND ( c.frequency = 'Q' )

not sure if this the correct way but response time is down to ~ 700ms now.

not sure why but the response of queries from all the above answers gave all the consumer ids available in the transaction table.

0
0

Use right join or inner join to get your desired result. For speeding up queries, you should add indices to all the fields in the WHERE-clause. Good luck!

SELECT DISTINCT c.consumer_id FROM consumer c
INNER JOIN transactions s
ON s.cycle != '2016-Q-2' AND s.active_flag != 'Y' AND s.status = 'Door Locked' 
WHERE  c.active_flag = 'Y'
AND c.frequency = 'Q'
0

I am not sure what your data looks like so I am unsure if the following will work but you may want to play with the underlying logic of your query because what you are looking to do should be achievable without the use of subqueries.

As a general rule you always want to stay away from using subqueries as they are extremely inefficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.