query optimization - MySQL: "Sending data" vs "Copying to tmp table" -
i have 2 queries same thing , give same result set, writing differently. looking @ profile, take same time execute: around 0.075 seconds
the fascinating thing query#1 takes of entire process execution time in state "copying tmp table". query#2 takes entire process execution time in state "sending data".
so, question is:
what state better mysql query spent of time, "sending data" or "copying tmp table"?
query#1 (spends execution time in state "copying tmp table")
select ac.album_category_id, ac.name, ac2.album_category_id, ac2.name, ac2.description, t2.total album_category ac join album_category ac2 on ac2.parent_album_category_id = ac.album_category_id join ( select a.album_category_id cat_id, sum(result.count_image) total album join ( select ifnull(a.parent_album_id, a.album_id) p_album_id, count(ai.image_id) count_image album_image ai join album on a.album_id = ai.album_id join image on i.image_id = ai.image_id i.is_listed = 1 group p_album_id ) result on result.p_album_id = a.album_id group a.album_category_id ) t2 on t2.cat_id = ac2.album_category_id order ac.position, ac2.position query#2 (spends execution time in state "sending data")
select ac.album_category_id, ac.name, ac2.album_category_id, ac2.name, ac2.description, t3.total album_category ac join album_category ac2 on ac2.parent_album_category_id = ac.album_category_id join ( select a2.album_category_id cat_id, sum(t2.sum_image) total album a2 join ( select ifnull(a.parent_album_id, a.album_id) p_album_id, sum(t1.count_image) sum_image album join ( select ai.album_id album_id, count(ai.image_id) count_image album_image ai join image on i.image_id = ai.image_id i.is_listed = 1 group ai.album_id ) t1 on t1.album_id = a.album_id group p_album_id ) t2 on t2.p_album_id = a2.album_id group a2.album_category_id ) t3 on t3.cat_id = ac2.album_category_id order ac.position, ac2.position edit
as per jkavalik request, i've added explain. i'm not sure how add in text, i've added images instead. hope that's ok.
explain query #1
copying tmp table: server copying temporary table in memory.
sending data: thread reading , processing rows select statement, , sending data client. because operations occurring during state tend perform large amounts of disk access (reads), longest-running state on lifetime of given query.
http://dev.mysql.com/doc/refman/5.6/en/general-thread-states.html
sending data happen on queries query state change fast on quick queries won't notice it. however, since noticing it, there may lot of io going on @ time. when run query, in terminal window @ iostat -x 10 , watch disk io wait (assuming using linux/unix). if wait shoots up, mysql reading disk, doing work , preparing push data client.
copying tmp table means result of part of query or subquery moved memory more work can done using temporary data (temp table). whenever have encountered state, there's possibility of optimization. i'd more concerned both states if query taking several seconds return. i'd optimize query 1 in case. might want try increasing innodb_buffer_pool_size (assuming using innodb engine)
https://dev.mysql.com/doc/refman/5.5/en/innodb-buffer-pool.html


Comments
Post a Comment