Mysql多插入proc挂起,而单个插入工作

时间:2016-12-20 09:45:18

标签: mysql

我有一个mysql程序,如下所示。问题是程序挂起insert into customformattributes。显示开放表格in_use <> 0&#39;发送数据&#39;在提到的插页上。完成此过程需要10分钟以上。但是,单个插件只需不到一分钟。索引是在表创建期间创建的。但是,我尝试在加载到表格后添加它们,它没有任何帮助

DELIMITER $$
CREATE PROCEDURE `insertattributesall`(in isactivetoproc int)
begin
if isactivetoproc = 1
then
-- attributes
start transaction;
alter table attributes truncate partition pactive;
insert into attributes select 
pl.CompanyId,
pl.FarmerCropId,
ai.ActivityId,
ai.ActivityName,
ai.ActivityPlanId,
ai.ActivityPlanName,
ai.ActivityScheduleId,
ai.DAS,
(case when fca.CompletionDate = '1900-01-01' then null else fca.CompletionDate end)CompletionDate,
case when date(now()) between date(fca.ExpectedStartDate) AND date(fca.ExpectedClosureDate)
AND fca.IsClosed = 0 then 2
when date(now()) > date(fca.ExpectedClosureDate) AND fca.IsClosed = 0 then 3
when date(now()) < date(fca.ExpectedStartDate) and  fca.IsClosed = 0 then 4
when date(now()) > date(fca.ExpectedClosureDate) AND fca.IsClosed = 1 then 5
when   fca.IsClosed = 1 then 1
end isclosed,
fca.LastModifiedDate,
ai.AttributeDataTypeId,
ai.AttributeId,
ai.AttributeName,
fcaa.FarmerCropActivityAttributeId,
fcaa.Reading,
ai.DataTypeId,
ai.DataTypeName,
ai.SequenaceNo,
fca.isactive,
pl.partitionflag
from
activityinformation ai
join FarmerCropActivity fca 
on ai.activityscheduleid = fca.activityscheduleid and fca.isactive = 1
left join FarmerCropActivityAttributes fcaa on
fcaa.farmercropactivityid = fca.farmercropactivityid
and fcaa.AttributeDataTypeID = ai.AttributeDataTypeID
 join plot pl on
pl.farmercropid = fca.farmercropid 
where pl.partitionflag = 1;
commit;
start transaction;
alter table customformattributes truncate partition pactive;
-- custom attributes
insert into customformattributes 
  select
pl.CompanyId,
pl.FarmerCropId,
ac.ActivityId,
ac.ActivityName,
fca.FarmerCropActivityId,
fca.isclosed,
(case when fca.CompletionDate = '1900-01-01' then null else fca.CompletionDate end)CompletionDate,
fca.LastModifiedDate,
ac.AttributeDataTypeId,
ac.AttributeId,
ac.AttributeName,
fcaa.FarmerCropActivityAttributeId,
fcaa.Reading,
ac.DataTypeId,
ac.DataTypeName,
 ac.SequenaceNo,
ac.customformid,
fca.isactive,
pl.partitionflag
from
activitycustominfo ac 
join FarmerCropActivity fca
on fca.activityid = ac.activityid  and fca.isactive = 1
left join FarmerCropActivityAttributes fcaa on 
fcaa.farmercropactivityid = fca.farmercropactivityid
and ac.AttributeDataTypeId = fcaa.AttributeDataTypeId
join plot pl on
pl.farmercropid = fca.farmercropid and ac.CropTypeId = pl.VarietyId
where pl.partitionflag = 1;
commit;
else
start transaction;
insert into attributes  select 
pl.CompanyId,
pl.FarmerCropId,
ai.ActivityId,
ai.ActivityName,
ai.ActivityPlanId,
ai.ActivityPlanName,
ai.ActivityScheduleId,
ai.DAS,
(case when fca.CompletionDate = '1900-01-01' then null else fca.CompletionDate end)CompletionDate,
case when date(now()) between date(fca.ExpectedStartDate) AND date(fca.ExpectedClosureDate)
AND fca.IsClosed = 0 then 2
when date(now()) > date(fca.ExpectedClosureDate) AND fca.IsClosed = 0 then 3
when date(now()) < date(fca.ExpectedStartDate) and  fca.IsClosed = 0 then 4
when date(now()) > date(fca.ExpectedClosureDate) AND fca.IsClosed = 1 then 5
when   fca.IsClosed = 1 then 1
end isclosed,
fca.LastModifiedDate,
ai.AttributeDataTypeId,
ai.AttributeId,
ai.AttributeName,
fcaa.FarmerCropActivityAttributeId,
fcaa.Reading,
ai.DataTypeId,
ai.DataTypeName,
ai.SequenaceNo,
fca.isactive,
pl.partitionflag
from
activityinformation ai
join FarmerCropActivity fca 
on ai.activityscheduleid = fca.activityscheduleid 
left join FarmerCropActivityAttributes fcaa on
fcaa.farmercropactivityid = fca.farmercropactivityid
and fcaa.AttributeDataTypeID = ai.AttributeDataTypeID
join plot pl on
pl.farmercropid = fca.farmercropid
where pl.partitionflag = 0
and date(pl.talendate) = date(now()) 
;
commit;
start transaction;-- custom attributes
insert into customformattributes 
 select
pl.CompanyId,
pl.FarmerCropId,
ac.ActivityId,
ac.ActivityName,
fca.FarmerCropActivityId,
fca.isclosed,
(case when fca.CompletionDate = '1900-01-01' then null else fca.CompletionDate end)CompletionDate,
fca.LastModifiedDate,
ac.AttributeDataTypeId,
ac.AttributeId,
ac.AttributeName,
fcaa.FarmerCropActivityAttributeId,
fcaa.Reading,
ac.DataTypeId,
ac.DataTypeName,
 ac.SequenaceNo,
ac.customformid,
fca.isactive,
pl.partitionflag
from
activitycustominfo ac 
join FarmerCropActivity fca
on fca.activityid = ac.activityid
left join FarmerCropActivityAttributes fcaa on 
fcaa.farmercropactivityid = fca.farmercropactivityid
and ac.AttributeDataTypeId = fcaa.AttributeDataTypeId
join plot pl on
pl.farmercropid = fca.farmercropid and ac.CropTypeId = pl.VarietyId
where pl.partitionflag = 0
and date(pl.talendate) = date(now()) 
;
commit;
end if;
end$$
DELIMITER ;

单个插入物在不到一分钟的时间内运行。创建的索引是

create index idx_pl_farmercropid on plot(farmercropid);
 create index idx_pl_companyid on plot(companyid);
 create index idx_pl_isactive on plot(partitionflag);
create index idx_fca_activityscheduleid on FarmerCropActivity(activityscheduleid);
create index idx_fca_activityid on FarmerCropActivity(ActivityId);
create index idx_fca_farmercropid on FarmerCropActivity(farmercropid);
create index idx_ai_activityid on activityinformation(activityid);
create index idx_ai_activityscheduleid on activityinformation(activityscheduleid);
create index idx_ac_activityid on activitycustominfo(activityid);
create index idx_ac_croptypeid on activitycustominfo(croptypeid);
create index idx_ac_AttributeDataTypeID on activitycustominfo(AttributeDataTypeID);
create index idx_fcaa_farmercropactivityid on FarmerCropActivityAttributes(farmercropactivityid);
create index idx_fcaa_AttributeDataTypeID on FarmerCropActivityAttributes(AttributeDataTypeID);

插页的计划是

表示属性表 - http://i63.tinypic.com/5d1c12.jpg 对于customformattribute - http://i64.tinypic.com/zxji8g.jpg

每个表中的行数如下

select count(*) from FarmerCropActivity; -- 1418392
select count(*) from FarmerCropActivityAttributes; -- 3183038
select count(*) from activityinformation; -- 109595
select count(*) from plot where partitionflag =1;  -- 157534
select count(*) from activitycustominfo ;  -- 13552

Plot table是一个分区表

任何关于此的想法都会有很大的帮助

由于

0 个答案:

没有答案