查询逻辑代码:
from sqlalchemy import Table
from common.database import db_connect
from app.config.config import config
from app.settings import env
from model.user import User
db_session,Base,engine = db_connect()
class Article(Base):
__table__ = Table("article", Base.metadata, autoload_with=engine)
# 查询出所有文章,但是不要草稿
def find_article(self,page,article_type="recommend"):
# 一页显示多少内容呢,我们默认为一个10,page默认应该是从1开始
if int(page) < 1:
page = 1
count = int(page) * config[env].page_count
# 这就证明是来到了推荐的分类下边
if article_type == "recommend":
result = db_session.query(Article,User.nickname).join(
User,User.user_id==Article.user_id
).filter(
Article.drafted==1
).order_by(
Article.browse_num.desc()
).limit(count).all()
else:
result = db_session.query(Article,User.nickname).join(
User, User.user_id == Article.user_id
).filter(
Article.label_name == article_type,
Article.drafted==1
).order_by(
Article.browse_num.desc()
).limit(count).all()
return result
经过排查是使用sqlalchemy的order_by方法那一处开始出现问题的
查看控制台信息, python项目中使用报错如下:
ERROR:app.app:Exception on / [GET] Traceback (most recent call last): File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1967, in _exec_single_context self.dialect.do_execute( File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 941, in do_execute cursor.execute(statement, parameters) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\cursors.py", line 153, in execute result = self._query(query) ^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\cursors.py", line 322, in _query conn.query(q) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 563, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 825, in _read_query_result result.read() File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 1206, in read self._read_result_packet(first_packet) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 1283, in _read_result_packet self._read_rowdata_packet() File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 1330, in _read_rowdata_packet packet = self.connection._read_packet() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 775, in _read_packet packet.raise_for_error() File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\protocol.py", line 219, in raise_for_error err.raise_mysql_exception(self._data) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\err.py", line 150, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.OperationalError: (1038, 'Out of sort memory, consider increasing server sort buffer size') The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject\controller\index.py", line 22, in home db_result = article.find_article(page, article_type) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject\model\article.py", line 27, in find_article ).limit(count).all() ^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\orm\query.py", line 2673, in all return self._iter().all() # type: ignore ^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\orm\query.py", line 2827, in _iter result: Union[ScalarResult[_T], Result[_T]] = self.session.execute( ^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\orm\session.py", line 2362, in execute return self._execute_internal( ^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\orm\session.py", line 2247, in _execute_internal result: Result[Any] = compile_state_cls.orm_execute_statement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\orm\context.py", line 305, in orm_execute_statement result = conn.execute( ^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1418, in execute return meth( ^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\sql\elements.py", line 515, in _execute_on_connection return connection._execute_clauseelement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1640, in _execute_clauseelement ret = self._execute_context( ^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1846, in _execute_context return self._exec_single_context( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1986, in _exec_single_context self._handle_dbapi_exception( File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 2355, in _handle_dbapi_exception raise sqlalchemy_exception.with_traceback(exc_info[2]) from e File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1967, in _exec_single_context self.dialect.do_execute( File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 941, in do_execute cursor.execute(statement, parameters) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\cursors.py", line 153, in execute result = self._query(query) ^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\cursors.py", line 322, in _query conn.query(q) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 563, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 825, in _read_query_result result.read() File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 1206, in read self._read_result_packet(first_packet) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 1283, in _read_result_packet self._read_rowdata_packet() File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 1330, in _read_rowdata_packet packet = self.connection._read_packet() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\connections.py", line 775, in _read_packet packet.raise_for_error() File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\protocol.py", line 219, in raise_for_error err.raise_mysql_exception(self._data) File "D:\pythonprojects\python-code\flaskProject.venv\Lib\site-packages\pymysql\err.py", line 150, in raise_mysql_exception raise errorclass(errno, errval) sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1038, 'Out of sort memory, consider increasing server sort buffer size') [SQL: SELECT article.id AS article_id, article.label_name AS article_label_name, article.article_image AS article_article_image, article.title AS article_title, article.article_content AS article_article_content, article.article_tag AS article_article_tag, article.user_id AS article_user_id, article.browse_num AS article_browse_num, article.drafted AS article_drafted, article.article_type AS article_article_type, article.create_time AS article_create_time, user.nickname AS user_nickname FROM article INNER JOIN user ON user.user_id = article.user_id WHERE article.drafted = %(drafted_1)s ORDER BY article.browse_num LIMIT %(param_1)s] [parameters: {'drafted_1': 1, 'param_1': 10}]
提取重要信息:
pymysql.err.OperationalError: (1038, 'Out of sort memory, consider increasing server sort buffer size')
。这个错误表明 MySQL 在执行排序操作时内存不足,导致查询失败。
所以调大一下linux的内存
再重启项目查看效果