创建排序索引需要很长时间

发布于 2025-01-10 05:22:25 字数 3318 浏览 1 评论 0原文

我有一个 SQL(MariaDB,版本:10.3.22-MariaDB-0+deb10u1):

SELECT
    v.employee_id AS id,
    REPLACE(
        FORMAT(
         COALESCE((
            SUM(COALESCE((COALESCE(v.amount, 0) / COALESCE(ash.value, a.standard, 0)), 0))
            /
            (
               SUM(COALESCE(v.worktime, 0)) 
               +
               (
                  SELECT SUM(COALESCE(t.break, 0)) 
                  + COALESCE(SUM(t.assigned_task1), 0) 
                  + COALESCE(SUM(t.assigned_task2), 0) 
                  + COALESCE(SUM(t.assigned_task3), 0) 
                  + COALESCE(SUM(t.assigned_task4), 0) 
                  + COALESCE(SUM(t.assigned_task5), 0) 
                  FROM times t 
                  WHERE t.employee_id = v.employee_id 
                  AND t.day BETWEEN '2022-02-03' AND '2022-02-03' 
               )
            ) 
         ), 0) * 100 
       , 0 
     ), ',', '') AS p_real 
FROM volumes v 
INNER JOIN activities a 
ON 
   v.activity_id = a.id 
   AND a.is_deleted = 0 
   AND a.id IN (-1, 145, 218, 227, 286) /* There are much more IDs in real SQL. */
LEFT JOIN activity_standard_histories ash 
ON ash.activity_id = a.id AND ash.created = (
    SELECT created 
    FROM activity_standard_histories 
    WHERE 
        DATE(activity_standard_histories.created) <= v.day 
        AND activity_standard_histories.activity_id = a.id 
    ORDER BY activity_standard_histories.created DESC 
    LIMIT 1
)
WHERE 
    v.employee_id IN ('552','553','554','2113','2991') /* There are much more employee_ids in real SQL. */
    AND v.day BETWEEN '2022-02-03' AND '2022-02-03' 
GROUP BY v.employee_id

volumes 表有 486.2Mb 大小,并且一直在增长。

这个查询执行了很长时间。我注意到的:

  1. 查询EXPLAIN

    “在此处输入图像描述"

  2. 此查询的状态长时间为“正在创建排序索引”。

表格索引:


输入图片此处描述

活动
输入图片此处描述

activity_standard_histories
输入图片此处描述


输入图片这里的描述

我看过很多资料:这个这个这个这个等等。但是,我不确定我到底应该申请什么。也许它可能是一些额外的复杂索引,但我不确定是哪一个,因为每个表已经有某些索引。

I have an SQL (MariaDB, version: 10.3.22-MariaDB-0+deb10u1):

SELECT
    v.employee_id AS id,
    REPLACE(
        FORMAT(
         COALESCE((
            SUM(COALESCE((COALESCE(v.amount, 0) / COALESCE(ash.value, a.standard, 0)), 0))
            /
            (
               SUM(COALESCE(v.worktime, 0)) 
               +
               (
                  SELECT SUM(COALESCE(t.break, 0)) 
                  + COALESCE(SUM(t.assigned_task1), 0) 
                  + COALESCE(SUM(t.assigned_task2), 0) 
                  + COALESCE(SUM(t.assigned_task3), 0) 
                  + COALESCE(SUM(t.assigned_task4), 0) 
                  + COALESCE(SUM(t.assigned_task5), 0) 
                  FROM times t 
                  WHERE t.employee_id = v.employee_id 
                  AND t.day BETWEEN '2022-02-03' AND '2022-02-03' 
               )
            ) 
         ), 0) * 100 
       , 0 
     ), ',', '') AS p_real 
FROM volumes v 
INNER JOIN activities a 
ON 
   v.activity_id = a.id 
   AND a.is_deleted = 0 
   AND a.id IN (-1, 145, 218, 227, 286) /* There are much more IDs in real SQL. */
LEFT JOIN activity_standard_histories ash 
ON ash.activity_id = a.id AND ash.created = (
    SELECT created 
    FROM activity_standard_histories 
    WHERE 
        DATE(activity_standard_histories.created) <= v.day 
        AND activity_standard_histories.activity_id = a.id 
    ORDER BY activity_standard_histories.created DESC 
    LIMIT 1
)
WHERE 
    v.employee_id IN ('552','553','554','2113','2991') /* There are much more employee_ids in real SQL. */
    AND v.day BETWEEN '2022-02-03' AND '2022-02-03' 
GROUP BY v.employee_id

volumes table has 486.2Mb size and growth up all the time.

This query is performed for a very long time. What I've noticed:

  1. Query EXPLAIN:

    enter image description here

  2. State for this query is "Creating sort index" for a long time.

Tables indexes:

volumes
enter image description here

activities
enter image description here

activity_standard_histories
enter image description here

times
enter image description here

I've seen a lot of sources: this, this, this, this and so on. However, I'm not sure what exactly should I apply. Maybe it could be some additional complex index, but I'm not sure which one, given that each table already has certain indexes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文