在hibernate中没有级联的批量删除的最佳做法是什么?

时间:2016-05-21 18:03:38

标签: java mysql hibernate

我想通过某些标准从表中加载数据,然后将它们插入到历史表中。之后我想从第一个表中删除数据。就像将数据从一个表移动到另一个表一样。

这是我的代码:

    long started = System.currentTimeMillis();

    List<SentNtf> sentNtfListToBackup = sentNtfDAO.getNtfListToBackup();

    long ended = System.currentTimeMillis();
    long duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " getNtfListToBackup tooks " + duration + " ms.");

    if(sentNtfListToBackup == null || sentNtfListToBackup.isEmpty()){
        return 0;
    }   

    started = System.currentTimeMillis();

    HashMap<Integer, NotificationHistory> ntfs = new HashMap<Integer, NotificationHistory>();
    for(SentNtf sentNtf : sentNtfListToBackup){
        NotificationHistory nh = createNotificationHistory(sentNtf.getNotification());
        ntfs.put(nh.getRid(), nh);
    }

    ended = System.currentTimeMillis();
    duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " loading NotificationHistory to memory tooks " + duration + " ms.");

    started = System.currentTimeMillis();

    insertObjects(new ArrayList<Object>(ntfs.values()));

    ended = System.currentTimeMillis();
    duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " inserting NotificationHistory to db tooks " + duration + " ms.");


    started = System.currentTimeMillis();

    List<Object> list = new ArrayList<Object>();
    for(SentNtf sentNtf : sentNtfListToBackup){
        SentNtfHistory snh = createSentNtfHistory(sentNtf, ntfs.get(sentNtf.getNotification().getRid()));
        list.add(snh);
    }

    ended = System.currentTimeMillis();
    duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " loading SentNtfHistory to memory tooks " + duration + " ms.");

    started = System.currentTimeMillis();

    insertObjects(list);

    ended = System.currentTimeMillis();
    duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " inserting SentNtfHistory to db tooks " + duration + " ms.");

    started = System.currentTimeMillis();

    deleteSentNtfs(sentNtfListToBackup);

    ended = System.currentTimeMillis();
    duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " deleting SentNtfHistory from db tooks " + duration + " ms.");

    started = System.currentTimeMillis();

    deleteNtfs(sentNtfListToBackup);

    ended = System.currentTimeMillis();
    duration = ended - started;
    logger.debug(this.getClass().getSimpleName() + " deleting NtfHistory from db tooks " + duration + " ms.");

这是日志:

getNtfListToBackup tooks 14962 ms.
loading NotificationHistory to memory tooks 109 ms.
inserting NotificationHistory to db tooks 47868 ms.
loading SentNtfHistory to memory tooks 404 ms.
inserting SentNtfHistory to db tooks 75916 ms.
deleting SentNtfHistory from db tooks 1081012 ms.
deleting NtfHistory from db tooks 1114347 ms.

正如您所看到的,虽然将150K实体插入目标表需要2分钟,但从源表中删除它们需要36.5分钟!!!

我正在删除这样的实体:

String hql = "delete from " + tableName + " f where f." + idName + " in (:ids)";
sessionFactory.getCurrentSession().createQuery(hql).setParameterList("ids", ids).executeUpdate();

那么这个问题的最佳方法是什么?

提前致谢。

0 个答案:

没有答案