码迷,mamicode.com
首页 > 数据库 > 详细

mysql 临时表

时间:2015-07-22 18:11:59      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

当查询大表时,我们可以通过构造临时表来避免频繁的遍历大表。临时表只有当前连接可见,当连接断开时会被自动drop,下文给出临时表的应用例子。

前提:

1、分别构造表t_demo_test1和t_demo_test2,并输入数据:

DROP TABLE IF EXISTS t_demo_test1;
CREATE TABLE IF NOT EXISTS t_demo_test1(
    tid int(20) NOT NULL AUTO_INCREMENT,
    userid int(20),
    day datetime NOT NULL,
    title varchar(50),
    content varchar(200),
    PRIMARY KEY(tid)
)ENGINE=InnoDB  DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;

DROP TABLE IF EXISTS t_demo_test2;
CREATE TABLE IF NOT EXISTS t_demo_test2(
    tid int(20) NOT NULL AUTO_INCREMENT,
    userid int(20),
    name varchar(50),
    class varchar(50),
    PRIMARY KEY(tid)
)ENGINE=InnoDB  DEFAULT CHARSET=gbk AUTO_INCREMENT=1 ;

insert into t_demo_test2(userid, name, class) values(11, fred, important class);

insert into t_demo_test1(userid, day, title, content) 
values(10,2015-04-05 22:10:30,title1,content sinny write test)
,(11,2015-04-05 22:10:30,title1,一种内容1)
,(12,2015-04-06 22:10:30,title1,一种内容2)
,(13,2015-04-07 22:10:30,title1,一种内容3)
,(14,2015-04-08 22:10:30,title1,一种内容4);

2、直接构造临时表

CREATE TEMPORARY TABLE tmp_table SELECT userid, name FROM t_demo_test2;

3、在存储过程中使用临时表

DELIMITER //
DROP PROCEDURE IF EXISTS p_modify_content;
CREATE PROCEDURE p_modify_content
(
    IN v_mcontent varchar(10),
    OUT v_output varchar(100),
    OUT v_count int
)
BEGIN
    DECLARE tmpid int(20);
    DECLARE tname varchar(50);
    DECLARE tuserid int(20);
    DECLARE done INT DEFAULT FALSE;
    DECLARE cur CURSOR FOR SELECT t_demo_test1.userid, t_demo_test1.tid FROM t_demo_test1;    
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    OPEN cur;
    
    DROP TEMPORARY TABLE IF EXISTS usertmp;
 
    CREATE TEMPORARY TABLE usertmp(
        userid int(20),
        name varchar(50)
    ) ENGINE = MEMORY;
    
    myloop: LOOP
        FETCH cur INTO tuserid, tmpid;

        IF done THEN
            LEAVE myloop;
        END IF;
        
        SELECT t_demo_test2.name from t_demo_test2 WHERE userid = tuserid INTO tname;
        
        select tname;
        IF !isNull(tname) THEN    
            INSERT INTO usertmp VALUES(tuserid, tname);
        END IF;
        
        UPDATE t_demo_test1 SET content = concat(content,v_mcontent, tname) WHERE tid = tmpid;    
        SET done = FALSE;/* SELECT t_demo_test2.name from t_demo_test2 WHERE userid = tuserid INTO tname; 也会触发NOT FOUND */
        SET tname = null;
    END LOOP;
    SELECT COUNT(*) FROM usertmp INTO v_count;
    SET v_output = finished;
    CLOSE cur;
    DROP TEMPORARY TABLE IF EXISTS usertmp;
END
//

call p_modify_content("hello", @content, @count);
select @content;//输出finished
select @count;//输出1

4、临时表与内存表的区别

临时表,schema和数据都在内存中,创建表后,无法通过show tables看到该表,重新进入后无表、无数据;

内存表,schema在硬盘中,数据在内存中,创建表后可以在show tables看到该表,重新进入后有表,无数据; 

如下,建立内存表:

create table XXtable(
    。。。
)engine = HEAP;

 

mysql 临时表

标签:

原文地址:http://www.cnblogs.com/Fredric-2013/p/4668045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!