修改查询逻辑:去除跨表查询,单独获取文章标题和内容
This commit is contained in:
@@ -93,31 +93,53 @@ class ArticleImageMatcher:
|
|||||||
SELECT
|
SELECT
|
||||||
at.id,
|
at.id,
|
||||||
at.article_id,
|
at.article_id,
|
||||||
at.coze_tag,
|
at.coze_tag
|
||||||
a.title,
|
|
||||||
a.content
|
|
||||||
FROM ai_article_tags at
|
FROM ai_article_tags at
|
||||||
INNER JOIN ai_articles a ON at.article_id = a.id
|
|
||||||
WHERE at.coze_tag IS NOT NULL
|
WHERE at.coze_tag IS NOT NULL
|
||||||
AND at.coze_tag != ''
|
AND at.coze_tag != ''
|
||||||
AND NOT EXISTS (
|
AND NOT EXISTS (
|
||||||
SELECT 1 FROM ai_article_images ai
|
SELECT 1 FROM ai_article_images ai
|
||||||
WHERE ai.article_id = at.article_id
|
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
|
ORDER BY at.id DESC
|
||||||
LIMIT %s
|
LIMIT %s
|
||||||
"""
|
"""
|
||||||
cursor.execute(sql, (limit,))
|
cursor.execute(sql, (limit,))
|
||||||
results = cursor.fetchall()
|
results = cursor.fetchall()
|
||||||
|
|
||||||
if results:
|
# 为每个结果添加文章标题和内容
|
||||||
logger.info(f"查询到 {len(results)} 篇需要匹配图片的文章")
|
processed_results = []
|
||||||
self.log_to_database('INFO', f"查询到需要匹配图片的文章", f"数量: {len(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:
|
else:
|
||||||
logger.info("未查询到需要匹配图片的文章")
|
logger.info("未查询到需要匹配图片的文章")
|
||||||
|
|
||||||
return results
|
return processed_results
|
||||||
finally:
|
finally:
|
||||||
connection.close()
|
connection.close()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user