views:

87

answers:

4

Hi folks, 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.

+2  A: 

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.

LukeH
+2  A: 

Prefix the queries with EXPLAIN to see how they are analysed.

Subqueries in the WHERE clause are best avoided, as they potentially need to be executed for every row. When a subquery is required, it's best used in the FROM clause, creating a derived table. E.g.

SELECT *
FROM
  some_table
  INNER JOIN (
    SELECT * FROM another_table
  ) derived_table ON some_table.id = derived_table.id
WHERE
  some_table.x = 'y';
Paul Annesley
There's an excellent Google Tech Talk video somewhere about MySQL and InnoDB, which explains this in more detail. I'm failing at Google right now, unfortunately.
Paul Annesley
+3  A: 

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

ovais.tariq
@ovais.tariq -You are absolutely right but when I saw time to execute these query then I found that first approach is giving fast result...is it because of join first it will join those fields and then using indexing it will take record so second approach is slow....That will be good if you give me your opinion on this..thanks
Rupeshit
mysql joins the tables from left to right, but the order of tables is determined by the query optimizer, if you run a explain you will see that the join order would be table3 join table2 join table1why is mysql doing so is because, rows can be reduced by filter table3 based on field = 'something' and hence there would be less rows to join table2 and table1 on.These things have to be taken in consideration when designing queries and schemas
ovais.tariq
thanks for the explanation Ovais.
Rupeshit
+3  A: 

Use join and create index on those columns which participate in comparison. Then use "Explain" to check performance :)

yogs