-1

I have 2 tables, TABLEA and TABLEB.

These contain pretty much the same data, around 90,000 records (30 fields)

There is a field, caller_id that is in both tables.

What I'm trying to do, is to find records in TABLEB that are in TABLEA.

I currently have the following:

SELECT * FROM TABLEB
WHERE caller_id IN (SELECT caller_id FROM TABLEA)

This query is incredibly slow (takes over 2 minutes), which isn't ideal

Is there a way I can possibly speed this up?

Thanks

UPDATE

1   SIMPLE  b   ALL NULL    NULL    NULL    NULL    11719   NULL
1   SIMPLE  a   ALL NULL    NULL    NULL    NULL    11719   NULL 
sipher_z
  • 1,221
  • 7
  • 25
  • 48

2 Answers2

0

Instead of subquery use join check for an index on caller_id

SELECT DISTINCT b.* FROM TABLEB b
INNER JOIN TABLEA a
 ON (a.caller_id =b.caller_id)

Also read SQL Joins Vs SQL Subqueries (Performance)

Community
  • 1
  • 1
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0

Your tables are not that big. Try this approach:

SELECT *
FROM TABLEB b
WHERE exists (SELECT 1 FROM TABLEA a where a.caller_id = b.caller_id);

And, be sure there is an index on TABLEA(caller_id):

create index tablea_caller_id on tablea(caller_id);

Some versions of MySQL do a poor job optimizing in with a subquery. The exists clause gets around this.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786