日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

md0 mysql_mysql 数据合并

發布時間:2024/9/3 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 md0 mysql_mysql 数据合并 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近有這樣的需求,由于早期數據庫表設計有些問題,為了加速查詢速度將幾個關聯表合并到一張表中,最開始想的是一個字段一個字段進行入庫更新。類似于

update tb_userinfo_0 a set avatar_update_count = (select

b.update_count from tb_anothertable_0 b where a.uid=b.uid);

但效率太低時間上無法忍受。

在網上一頓神搜,終于找到一個可行的方案。

1.創建主表的備份

2.將主表重命名成臨時使用的表

3.將主表和關聯表的數據寫到一張臨時表中去

4.添加字段或者修改字段

5.將臨時表命名成主表的名字

6.刪除掉第2步產生的臨時表

主要用到了以下的sql語句:

begin

set @p0 = 'create table ht_userinfo_';

set @p1 = ' (select * from ht_userinfo_';

set @pcmd0=concat(@p0,table_index ,'_bak',@p1,table_index,');');

prepare pstmt0 from @pcmd0;

execute pstmt0;

set @s0 = 'alter table ht_userinfo_';

set @s1 = ' rename to userinfotemp_';

set @cmd0=concat(@s0,table_index ,@s1,table_index);

prepare stmt0 from @cmd0;

execute stmt0;

set @str1 = '

Create table new_table_name (Select a.* ,

b.c_code as a_code, b.c_bg as a_bg, b.update_count as a_update_count, b.update_time as a_update_time,

c.current_xml as xml, c.update_count as f_update_count, c.update_time as f_update_time

from userinfotemp_';

set @str2 = ' a left join tb_user_avatar_';

set @str3 =' b on a.uid=b.uid

left join tb_user_fashion_';

set @str4 =' c on a.uid=c.uid);';

set @cmd=concat(@str1,table_index ,@str2,table_index ,@str3,table_index ,@str4);

prepare stmt from @cmd;

execute stmt;

alter table new_table_name add primary key(id);

alter table new_table_name add `pshow_link` varchar(100) DEFAULT NULL;

alter table new_table_name add `pshow_update_time` datetime DEFAULT NULL;

alter table new_table_name add `pshow_update_count` bigint(20) DEFAULT 0;

update new_table_name set a_update_count = 0 where a_update_count is null;

update new_table_name set f_update_count = 0 where f_update_count is null;

alter table new_table_name modify a_update_count bigint(20) DEFAULT '0';

set @sss0 = 'alter table new_table_name';

set @sss1 = ' rename to ht_userinfo_';

set @sscmd0=concat(@sss0 ,@sss1,table_index);

prepare ssstmt0 from @sscmd0;

execute ssstmt0;

set @ss0 = 'drop table userinfotemp_';

set @scmd0=concat(@ss0 ,table_index);

prepare sstmt0 from @scmd0;

execute sstmt0;

end

其中tableindex 是傳入參數

寫的有些亂,但是還是可以看懂的。

正式上線出了個大問題,由于來回導表把索引弄沒了,導致查詢極其緩慢有時一條要30s,把數據庫的服務器的cpu占滿了,昨晚緊急升級加上索引問題解決。

剩下一個疑惑:為什么查會跑到主庫上去?

0

4

分享到:

2010-12-28 13:20

瀏覽 1181

分類:數據庫

評論

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的md0 mysql_mysql 数据合并的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。