Mysql PREPARE语句在记录插入时花费了很多时间

时间:2013-07-02 07:25:39

标签: mysql sql stored-procedures mysqli rdbms

我在商店程序中使用PREPARE语句,但是在记录插入时花费了很多时间。请建议我替换PREPARE声明。

SET @COUNT = 0;
SET @countQuery = CONCAT("select count(ufl.id) INTO @COUNT from user_friend_list as ufl where ufl.user_id = ",@USERREGID," AND ufl.friend_user_id = ",FID);
  PREPARE STMTC FROM @countQuery; 
 EXECUTE STMTC; 


IF @COUNT = 0 THEN
INSERT INTO user_friend_list(user_id, friend_user_id) VALUES(@USERREGID,FID),(FID,@USERREGID);
    END IF;
      DEALLOCATE PREPARE STMTC;

1 个答案:

答案 0 :(得分:2)

为什么不这样做呢

IF 0 = (SELECT COUNT(*) 
          FROM user_friend_list 
         WHERE user_id = @USERREGID
           AND friend_user_id = FID) THEN
    INSERT INTO user_friend_list(user_id, friend_user_id) 
    VALUES (@USERREGID,FID), (FID,@USERREGID);
END IF; 

这是 SQLFiddle 演示