-1

I'm trying to run two MySQL queries on tables with no matching columns. The results of these two queries need to be matched up and present information only if a portion of one column matches a column from the other database. Here is what I have so far:

select data2, time_id from db.table1
where data2 in
(select right(dst,10) from db2.table2
where calldate like '2012-09-01%' and lastdata like <blocked for privacy>)
having (time_id between '1346475600' and '1346562000');

In my understanding, the subquery on lines 3-4 should be called first, correct? My issue with this statement is that it always times out. It just takes too long. Am I formatting this incorrectly? I'm sure an alternative would be to use a join statement, and any help in that direction would be great.

2
  • A table is not a database. Joining two databases is quite a bit harder.
    – GolezTrol
    Sep 11, 2012 at 21:18
  • Ok, then what do you interpret that I'm doing here? And where should I start?
    – Brad Rice
    Sep 11, 2012 at 22:01

1 Answer 1

0

You're not formatting it incorrectly, it's just a really bad query and requires the server to do some ridiculous calculating. Let's take some steps to do it right, really just one major thing and one minor.

  1. Don't try to join on a partial field. Add a column to table2 that will contain the data to join on. Run a query to populate that field. add a trigger to keep that field up to date. Not allowed to alert the schema? create a temp table and populate it accordingly and join to that instead.
  2. Don't use a like on a date time field, say call date BETWEEN <datetime 1> AND <datetime 2> instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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