mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
Merge pull request #2744 from xinnan-tech/fix-delete-audio-data
分批次删除音频数据,避免数据库超时
This commit is contained in:
+18
-6
@@ -1,6 +1,9 @@
|
|||||||
package xiaozhi.modules.agent.dao;
|
package xiaozhi.modules.agent.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
|
||||||
@@ -15,12 +18,6 @@ import xiaozhi.modules.agent.entity.AgentChatHistoryEntity;
|
|||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity> {
|
public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity> {
|
||||||
/**
|
|
||||||
* 根据智能体ID删除音频
|
|
||||||
*
|
|
||||||
* @param agentId 智能体ID
|
|
||||||
*/
|
|
||||||
void deleteAudioByAgentId(String agentId);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据智能体ID删除聊天历史记录
|
* 根据智能体ID删除聊天历史记录
|
||||||
@@ -35,4 +32,19 @@ public interface AiAgentChatHistoryDao extends BaseMapper<AgentChatHistoryEntity
|
|||||||
* @param agentId 智能体ID
|
* @param agentId 智能体ID
|
||||||
*/
|
*/
|
||||||
void deleteAudioIdByAgentId(String agentId);
|
void deleteAudioIdByAgentId(String agentId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据智能体ID获取所有音频ID列表
|
||||||
|
*
|
||||||
|
* @param agentId 智能体ID
|
||||||
|
* @return 音频ID列表
|
||||||
|
*/
|
||||||
|
List<String> getAudioIdsByAgentId(String agentId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量删除音频
|
||||||
|
*
|
||||||
|
* @param audioIds 音频ID列表
|
||||||
|
*/
|
||||||
|
void deleteAudioByIds(@Param("audioIds") List<String> audioIds);
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-2
@@ -84,7 +84,16 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void deleteByAgentId(String agentId, Boolean deleteAudio, Boolean deleteText) {
|
public void deleteByAgentId(String agentId, Boolean deleteAudio, Boolean deleteText) {
|
||||||
if (deleteAudio) {
|
if (deleteAudio) {
|
||||||
baseMapper.deleteAudioByAgentId(agentId);
|
// 分批删除音频,避免超时
|
||||||
|
List<String> audioIds = baseMapper.getAudioIdsByAgentId(agentId);
|
||||||
|
if (audioIds != null && !audioIds.isEmpty()) {
|
||||||
|
int batchSize = 1000; // 每批删除1000条
|
||||||
|
for (int i = 0; i < audioIds.size(); i += batchSize) {
|
||||||
|
int end = Math.min(i + batchSize, audioIds.size());
|
||||||
|
List<String> batch = audioIds.subList(i, end);
|
||||||
|
baseMapper.deleteAudioByIds(batch);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (deleteAudio && !deleteText) {
|
if (deleteAudio && !deleteText) {
|
||||||
baseMapper.deleteAudioIdByAgentId(agentId);
|
baseMapper.deleteAudioIdByAgentId(agentId);
|
||||||
@@ -107,7 +116,7 @@ public class AgentChatHistoryServiceImpl extends ServiceImpl<AiAgentChatHistoryD
|
|||||||
// 添加此行,确保查询结果按照创建时间降序排列
|
// 添加此行,确保查询结果按照创建时间降序排列
|
||||||
// 使用id的原因:数据形式,id越大的创建时间就越晚,所以使用id的结果和创建时间降序排列结果一样
|
// 使用id的原因:数据形式,id越大的创建时间就越晚,所以使用id的结果和创建时间降序排列结果一样
|
||||||
// id作为降序排列的优势,性能高,有主键索引,不用在排序的时候重新进行排除扫描比较
|
// id作为降序排列的优势,性能高,有主键索引,不用在排序的时候重新进行排除扫描比较
|
||||||
.orderByDesc(AgentChatHistoryEntity::getId);
|
.orderByDesc(AgentChatHistoryEntity::getId);
|
||||||
|
|
||||||
// 构建分页查询,查询前50页数据
|
// 构建分页查询,查询前50页数据
|
||||||
Page<AgentChatHistoryEntity> pageParam = new Page<>(0, 50);
|
Page<AgentChatHistoryEntity> pageParam = new Page<>(0, 50);
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
-- 为智能体聊天历史记录添加音频ID索引
|
||||||
|
ALTER TABLE ai_agent_chat_history ADD INDEX idx_ai_agent_chat_history_audio_id (audio_id);
|
||||||
@@ -452,3 +452,10 @@ databaseChangeLog:
|
|||||||
- sqlFile:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202512161529.sql
|
path: classpath:db/changelog/202512161529.sql
|
||||||
|
- changeSet:
|
||||||
|
id: 202512192245
|
||||||
|
author: hrz
|
||||||
|
changes:
|
||||||
|
- sqlFile:
|
||||||
|
encoding: utf8
|
||||||
|
path: classpath:db/changelog/202512192245.sql
|
||||||
@@ -22,13 +22,18 @@
|
|||||||
created_at, updated_at
|
created_at, updated_at
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
<delete id="deleteAudioByAgentId">
|
<select id="getAudioIdsByAgentId" resultType="java.lang.String">
|
||||||
DELETE FROM ai_agent_chat_audio
|
SELECT DISTINCT audio_id
|
||||||
WHERE id IN (
|
FROM ai_agent_chat_history
|
||||||
SELECT audio_id
|
WHERE agent_id = #{agentId} AND audio_id IS NOT NULL
|
||||||
FROM ai_agent_chat_history
|
</select>
|
||||||
WHERE agent_id = #{agentId}
|
|
||||||
)
|
<delete id="deleteAudioByIds">
|
||||||
|
DELETE FROM ai_agent_chat_audio
|
||||||
|
WHERE id IN
|
||||||
|
<foreach collection="audioIds" item="id" open="(" separator="," close=")">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
<update id="deleteAudioIdByAgentId">
|
<update id="deleteAudioIdByAgentId">
|
||||||
|
|||||||
Reference in New Issue
Block a user