from core.mysql.models import ParseMonitoringDetail
from peewee import fn
from loguru import logger


group_by_parser_name: bool = True
group_by_error_class_name: bool = True
group_by_track_source_id: bool = True

query = (ParseMonitoringDetail
         .select(
             ParseMonitoringDetail.parser_name,
             ParseMonitoringDetail.error_class_name,
             ParseMonitoringDetail.track_source_id,
             fn.COUNT("*").alias('task_count')
         )
         .where(ParseMonitoringDetail.success == False)
         .order_by(fn.COUNT("*").alias('task_count').desc()))

if group_by_parser_name:
    query = query.group_by(
        ParseMonitoringDetail.parser_name,
    )
if group_by_error_class_name:
    query = query.group_by(
        ParseMonitoringDetail.error_class_name,
    )
if group_by_track_source_id:
    query = query.group_by(
        ParseMonitoringDetail.track_source_id
    )

logger.debug(f'sql: {str(query)}')

result = query.dicts().execute()
result = list(result)
logger.debug(result)

执行上面的代码,对应用户的 sql 只有一个

SELECT
    `t1`.`parser_name`,
    `t1`.`error_class_name`,
    `t1`.`track_source_id`,
    COUNT('*') AS `task_count`
FROM
    `parse_monitoring_detail` AS `t1`
WHERE
    (`t1`.`success` = 0)
GROUP BY
    `t1`.`track_source_id`
ORDER BY
    `task_count` DESC

但是从 wireshark 抓包可以看到有多个 Request Query

图片.png

192.168.26.31 是客户端
192.168.38.223 是 Mysql server

那么这些 Request Query 究竟是啥呢?

第一个 Request Query

SET NAMES utf8mb4

第二个 Request Query

SET sql_mode='PIPES_AS_CONCAT'

第三个 Request Query

SELECT
    `t1`.`parser_name`,
    `t1`.`error_class_name`,
    `t1`.`track_source_id`,
    COUNT('*') AS `task_count`
FROM
    `parse_monitoring_detail` AS `t1`
WHERE
    (`t1`.`success` = 0)
GROUP BY
    `t1`.`track_source_id`
ORDER BY
    `task_count` DESC

universe_king
3.4k 声望680 粉丝