0

I have two tables : table1 has 4lacs (400,000) records and other have 100 records.

I am trying following query :

SELECT DISTINCT t1.id as id 
FROM table1 t1 
JOIN table2 t2 ON t2.f_id = t1.id 
              AND  t1.action = 0;

I am using MYISAM Engine. t1.id and t2.f_id are same and indexed.

this query is taking 30 secs. Is there any way to make it faster.

3 Answers 3

4

Be sure to have an index on table1.id, table1.action and table2.f_id, and assuming no other indexes live on either table that are causing performance issues, this should be closer to optimal:

SELECT
     DISTINCT t1.id AS id
FROM
     table1 AS t1
     JOIN table2 AS t2 ON t2.f_id = t1.id
WHERE
     t1.action = 0
Sign up to request clarification or add additional context in comments.

Comments

0

Set one index on t1.action,t1.id and another one on t2.f_id.

Comments

0

The first step would be to add an index on table1 that covers id and action together.

Introducing distinct into the query is going to slow it down as well.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.