以下是我现在使用的存储过程。我怀疑这会减慢大型数据的网站负载。任何人都可以建议我解决这个问题的方法......
BEGIN
DECLARE limitStart INT(10);
DECLARE limitEnd INT(10);
SET @login_user_id=(SELECT login_user_id);
SET @friend_fb_id=(SELECT friend_fb_id);
DROP TEMPORARY TABLE IF EXISTS temp_tab;
CREATE TEMPORARY TABLE temp_tab(row_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
fb_id BIGINT(20),user_fb_id BIGINT(20),name VARCHAR(500),gender VARCHAR(50)
,likes INT(10),movies INT(10),interests INT(10),books INT(10),total_count INT(10)) CHARSET=utf8;
IF @friend_fb_id=0 OR @friend_fb_id='' THEN
BEGIN
SET @gender=(SELECT gender FROM users WHERE user_fb_id=@login_user_id);
SET @insert_sql=concat('Insert into temp_tab(name,user_fb_id,gender)
select name,user_fb_id,gender from users where relationship_id NOT IN(2,3,4) and user_fb_id<>',@login_user_id,' and gender<>',''',@gender,''','
and user_fb_id not in(select friend_fb_id from friends where is_ignored>0)
and user_fb_id not in(select friend_fb_id from friends where user_fb_id=',@login_user_id,' and is_app_friend=1)');
PREPARE stmt1 FROM @insert_sql;
EXECUTE stmt1;
SET @update_sql=concat('update temp_tab inner join data on temp_tab.user_fb_id=data.user_fb_id set temp_tab.likes=(select count(data.type_id) from data where data.user_fb_id not in(',@login_user_id,') and
data.fb_id in(select data.fb_id from data where data.user_fb_id=',@login_user_id,' and data.type_id=1)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id),
temp_tab.movies=(select count(data.type_id) from data where data.user_fb_id not in(',@login_user_id,') and
data.fb_id in(select data.user_fb_id from data where data.user_fb_id=',@login_user_id,' and data.type_id=2)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id),
temp_tab.interests=(select count(data.type_id) from data where data.user_fb_id not in(',@login_user_id,') and
data.fb_id in(select data.fb_id from data where data.user_fb_id=',@login_user_id,' and data.type_id=3)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id),
temp_tab.books=(select count(data.type_id) from data where data.user_fb_id not in(',@login_user_id,') and
data.fb_id in(select data.fb_id from data where data.user_fb_id=',@login_user_id,' and data.type_id=4)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id)');
PREPARE stmt1 FROM @update_sql;
EXECUTE stmt1;
END;
ELSE
BEGIN
SET @gender=(SELECT gender FROM users WHERE user_fb_id=@friend_fb_id);
SET @insert_sql=concat('Insert into temp_tab(name,user_fb_id,gender)
select name,user_fb_id,gender from users where relationship_id NOT IN(2,3,4) and user_fb_id<>',@friend_fb_id,' and gender<>',''',@gender,''',' and user_fb_id<>',@login_user_id,'
and user_fb_id not in(select friend_fb_id from friends where is_ignored>0)
and user_fb_id not in(select friend_fb_id from friends where user_fb_id=',@friend_fb_id,' and is_app_friend=1)
and user_fb_id not in(select friend_fb_id from friends where user_fb_id=',@login_user_id,' and is_app_friend=1)');
PREPARE stmt1 FROM @insert_sql;
EXECUTE stmt1;
SET @update_sql=concat('update temp_tab inner join data on temp_tab.user_fb_id=data.user_fb_id set temp_tab.likes=(select count(data.type_id) from data where data.user_fb_id not in(',@friend_fb_id,') and
data.fb_id in(select data.fb_id from data where data.user_fb_id=',@friend_fb_id,' and data.type_id=1)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id),
temp_tab.movies=(select count(data.type_id) from data where data.user_fb_id not in(',@friend_fb_id,') and
data.fb_id in(select data.user_fb_id from data where data.user_fb_id=',@friend_fb_id,' and data.type_id=2)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id),
temp_tab.interests=(select count(data.type_id) from data where data.user_fb_id not in(',@friend_fb_id,') and
data.fb_id in(select data.fb_id from data where data.user_fb_id=',@friend_fb_id,' and data.type_id=3)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id),
temp_tab.books=(select count(data.type_id) from data where data.user_fb_id not in(',@friend_fb_id,') and
data.fb_id in(select data.fb_id from data where data.user_fb_id=',@friend_fb_id,' and data.type_id=4)
and temp_tab.user_fb_id=data.user_fb_id group by user_fb_id)');
PREPARE stmt1 FROM @update_sql;
EXECUTE stmt1;
END;
END IF;
DROP TEMPORARY TABLE IF EXISTS match_tab;
CREATE TEMPORARY TABLE match_tab(s_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
fb_id BIGINT(20),user_fb_id BIGINT(20),name VARCHAR(500),gender VARCHAR(50)
,likes INT(10),movies INT(10),interests INT(10),books INT(10),total_count INT(10),is_in_app TINYINT(4) DEFAULT 0) CHARSET=utf8;
INSERT INTO match_tab(user_fb_id,name,gender,likes,movies,interests,books,total_count)
SELECT user_fb_id,name,gender,ifnull(likes,0),ifnull(movies,0),ifnull(interests,0),ifnull(books,0),
sum(ifnull(books,0)+ifnull(movies,0)+ifnull(interests,0)+ifnull(likes,0))AS total_count FROM temp_tab
GROUP BY user_fb_id;
UPDATE match_tab SET is_in_app=1 WHERE user_fb_id IN
(SELECT user_fb_id FROM users WHERE access_token IS NOT NULL);
SET limitStart = ((page*page_size)-page_size) ;
SET limitEnd = page_size ;
SET @SNO = ((page - 1)*page_size);
SELECT @a:=@a+1 sno,user_fb_id,name,gender,likes,movies,interests,books,total_count,is_in_app
FROM match_tab ,(SELECT @a:= @SNO) AS a WHERE total_count<>0 ORDER BY total_count DESC
LIMIT limitStart,limitEnd;
SELECT count(s_id)AS total_count FROM match_tab WHERE total_count<>0;
END