我们的一些SQL查询需要超过3小时才能执行,考虑到我们的数据库目前大约有5000个条目,这真的很长。
这里有两个最长的陈述:
/* Alte Jobs anzeigen */
update vwr_synch.listings set vwr_synch.listings.Alt="alt"
where vwr_synch.listings.JobKey not in (select vwr_synch.jobs.JobKey from vwr_synch.jobs)
;
update vwr_synch.listings set vwr_synch.listings.Alt="alt" where vwr_synch.listings.VWRStatus="NoJobs" or vwr_synch.listings.VWRStatus="Problem"
;
update vwr_synch.listings set vwr_synch.listings.Alt=NULL
where vwr_synch.listings.VWRStatus="Active" and vwr_synch.listings.VWRRetry!="0" and vwr_synch.listings.Alt="alt"
;
/* Neue Jobs anzeigen */
update vwr_synch.jobs set vwr_synch.jobs.NeuAlt="Neu"
where vwr_synch.jobs.JobKey not in (select vwr_synch.listings.JobKey from vwr_synch.listings) and (vwr_synch.jobs.`Status`="Active" and vwr_synch.jobs.Retry="0")
;
答案 0 :(得分:3)
您的示例代码有多个语句,因此我只关注第一个语句。
由于使用not in
的语义,我更喜欢NULL
,尽管有证据表明not in
可能更有效(请参阅评论中的链接)。这是第一个查询:
update vwr_synch.listings
set vwr_synch.listings.Alt = 'alt'
where vwr_synch.listings.JobKey not in (select vwr_synch.jobs.JobKey from vwr_synch.jobs);
我会将其更改为:
update vwr_synch.listings l
set l.Alt = 'alt'
where not exists (select 1 from vwr_synch.jobs.JobKey jk where jk.JobKey = l.JobKey);
然后,为了有效地工作,您需要vwr_synch.jobs.JobKey(JobKey)
上的索引。
接下来的两个陈述是:
update vwr_synch.listings l
set l.Alt = 'alt'
where l.VWRStatus in ('NoJobs', 'Problem');
update vwr_synch.listings l
set l.Alt = NULL
where l.VWRStatus = 'Active' and l.VWRRetry <> '0' and l.Alt = 'alt';
对于这些,您需要vwr_synch.listings(VWRStatus, Alt, VWRRetry)
上的索引。