注意点:

(1)进行拆分的总表表名是不同的。所以创建暂时表,把总表的数据先插入暂时表

(2)为了避免最外层游标轮询数据结束时,抛出 not found 退出程序,不会运行关闭游标等兴许操作,定义continue handler。 declare continue handler for not found set done1=1;

1.1、外部存储过程调用主存储过程

CREATE PROCEDURE `bbs_split_thread_post_outer`(IN `in_tabname` varchar(128))

BEGIN

declare v_row_count int(11);

declare v_sql varchar(200);



-- 必须清空暂时表bbs_fromask_importask_tmp

select sysdate();

truncate table bbs_fromask_importask_tmp;





set @sql=concat('insert into bbs_fromask_importask_tmp select * from ',in_tabname,';');

prepare stmt from @sql;

execute stmt;



call bbs_split_thread_post();





select sysdate();

END

CREATE PROCEDURE `bbs_split_thread_post`()

BEGIN

declare v_source_count int(11);

declare v_thread_pkid int(11);

declare v_thread_pkid_uni_count int(11);

  declare done1,done2 int default 0;

declare v_cur_thread_pkid cursor for select pkid,count(*) from bbs_fromask_importask_tmp group by pkid;

declare continue handler for not found set done1=1;





-- 清空暂时表

truncate table pre_data_thread_tmp;

truncate table pre_data_post_tmp;



-- 推断原表是否有数据,没有什么也不做

select count(*) into v_source_count from bbs_fromask_importask_tmp;

if v_source_count>0 then

    select ifnull(max(id),0) into @thread_max_id from yaolanbbs.pre_data_thread; -- 可做改动

  select @thread_max_id;

 

  -- 打开游标

  open v_cur_thread_pkid;

  repeat

  fetch v_cur_thread_pkid into v_thread_pkid,v_thread_pkid_uni_count;

  -- 每次fetch最大id自增1

  if not done1 then

  set @thread_max_id=@thread_max_id+1;

  insert into pre_data_thread_tmp(id,title,age,rule,param)

  select @thread_max_id,t1.qtitle,t1.age,'age',t1.age from bbs_fromask_importask_tmp t1 where t1.pkid=v_thread_pkid limit 1;

  -- 依据pkid对一组数据进行处理

  begin

  declare v_post_answer text;

  declare v_cur_post_record cursor for select t1.answer from bbs_fromask_importask_tmp t1 where t1.pkid=v_thread_pkid;

  declare exit handler for not found close v_cur_post_record;

  -- declare continue handler for not found set done2=1;

 

  insert into pre_data_post_tmp(tid,text,sort)

  select @thread_max_id,t1.qdesc,1 from bbs_fromask_importask_tmp t1 where t1.pkid=v_thread_pkid limit 1;

 

  set @sort=2; ######设置值

  open v_cur_post_record;

  repeat 

  fetch v_cur_post_record into v_post_answer;

   -- select @sort;

  -- select v_post_answer;

  insert into pre_data_post_tmp(tid,text,sort) values(@thread_max_id,v_post_answer,@sort);

  -- insert into testincre values(@sort,v_post_answer);

  set @sort=@sort+1;

  until 0 end repeat;

  -- select curdate(); 不会运行的原因。定义exit handler

  close v_cur_post_record;

  end;

  end if;

  until done1 end repeat;

  -- 关闭游标

  close v_cur_thread_pkid;

  -- select curdate();

  /*-- 导入终于表

  insert into pre_data_thread(id,title,create_time,age,rule,param)

  select id,title,create_time,age,rule,param from pre_data_thread_tmp;

  insert into pre_data_post(tid,text,sort,create_time)

  select tid,text,sort,create_time from pre_data_post_tmp;

*/

end if;

END

总结:

(1)30G内存没有负载的情况下。8488276行拆为两个表共10500000行用时39分钟

drop procedure if exists if_var_inner_updated_swap;

create procedure `if_var_inner_updated_swap`()

begin



declare ind int (8);



declare update_status char (10);



declare swap_status char (10);





select

id

from

datalogic_var_inner_update_swap_log

where

task_name = "datalogic_var_inner_apply_hist_uuid_01_backup"

and task_date = curdate() into ind;



select

task_update_status

from

datalogic_var_inner_update_swap_log

where

id = ind into update_status;



select

task_swap_status

from

datalogic_var_inner_update_swap_log

where

id = ind into swap_status;



if (

update_status = 'finished'

and swap_status =0

) then

rename table datalogic_var_inner_apply_hist_uuid_01 to datalogic_var_inner_apply_hist_uuid_01_temp,

datalogic_var_inner_apply_hist_uuid_01_backup to datalogic_var_inner_apply_hist_uuid_01,

datalogic_var_inner_apply_hist_uuid_01_temp to datalogic_var_inner_apply_hist_uuid_01_backup;



update datalogic_var_inner_update_swap_log

set task_swap_status = '1'

where

id = ind;

end if;



end

mysql开发之---使用游标双层嵌套对总表进行拆分为帖子表和回复表的更多相关文章

  1. 第一篇:Win10系统搭建Python+Django+Nginx+MySQL 开发环境详解(完美版)

    Win10+Python+Django+Nginx+MySQL 开发环境搭建详解 PaulTsao 说明:本文由作者原创,仅供内部参考学习与交流,转载引用请注明出处,用于商业目的请联系作者本人. Wi ...

  2. MySQL开发指南

    数据库开发是数据库管理系统(DBMS)和数据库应用软件设计研发的总称,数据运维.参与数据库生产环境的问题优化和解决等方面的事宜. 1.关于MySQL数据库 2.搭建MySQL环境 3.入门常用SQL. ...

  3. MySQL(8)---游标

    Mysql(8)-游标 上一遍博客写了有关存储过程的语法知识 Mysql(7)---存储过程 游标或许你在工作中很少用到,但用不到不代表不去了解它,但你真正需要它来解决问题的时候,再花时间去学习很可能 ...

  4. 使用FragmentTabHost+TabLayout+ViewPager实现双层嵌套Tab

    大多数应用程序都会在底部使用3~5个Tab对应用程序的主要功能进行划分,对于一些信息量非常大的应用程序,还需要在每个Tab下继续划分子Tab对信息进行分类显示. 本文实现采用FragmentTabHo ...

  5. Win7搭建nginx+php+mysql开发环境以及websocket聊天实例测试

    Win7搭建nginx+php+mysql开发环境以及websocket聊天实例测试一.下载相关安装包 1.下载nginx最新版本(nginx1.3.13版之后才支持websocket协议) 下载地址 ...

  6. Vue 双层嵌套

    这种的需要双层嵌套. 代码: <table id="ict-table" class="ict-table ict-report-table blue-theme& ...

  7. 根据多年经验整理的《互联网MySQL开发规范》

    一.基础规范 使用 INNODB 存储引擎 表字符集使用 UTF8  所有表都需要添加注释 单表数据量建议控制在 5000W 以内 不在数据库中存储图⽚.文件等大数据 禁止在线上做数据库压力测试 禁⽌ ...

  8. 深入理解MySQL开发性能优化.pptx

    深入理解MySQL开发性能优化.pptx,依旧上传baidu pan http://pan.baidu.com/s/1jIwGslS,视频暂未出,培训完成后会更新.

  9. 【MySql】赶集网mysql开发36条军规

    [MySql]赶集网mysql开发36条军规 2012-05-14 14:02:33 分类: Linux   写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒 ...

随机推荐

  1. C++封装随笔

    1接口的封装和设计思想入门 接口的封装和设计思想入门 第一套api函数 #ifndef _CLT_SOCKET_H__ #define _CLT_SOCKET_H__ //客户端初始化环境 int c ...

  2. Broadcom GNSS xxx Geolocaltion Sensor与Windows导航程序的兼容性(转)

    Broadcom是Windows 8(3G)平板普遍采用的一款GPS传感器, 其windows驱动程序可以提供GNSS接口.GNSS接口提供的数据,说实话确实比普通手机的数据好.在开机.室外.无AGP ...

  3. python自动化测试windows gui

    http://sourceforge.net/projects/pywinauto/files/pywinauto/ http://www.microsoft.com/en-us/download/c ...

  4. java实现MQ消息收发两种方式

    定义: 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.简单理解:蓝牙配对 jar包依赖: <!-- ...

  5. 计蒜客 28315.Excellent Engineers-线段树(单点更新、区间最值) (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E)

    先写这几道题,比赛的时候有事就只签了个到. 题目传送门 E. Excellent Engineers 传送门 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到 ...

  6. (4)oracle连接工具和配置监听

    一.SQL PLUS sql plus 是oracle最常用的命令行工具,启动sqlplus工具的方法有两种 1. 是在安装好的oracle开始程序的路径下运行程序 点击运行弹出此界面 2 .是在cm ...

  7. (1)C#工具箱-公共控件1

    公共控件 InitializeComponent() 先说下InitializeComponent()这个方法,它在form1.cs里调用这个方法对控件进行初始化,控件的方法要在这个方法之后,否则会因 ...

  8. Sql Jions 的简易理解

    Sql Jions 的简易理解 Select  * from TableA A  left jion TableB  B on  A.key = B.key Select  * from TableA ...

  9. 818D - Multicolored Cars

    818D - Multicolored Cars 题意 在 1 到 n 时刻,有 n 量有颜色的车通过,用数字表示颜色,Alice 选择一个颜色A,要求 Bob 选择一个颜色B,使得对于任意时刻 cn ...

  10. Closest Binary Search Tree Value -- LeetCode

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...