Jump to content

Using "IN"!


cherni99

Recommended Posts

Hi all,

 

I have the following DB structure with the following data...

 

Person A lives in Holland (stored in "person" table) and would like to visit Spain,Italy,Portugal (stored in "places" table)

Person B lives in Spain (stored in "person" table) and would like to visit Holland,Germany,Sweden (stored in "places" table)

 

I am running the query below:

 

select  count(*)

from    person pe1,

            places pl1,

            person pe2,

            places pl2

where  pe1.userid_db = pl1.userid_db

and      pe2.userid_db = pl2.userid_db

and      pe1.country_db in (pl2.countries_of_interest_db)

and      pe2.country_db in (pl1.countries_of_interest_db)

 

For now, there are approx 300 person records to scan and match. I realise that the "IN" is not very efficient so my question is "is there a way to optimise this query??"

 

Version: MySQL client version: 4.1.15

 

Thanks for your help!

C

 

Link to comment
Share on other sites

OK - I read the rules... ;)

 

Another thing to consider, there will be a join on person so the query will change to:

 

select  count(*)

from    person pe1,

            places pl1,

            person pe2,

            places pl2

where  pe1.userid_db = 1234567890  /* I will link the user id directly to pl1 but I'll leave this til the main reason of this post is discussed */

and      pe1.userid_db = pl1.userid_db

and      pe2.userid_db = pl2.userid_db

and      pe1.country_db in (pl2.countries_of_interest_db)

and      pe2.country_db in (pl1.countries_of_interest_db)

 

The query works...I'm just trying to make it more efficient.

 

The EXPLAIN...

id select_type table type possible_keys   key   key_len ref     rows Extra

1 SIMPLE         pl1         ALL         NULL           NULL        NULL         NULL    257 Using where

1 SIMPLE         pl2         ALL         NULL           NULL        NULL         NULL    257

1 SIMPLE         pe1         ALL         NULL           NULL        NULL         NULL    264 Using where

1 SIMPLE         pe2         ALL         NULL           NULL        NULL         NULL    264 Using where

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.