修改查询逻辑:去除跨表查询,单独获取文章标题和内容

This commit is contained in:
2026-02-05 11:47:43 +08:00
parent 514f085c79
commit d0a4583cb1

View File

@@ -93,31 +93,53 @@ class ArticleImageMatcher:
SELECT
at.id,
at.article_id,
at.coze_tag,
a.title,
a.content
at.coze_tag
FROM ai_article_tags at
INNER JOIN ai_articles a ON at.article_id = a.id
WHERE at.coze_tag IS NOT NULL
AND at.coze_tag != ''
AND NOT EXISTS (
SELECT 1 FROM ai_article_images ai
WHERE ai.article_id = at.article_id
)
AND a.status = 'pending_review'
AND at.article_id IN (
SELECT id FROM ai_articles
WHERE status = 'pending_review'
)
ORDER BY at.id DESC
LIMIT %s
"""
cursor.execute(sql, (limit,))
results = cursor.fetchall()
if results:
logger.info(f"查询到 {len(results)} 篇需要匹配图片的文章")
self.log_to_database('INFO', f"查询到需要匹配图片的文章", f"数量: {len(results)}")
# 为每个结果添加文章标题和内容
processed_results = []
for row in results:
# 查询文章标题和内容
article_sql = """
SELECT title, content
FROM ai_articles
WHERE id = %s
"""
cursor.execute(article_sql, (row['article_id'],))
article_data = cursor.fetchone()
if article_data:
processed_row = {
'id': row['id'],
'article_id': row['article_id'],
'coze_tag': row['coze_tag'],
'title': article_data['title'],
'content': article_data['content']
}
processed_results.append(processed_row)
if processed_results:
logger.info(f"查询到 {len(processed_results)} 篇需要匹配图片的文章")
self.log_to_database('INFO', f"查询到需要匹配图片的文章", f"数量: {len(processed_results)}")
else:
logger.info("未查询到需要匹配图片的文章")
return results
return processed_results
finally:
connection.close()
except Exception as e: