你能更快地进行这个查询吗?

时间:2010-09-03 07:53:42

标签: sql mysql

我目前正在做一个暑期工作,我必须扩展现有的计划。

我的老板让我为我们的客户制作一个工具,这样他们就能看到每个月他们的员工花了多少钱。但那还不是全部。问题是公司可以拥有一个或多个“社团”或子公司。我们想知道一个公司每个社会的员工成本是多少。

这些是我使用的表格:

  • 社会:包含people_id的子公司,其中包含社会的名称等
  • 时间表:包含人员和社会信息的时间表条目
  • 人:数据库中的所有人或联系人
  • salarystate:包含特定月份某人的工资
  • 开销:特定月份的人的间接费用(注意日期是一个字符串(!)格式如下: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; 

2 个答案:

答案 0 :(得分:2)

  1. 创建日期维度表 http://www.sqlserversavvy.com/2008/03/t-sql-script-to-create-date-dimension.html
  2. 将与日期列相关的列添加到此表
  3. 与dimDate分组

答案 1 :(得分:1)

现在我使用临时表,它快速地运行:)。如果您有兴趣,现在就是代码:

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;