0

I'm trying to optimize this query:

SELECT post_id 
FROM wp_postmeta
WHERE meta_key = 'passenger_group_id'
AND meta_value in (SELECT post_id 
                   FROM wp_postmeta 
                   WHERE meta_key = 'group_event' AND meta_value = '14608')
AND post_id IN (SELECT post_id 
                FROM wp_postmeta 
                WHERE meta_value='Cancelled')
AND post_id NOT IN (SELECT ID 
                    FROM wp_posts 
                    WHERE post_status='trash') 

Any help would be greatly appreciated.

2
  • 3
    What concrete database is this for? Oracle? MySQL? SQL Server? DB2? Something else entirely? Also: what do your tables look like - what columns (and datatypes!) do they have. How many rows are in those tables? What kind of indexes do you have on your tables? Once you're provided all of this - then maybe someone can help..... Commented Oct 15, 2014 at 17:10
  • @marc_s, those are tables in the Wordpress schema, so this must be MySQL. I've added tags. But your point is right -- people who ask questions should provide DDL for their tables. Commented Oct 15, 2014 at 17:36

1 Answer 1

1

you can use EXISTS clause alone and add NOT IN condition can be made NOT EXISTS

select wp1.post_id 
from wp_postmeta wp1
WHERE meta_key = 'passenger_group_id'
and exists  ( select 1 FROM
              wp_postmeta wp2
              where ( (wp2.meta_key = 'group_event' AND wp2.meta_value = '14608' ) or wp2.meta_value ='Cancelled' )
              and wp2.post_id = wp1.meta_value 

            )
and not exists ( select 1 from
                 wp_posts p
                 on p.id = wp1.post_id
                 and p.post_status = 'trash')
                )
Sign up to request clarification or add additional context in comments.

2 Comments

the not in references a different table than what you've done here. I don't see how this would work given wp_posts isn't in your query.
@FlawlessData, i think needs more changes, as second not in is from different table

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.