이 문제를 해결하려면 영구 "임시"테이블을 만들고 테이블 이름에 SPID (죄송합니다. 저는 SQL Server 영역에서 왔습니다)를 접미사로 붙여 고유 한 테이블 이름을 만듭니다. 그런 다음 동적 SQL 문을 만들어 쿼리를 만듭니다. 문제가 발생하면 테이블이 삭제되고 다시 생성됩니다.
더 나은 옵션을 원합니다. 어서, MySQL Devs. 2008 년부터 '버그'/ '기능 요청'이 오픈되었습니다! 만난 모든 '버그'가 같은 보트에있는 것 같습니다.
select concat('ReviewLatency', CONNECTION_ID()) into @tablename;
set @dsql=concat('drop table if exists ', @tablename, ';');
PREPARE QUERY1 FROM @dsql;
EXECUTE QUERY1;
DEALLOCATE PREPARE QUERY1;
set @dsql=concat('
create table ', @tablename, ' (
`EventUID` int(11) not null,
`EventTimestamp` datetime not null,
`HasAudit` bit not null,
`GroupName` varchar(255) not null,
`UserID` int(11) not null,
`EventAuditUID` int(11) null,
`ReviewerName` varchar(255) null,
index `tmp_', @tablename, '_EventUID` (`EventUID` asc),
index `tmp_', @tablename, '_EventAuditUID` (`EventAuditUID` asc),
index `tmp_', @tablename, '_EventUID_EventTimestamp` (`EventUID`, `EventTimestamp`)
) ENGINE=MEMORY;');
PREPARE QUERY2 FROM @dsql;
EXECUTE QUERY2;
DEALLOCATE PREPARE QUERY2;
set @dsql=concat('
insert into ', @tablename, '
select e.EventUID, e.EventTimestamp, e.HasAudit, gn.GroupName, epi.UserID, eai.EventUID as `EventAuditUID`
, concat(concat(concat(max(concat('' '', ui.UserPropertyValue)), '' (''), ut.UserName), '')'') as `ReviewerName`
from EventCore e
inner join EventParticipantInformation epi on e.EventUID = epi.EventUID and epi.TypeClass=''FROM''
inner join UserGroupRelation ugr on epi.UserID = ugr.UserID and e.EventTimestamp between ugr.EffectiveStartDate and ugr.EffectiveEndDate
inner join GroupNames gn on ugr.GroupID = gn.GroupID
left outer join EventAuditInformation eai on e.EventUID = eai.EventUID
left outer join UserTable ut on eai.UserID = ut.UserID
left outer join UserInformation ui on eai.UserID = ui.UserID and ui.UserProperty=-10
where e.EventTimestamp between @StartDate and @EndDate
and e.SenderSID = @FirmID
group by e.EventUID;');
PREPARE QUERY3 FROM @dsql;
EXECUTE QUERY3;
DEALLOCATE PREPARE QUERY3;
set @dsql=concat('
select rl1.GroupName as `Group`, coalesce(max(rl1.ReviewerName), '''') as `Reviewer(s)`, count(distinct rl1.EventUID) as `Total Events`
, (count(distinct rl1.EventUID) - count(distinct rl1.EventAuditUID)) as `Unreviewed Events`
, round(((count(distinct rl1.EventUID) - count(distinct rl1.EventAuditUID)) / count(distinct rl1.EventUID)) * 100, 1) as `% Unreviewed`
, date_format(min(rl2.EventTimestamp), ''%W, %b %c %Y %r'') as `Oldest Unreviewed`
, count(distinct rl3.EventUID) as `<=7 Days Unreviewed`
, count(distinct rl4.EventUID) as `8-14 Days Unreviewed`
, count(distinct rl5.EventUID) as `>14 Days Unreviewed`
from ', @tablename, ' rl1
left outer join ', @tablename, ' rl2 on rl1.EventUID = rl2.EventUID and rl2.EventAuditUID is null
left outer join ', @tablename, ' rl3 on rl1.EventUID = rl3.EventUID and rl3.EventAuditUID is null and rl1.EventTimestamp > DATE_SUB(NOW(), INTERVAL 7 DAY)
left outer join ', @tablename, ' rl4 on rl1.EventUID = rl4.EventUID and rl4.EventAuditUID is null and rl1.EventTimestamp between DATE_SUB(NOW(), INTERVAL 7 DAY) and DATE_SUB(NOW(), INTERVAL 14 DAY)
left outer join ', @tablename, ' rl5 on rl1.EventUID = rl5.EventUID and rl5.EventAuditUID is null and rl1.EventTimestamp < DATE_SUB(NOW(), INTERVAL 14 DAY)
group by rl1.GroupName
order by ((count(distinct rl1.EventUID) - count(distinct rl1.EventAuditUID)) / count(distinct rl1.EventUID)) * 100 desc
;');
PREPARE QUERY4 FROM @dsql;
EXECUTE QUERY4;
DEALLOCATE PREPARE QUERY4;
set @dsql = concat('drop table if exists ', @tablename, ';');
PREPARE QUERY5 FROM @dsql;
EXECUTE QUERY5;
DEALLOCATE PREPARE QUERY5;