Slope-One使用mysql存储过程实现推荐器

时间:2013-12-27 12:30:52

标签: mysql stored-procedures recommendation-engine collaborative-filtering

我正在尝试使用mysql存储过程实现Slop-One推荐程序,查询运行正常并且不会给出任何错误。但它没有插入/更新'dev'表。 表的结构是:

rating (user_id, article_id, rating_value, date)

dev (article1_id, article2_id, count, sum)

'dev'表具有联合主键(article1_id, article2_id)。我的程序的sql如下:

DELIMITER $$

CREATE PROCEDURE update_matrix(IN user INT(11), IN article INT(11), IN rating TINYINT(1))
    BEGIN
      DECLARE article_id2 INT(11);
      DECLARE rating_diff TINYINT(1);
      DECLARE done TINYINT(1) DEFAULT 0;
      DECLARE mycursor CURSOR FOR
        SELECT DISTINCT article_id, (rating - rating_value)
        FROM rating
        WHERE user_id = user AND article_id != article;
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
      OPEN mycursor;
        FETCH mycursor INTO article_id2, rating_diff;
        WHILE(!done) DO
          INSERT INTO dev (`article1_id`, `article2_id`, `count`, `sum`)
          VALUES (article, article_id2, 1, rating_diff)
          ON DUPLICATE KEY UPDATE count = count + 1, sum = sum + rating_diff;

          INSERT INTO dev (`article1_id`, `article2_id`, `count`, `sum`)
          VALUES (article_id2, article, 1, -rating_diff)
          ON DUPLICATE KEY UPDATE count = count + 1, sum = sum - rating_diff;

          FETCH mycursor INTO article_id2, rating_diff;
        END WHILE;
      CLOSE mycursor;
    END$$
  DELIMITER ;

0 个答案:

没有答案