开发者

Can you optimize this SQL query for me?

I'm running this query in 2 mysql_query calls. I would like to do the most performant thing and run it in one query. Can anyone optimize this for me:

if(mysql_num_rows(eq("select * 
                        from player_data 
                       where uid = $uid")) == 0 )
      eq("update开发者_如何学Go player_data 
             set uid = $uid, 
                `stat`= REPLACE(`stat`, '$temp_uid', '$uid') 
           where uid = $temp_uid");

Note: eq is just a wrapper function for mysql_query


You could try:

eq("update player_data 
       set uid = $uid, 
           `stat`=REPLACE(`stat`, '$temp_uid', '$uid') 
     where uid=$temp_uid 
       and not exists (select * 
                         from player_data 
                        where uid = $uid)");


if(mysql_num_rows(eq("select 1 from player_data where uid=$uid")) == 0 )
      eq("update player_data set uid=$uid, `stat`=REPLACE(`stat`, '$temp_uid', '$uid') where uid=$temp_uid");

Avoid select * if you just want to get the count of rows in the table.


Will A's answer is very good, and if you do need to select a field specify it. Selecting everything is terrible for performance.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜