簡體   English   中英

您可以使查詢更快嗎?

[英]Can you make this query faster?

我目前正在做一個暑假工作,我必須擴展現有的程序。

我的老板要求我為我們的客戶制作工具,以便他們可以看到他們的員工每月花費多少。 但這還不是全部。 問題是一家公司可以擁有一個或多個“社團”或子公司。 我們想知道員工在公司中每個社會的成本是多少。

這些是我使用的表:

  • 社團:帶有people_id的子公司,其中包含社團的名稱等
  • 時間表:包含人員和社會信息的時間表條目
  • 人員:數據庫中的所有人員或聯系人
  • 薪金狀態:包含某人一個月的薪水
  • 間接費用:某人在特定月份的間接費用(請注意,日期是一個字符串(!),格式為:YYYY-MM-DD)

該查詢有效,但是執行時間很長。 有沒有辦法使其更快?

我選擇年份和月份,獲得雇員(工人)的姓名和社團的名稱。 然后,我選擇員工(針對特定社會)工作的分鍾總數。 最后,我通過檢查他當月的工資和當月的間接費用來計算費用。

SELECT
    YEAR(TS.assigndate) AS timesheet_year,
    MONTH(TS.assigndate) AS timesheet_month,
    CONCAT(TP.name, ' ', TP.firstname) AS worker,
    CONCAT(SP.name, ' ', SP.firstname) AS society,
    (
        SELECT
            SUM(timeunits) AS minutes
        FROM timesheet
        WHERE
            people_id = TP.id AND
            society_id = S.id AND
            MONTH(assigndate) = timesheet_month
    ) AS minutes,
    (
        SELECT (minutes / 60)
    ) AS hours,
    (
        SELECT(OO.hourtarif + SS.hourtarif) AS cost
        FROM salarystate SS, overhead OO
        WHERE
            people_id = TP.id AND
            YEAR(OO.date) = timesheet_year AND
            MONTH(OO.date) = timesheet_month AND
            CONVERT(SUBSTRING(SS.month FROM 1 FOR 4), UNSIGNED) = timesheet_year AND
            CONVERT(SUBSTRING(SS.month, -2), UNSIGNED) = timesheet_month
    ) AS cost,
    (
        SELECT (hours * cost)
    ) AS total_cost
FROM timesheet TS, society S, people SP, people TP
WHERE
    S.id = TS.society_id AND
    SP.id = S.people_id AND
    TP.id = TS.people_id
GROUP BY timesheet_year, timesheet_month, worker, society; 
  1. 創建日期維度表http://www.sqlserversavvy.com/2008/03/t-sql-script-to-create-date-dimension.html
  2. 將與此日期列相關的列添加到此表
  3. 與dimDate分組

現在,我使用臨時表,它會像地獄般快速:)。 如果您有興趣,這是現​​在的代碼:

CREATE TEMPORARY TABLE IF NOT EXISTS people_hours (
    people_id INTEGER NOT NULL,
    society_id INTEGER NOT NULL,
    year INTEGER NOT NULL,
    month INTEGER NOT NULL,
    hours DOUBLE NOT NULL,
    PRIMARY KEY(people_id, society_id, year, month)
);

CREATE TEMPORARY TABLE IF NOT EXISTS people_cost (
    people_id INTEGER NOT NULL,
    year INTEGER NOT NULL,
    month INTEGER NOT NULL,
    cost DOUBLE NOT NULL,
    PRIMARY KEY(people_id, year, month)
);

TRUNCATE people_hours;
TRUNCATE people_cost;

INSERT INTO people_hours (people_id, society_id, year, month, hours)
SELECT
    p.id as people_id,
    s.id as society_id,
    YEAR(t.assigndate) as year,
    MONTH(t.assigndate) as month,
    SUM(t.timeunits)/60 as hours
FROM people p, society s, timesheet t
WHERE
    t.society_id = s.id AND
    t.people_id = p.id
GROUP BY year, month, people_id, society_id;

INSERT INTO people_cost (people_id, year, month, cost)
SELECT
    p.id as people_id,
    YEAR(o.date) as cost_year,
    MONTH(o.date) as cost_month,
    SUM(o.hourtarif + s.hourtarif) as cost
FROM people p, salarystate s, overhead o
WHERE
    s.people_id = p.id AND
    CONVERT(SUBSTRING(s.month FROM 1 FOR 4), UNSIGNED) = YEAR(o.date) AND
    CONVERT(SUBSTRING(s.month, -2), UNSIGNED) = MONTH(o.date)
GROUP BY cost_year, cost_month, people_id;

SELECT 
    h.year,
    h.month,
    h.society_id,
    h.hours,
    c.cost,
    (h.hours * c.cost) AS total_cost,
    CONCAT(p.name, ' ', p.firstname) AS employee,
    CONCAT(ps.name, ' ', ps.firstname) AS society
FROM people_hours h, people_cost c, people p, people ps, society s
WHERE
    h.society_id = s.id AND
    h.people_id = p.id AND
    h.people_id = c.people_id AND
    s.people_id = ps.id AND
    h.year = c.year AND
    h.month = c.month
ORDER BY h.year, h.month, h.people_id, h.society_id;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM