Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql query with Left Join is too very slow

Query:

   select `r`.`id` as `id` 
     from `tbl_rls` as `r` 
left join `tblc_comment_manager` as `cm` on `cm`.`rlsc_id` != `r`.`id`

Both tables have 8k records but why is it very slow, taking 2-3 minutes and more sometimes?

OMG , this query makes mysql server down. Will get back to you peoples in a second :(

All peoples those suggested Indexing the columns are all Correct. Yeh the query i wrote was silly and buggy. Thanks correcting me.

like image 687
Arshdeep Avatar asked Jul 15 '10 16:07

Arshdeep


People also ask

How do you optimize SQL query with left joins?

Try creating an index and see if it is faster. For further optimisation, you can use SQL's EXPLAIN to see if your query is using indexes where it should be. Try http://www.dbtuna.com/article.asp?id=14 and http://www.devshed.com/c/a/MySQL/MySQL-Optimization-part-1/2/ for a bit of info on EXPLAIN.

Are joins slow in MySQL?

The problem is joins are relatively slow, especially over very large data sets, and if they are slow your website is slow. It takes a long time to get all those separate bits of information off disk and put them all together again.

IS LEFT join slower than join?

The LEFT JOIN query is slower than the INNER JOIN query because it's doing more work. From the EXPLAIN output, it looks like MySQL is doing nested loop join.

Are left joins faster than inner joins?

If the tables involved in the join operation are too small, say they have less than 10 records and the tables do not possess sufficient indexes to cover the query, in that case, the Left Join is generally faster than Inner Join. As you can see above, both the queries have returned the same result set.


3 Answers

Consider also indexing your tables. We're running multiple left joins on a 1million+ record table that doesn't take more than a second or two to return results.

like image 172
bpeterson76 Avatar answered Oct 07 '22 05:10

bpeterson76


Do you really need the != or is it meant to be =?

 select `r`.`id` as `id` from `tbl_rls` as `r` 
  left join `tblc_comment_manager` as `cm` 
on  `cm`.`rlsc_id`!=`r`.`id

This will select nearly the cartesian product of the 2 tables. (I guess around 60 million rows)

Edit: From the comment

yes it is " != " to match tbl_rls.id those are not in tblc_comment_manager

I think this is what you need if you want to use the outer join approach.

 select DISTINCT `r`.`id` as `id` from `tbl_rls` as `r` 
  left join `tblc_comment_manager` as `cm` 
on  `cm`.`rlsc_id`=`r`.`id
WHERE `cm`.`rlsc_id` IS NULL

Although my preference is usually

 select `r`.`id` as `id` 
 from `tbl_rls`
 as `r` 
 WHERE NOT EXISTS(
          SELECT * FROM `tblc_comment_manager` as `cm` 
          WHERE  `cm`.`rlsc_id`=`r`.`id)
like image 13
Martin Smith Avatar answered Oct 07 '22 06:10

Martin Smith


What do you want to select?

Use this query if you want to find tbl_rls records that haven't matching records in other table

select `r`.`id`
from `tbl_rls` as `r` 
left join `tblc_comment_manager` as `cm` 
    on  `cm`.`rlsc_id`=`r`.`id
where `cm`.`rlsc_id` IS NULL
like image 4
Naktibalda Avatar answered Oct 07 '22 06:10

Naktibalda