Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which query will execute faster in MySql

I created indexing on my tables and then I fire same queries using two different ways:I run those queries on MySql but always got different execution time sometimes first one is faster and sometimes second..Thats why I want experts opinion on this.Queries are First one is

  select t1.field 
  from table1 as t1 
  where t1.field in (
      select t2.field 
      from table2 as t2 
      where t2.field in (
        select t3.field from table3 as t3 
          where t3.field='something'))

And Second using join as

 select t1.field 
 from table1 as t1, 
      table2 as t2,
      table3 as t3 
 where t1.field = t2.field 
  and t2.field = t3.field 
  and t3.field='something'

So can anyone one tell me which will give me high performance and why as my DB is too big....So I wanted to know which is the better way to write such queries in MySql.

like image 900
Rupeshit Avatar asked Jun 03 '26 16:06

Rupeshit


2 Answers

the second approach is going to be faster than the first one,.

proper indexing would be having indexes on t1(field), t2(field), t3(field)

when you use explain for the first approach you will see that mysql will create two derived tables for both the two subqueries

while in the second approach the rows examined by mysql are going to be far less

like image 105
ovais.tariq Avatar answered Jun 05 '26 06:06

ovais.tariq


Only you can really answer your question, because only you have access to the exact combination of hardware, software, schema, indexes, data etc that your queries will actually be running against.

Take a look at the MySQL EXPLAIN statement.

like image 30
LukeH Avatar answered Jun 05 '26 06:06

LukeH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!