开发者

which of these mysql queries is more efficient, using left join or not

开发者 https://www.devze.com 2023-03-28 09:16 出处:网络
i have a following sql query $select_query_1 = SELECT * FROM user_module_comments WHERE useid = \'$hash\' ORDER BY id DESC LIMIT 0, 25

i have a following sql query

$select_query_1 = SELECT * FROM user_module_comments WHERE useid = '$hash' ORDER BY id DESC LIMIT 0, 25
  while($table = mysql_fetch_array($select_query_1)){
    $user_moid = $table['canvas'];
    $user_xtract_canvas = mysql_query("SELECT mcanvas FROM user_module WHERE uid = '$user_moid' LIMIT 1");
    $selected = mysql_fetch_array($user_xtract_canvas);
    $user_canvas_extr开发者_运维知识库act = $selected['mcanvas']; // this is what i need
  }

OR this sql query

$select_query = SELECT user_module_comments.useid, user_module.mcanvas FROM user_module_comments LEFT JOIN user_module ON user_module.uid = user_module_comments.useid WHERE useid = '$hash' ORDER BY user_module_comments.id DESC LIMIT 0, 25

which of these queries is more efficient thank


The JOIN is likely to be far, far faster than doing related queries in a loop. In general it is almost always faster to do one query than to do n queries. I only say "almost always" because I'm sure someone can come up with a use case where the opposite may be true.

There is a lot of overhead involved with MySQL compiling the SQL statement over and over in the loop, executing it, and fetching a rowset. Using the single statement eliminates all of that overhead.

You should install Xdebug and actually profile these statements in PHP to find out how long they take to execute.

0

精彩评论

暂无评论...
验证码 换一张
取 消