Mysql 存储过程增加了加载时间

Mysql 存储过程增加了加载时间,mysql,performance,stored-procedures,load,Mysql,Performance,Stored Procedures,Load,下面是我现在使用的存储过程。我怀疑这会减缓较大数据的站点负载。有人能给我建议一些解决这个问题的方法吗 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 TEM

下面是我现在使用的存储过程。我怀疑这会减缓较大数据的站点负载。有人能给我建议一些解决这个问题的方法吗

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

这是非常广泛的。下面是一些关于优化的基本读物:您需要提供更多信息。为什么要创建临时表?您是否使用explain select..分析select查询?表是否在执行select的位置编制索引?