28 Oct 2013

How to Optimize your SQL Query?

How to Optimize your SQL Query?

  • Avoid "order by" or "distinct" in Query.
  • Join only on Primary keys or Unique keys of the table, join on all the primary keys or unique keys if the table has multiple primary keys or unique keys.
  • Don't use UDF in the select list or on the where clause because this udf will be executed on each row of the result set which could be costly. 
  • Avoid sub queries in the query, sub queries will hinder the performance as they may miss the indexes the underlying table is having which will make the engine take more time to identify the rows.
  • Try to move where conditions to inner joins if possible, because the where conditions will be applied on the joined set, instead if you add conditions to the joins then the joining subsets will reduce.
  • Use joins instead of  in/ not in clause.
  • TODO:How to join table data?Explain with an example

Disable Foreign key check in mysql

Disable Foreign key check in mysql:

SET foreign_key_checks = 1 

//usually this is added at the beginning of the   dump file for disabling the foreign key checks while restoring the dump.