Comments on: How to optimize subqueries and joins in MySQL http://www.xaprb.com/blog/2006/04/30/how-to-optimize-subqueries-and-joins-in-mysql/ Stay curious! Fri, 10 May 2013 18:25:19 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Sam http://www.xaprb.com/blog/2006/04/30/how-to-optimize-subqueries-and-joins-in-mysql/#comment-32833 Sam Fri, 01 Mar 2013 09:20:56 +0000 http://www.xaprb.com/blog/?p=127#comment-32833 How to optimize following query?

set @feed_id = ”;
set @num = 1;

SELECT * FROM (SELECT `Product`.`id`, `Product`.`feed_id`, `Product`.`priority`, @num := if(@feed_id = Product.feed_id, @num + 1, 1) as row_number, @feed_id := Product.feed_id as dummy FROM `bigbridge_dfm_huge`.`products` AS `Product` WHERE `Product`.`category_id` = 49 AND `Product`.`priority` > 0 ORDER BY `Product`.`feed_id` ASC) as prods ORDER BY priority, row_number, feed_id LIMIT 0, 40

]]>
By: Mazen http://www.xaprb.com/blog/2006/04/30/how-to-optimize-subqueries-and-joins-in-mysql/#comment-20086 Mazen Sun, 17 Jun 2012 17:30:21 +0000 http://www.xaprb.com/blog/?p=127#comment-20086 The tip about how to force the inner query to execute first was a life saver, I can’t thank you enough.

I only just realized that this is quite an old post (from the comments)… amazing

]]>
By: Jamie Hamel-Smith http://www.xaprb.com/blog/2006/04/30/how-to-optimize-subqueries-and-joins-in-mysql/#comment-19586 Jamie Hamel-Smith Sun, 21 Aug 2011 06:03:58 +0000 http://www.xaprb.com/blog/?p=127#comment-19586 You sir are my new hero! I’m a self taught developer at a web company and your advice just got a query I’d been writing for a cron job from 235 seconds down to 2 seconds.

Nesting of the subquery is almost magical in the right situation. Life-saver!

]]>
By: Arpit http://www.xaprb.com/blog/2006/04/30/how-to-optimize-subqueries-and-joins-in-mysql/#comment-18986 Arpit Sat, 18 Dec 2010 02:36:31 +0000 http://www.xaprb.com/blog/?p=127#comment-18986 pls help me optimize this query..its already optimized 2 some extent..bt i want 2 c if it can get further..

SELECT e.emp_no as EMP_No,DEp.dept_name AS CurrentDEpt,e.first_nAme as FirstName,e.last_nAme as SurName,t.tiTle as CurrentStatus,e.genDer as Gender,s.salary,(s.salary/10) as BOnus

FROM (dEpARTMeNts dep) JOIN (dept_emp de) ON (de.dept_no = dep.depT_NO) JOIN (employees e) ON (de.emp_no = e.emP_NO) JOIN (titles t) ON (t.emp_NO = e.emp_no AND (t.to_date = ’9999-01-01′))
join (salaries s) on (s.emp_no = t.emp_no and s.to_date = ’9999-01-01′) having s.salary >100000
order by e.emp_no

]]>
By: nishi http://www.xaprb.com/blog/2006/04/30/how-to-optimize-subqueries-and-joins-in-mysql/#comment-18534 nishi Tue, 03 Aug 2010 15:54:05 +0000 http://www.xaprb.com/blog/?p=127#comment-18534 update tableA,tableB
set tableA.col2 = 1
where tableA.col1 = tableB.col1 and (tableB.col2 200);

Its taking couple of hours to execute the below query. Both tables have millions of entries and index is on col1, col2 and col3 of tableB.Also col1 in tableB is a foreign key referring to col1 of tableA
What can I do to make it run fast?
It will be of great help if you could help me with this asap

]]>