首页 > 从mysql中查询数据,然后插入到另一个数据库中,怎样提升这个代码的运行速度

从mysql中查询数据,然后插入到另一个数据库中,怎样提升这个代码的运行速度

我从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

1.第一步的查询看是否有优化的空间,查询1秒多感觉有点太多了,看是否都使用上索引了。
2.可以贴一下你插入逻辑的伪代码吗,不清楚你是如何做插入操作的,感觉5000条数据插入不可能需要几十秒吧。


45秒到200秒这个时间跨度好大。
1、你这个sql写的太复杂了,建议简化sql,部分的逻辑处理放在代码中执行。
2、保存到另一个数据库考虑批量提交。

【热门文章】
【热门文章】