我从mysql中查询数据,然后将查询到的数据插入到另一个数据库中,目前速度查询5000个并插入的速度大概在45秒到200秒之间。怎样可以提升它的速度。
这条sql本身的执行时间在numiid很多的时候或者表中数据较多的时候就较慢,有时在1.6秒以上
sql:
select ir.id,iss.numIid,iss.bid,b.bname as bT,iss.cid,c.name as cT,iss.userId ,ss.shopTitle,iss.title as itemTitle,ir.type,ir.page,if(count(keyword) is null,0,count(keyword)) keywordCount1,IF(k.ssrq is null, 0,sum(k.ssrq )) ssrqCount1,if(iss.sales!=-1,iss.sales,IF(iss.trades!=-1,iss.trades,iss.receivers)) salesCount1
from table iss
inner join table0 ir on iss.numIid=ir.numIid
<if test="type != null"> and ir.type =#{type}</if>
<if test="type == null"> and ir.type in (1,2,3,4,11,12,13,21,22,31,32)</if>
<if test="page != null"> and ir.page =#{page}</if>
<if test="page == null"> and ir.page in(1,2,3,4,5,6,7,8,9,10) </if>
left join table1_${batchId} k on ir.keyword =k.id
left join table2_${batchId} ss on ss.userId =iss.userId
left join table3 b on iss.bid=b.bid
left join table4 c on iss.cid=c.id
where iss.numIid in (
<foreach collection="numIids" item="numIid" separator=",">
#{numIid}
</foreach>
)
group by iss.numIid,ir.page,ir.type
45秒到200秒這個時間跨度好大。
1、你這個sql寫的太複雜了,建議簡化sql,部分的邏輯處理放在程式碼中執行。
2、保存到另一個資料庫考慮批次提交。
1.第一步的查詢看是否有優化的空間,查詢1秒多感覺有點太多了,看是否都使用上索引了。
2.可以貼一下你插入邏輯的偽代碼嗎,不清楚你是如何做插入操作的,感覺5000條資料插入不可能需要幾十秒吧。