질문에 답변이 많아질 때에 대한 질문입니다.
저는 데이터를 수집하는 게시판으로 파이보를 이용하고 있습니다.
하드웨어 아두이노에서 get 메소드를 통해서 자료를 주소 뒤에 붙여 보내고 답변으로 각 게시판에 저장합니다.
그런데 전송되는 데이터가 많아지면서 답변이 10만개가 넘어가니 게시판 시작 화면의 로딩 속도가 현저히 떨어졌습니다
answer_set | length 로 값을 호출하는 부분에서 질문 항목 10개에 있는 답변 전체를 SQL 쿼리하는 과정이 오래 걸리는 것 같았습니다.
질문에 달린 답변 개수 표시하는 방법을 어떻게 좀 더 빠른 속도로 처리할 수 있는 아이디어가 있을까 해서 글 올려 봅니다.
좋은 책 써 주셔서 정말 감사합니다.
장닭 님 387
M 2021년 11월 11일 9:33 오전
1개의 답변이 있습니다. 1 / 1 Page
. question_views.py
@bp.route('/list/')
def _list():
result = list()
result.insert(0, 0)
page = request.args.get('page', type=int, default=1)
sql = text('select count() from user')
sqlresult = db.engine.execute(sql)
cnt = int(sqlresult.fetchone()[0])
for i in range(1, cnt):
sql = text('select count() from answer where question_id=' + str(i))
sqlresult = db.engine.execute(sql)
result.insert(i, int(sqlresult.fetchone()[0]))
question_list = Question.query.order_by(Question.create_date.desc())
question_list = question_list.paginate(page, per_page=10)
return render_template('question/question_list.html', question_list=question_list, result=result, cnt=cnt)
. qustion_list.html
<tbody>
......................................
<td>
<a href = "{{ url_for('question.detail', question_id=question.id) }}">{{ question.subject }}</a>
{% if result[question.id] > 0 %}
<span class="text-danger small ml-2">{{result[question.id]}}</span>
{% endif %}
</td>
......................................
</tbody>
로 해서 빠르게 잘 처리 되었습니다. 아이디어 주셔서 감사합니다. 건강하세요.
장닭 님
M 2021년 11월 11일 9:29 오전