Subject | Re: [ib-support] Re: Better procedure for "horizontal table"? |
---|---|
Author | Svein Erling Tysvær |
Post date | 2001-09-06T11:59:27Z |
>>Do you have an index on (Work_Day, Emp_No)?These indexes?? I was thinking of one combined index, i.e.
>Yes, there are this indexes.
CREATE INDEX DayEmployee ON Pers(Work_Day, Emp_No)
Don't know how much better it will be, but my guess is that it could help
quite a bit.
>Yes, it is right. EOM returns the End Of Month date. I've found, thatAbsolutely right.
>WHERE DateField BETWEEN Date1 AND Date2 uses indexes, but EXTRACT
>(MONTH FROM DateField) doesn't.
>IMHO the problem isn't the call of EOM procedure, the problem isSounds right, but every little bit could help :.) So try changing your
>twelvetimes SELECT SUM(... for every employee in the main-query, 100
>employees 1200 x SELECT SUM(... :-(
procedure to something like the procedure below and tell us your results (I
think the combined index will make the greatest difference, but don't know
until you report back).
HTH,
Set
CREATE PROCEDURE MonthTable ( TheYear SMALLINT, Dept_No SMALLINT )
RETURNS ( Emp_No INTEGER, Name VARCHAR(50), M01 DECIMAL (9,2),
M02 DECIMAL (9,2), M03 DECIMAL (9,2),
M04 DECIMAL (9,2), M05 DECIMAL (9,2),
M06 DECIMAL (9,2), M07 DECIMAL (9,2),
M08 DECIMAL (9,2), M09 DECIMAL (9,2),
M10 DECIMAL (9,2), M11 DECIMAL (9,2),
M12 DECIMAL (9,2) )
AS
DECLARE VARIABLE Date1 DATE;
DECLARE VARIABLE Date2 DATE;
BEGIN
FOR SELECT Emp_No, NAME1 || ' ' || NAME2, Work_Hours
FROM Pers
WHERE Dept_No = :Dept_No
INTO :Emp_No, :Name DO
BEGIN
EXECUTE PROCEDURE EncodeDate(TheYear, 1, 1) RETURNING_VALUES (Date1);
EXECUTE PROCEDURE EncodeDate(TheYear, 2, 1) RETURNING_VALUES (Date2);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date1 AND :Date2 - 1) INTO M01;
EXECUTE PROCEDURE EncodeDate(TheYear, 3, 1) RETURNING_VALUES (Date1);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date2 AND :Date1 - 1) INTO M02;
EXECUTE PROCEDURE EncodeDate(TheYear, 4, 1) RETURNING_VALUES (Date2);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date1 AND :Date2 - 1) INTO M03;
EXECUTE PROCEDURE EncodeDate(TheYear, 5, 1) RETURNING_VALUES (Date1);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date2 AND :Date1 - 1) INTO M04;
EXECUTE PROCEDURE EncodeDate(TheYear, 6, 1) RETURNING_VALUES (Date2);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date1 AND :Date2 - 1) INTO M05;
EXECUTE PROCEDURE EncodeDate(TheYear, 7, 1) RETURNING_VALUES (Date1);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date2 AND :Date1 - 1) INTO M06;
EXECUTE PROCEDURE EncodeDate(TheYear, 8, 1) RETURNING_VALUES (Date2);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date1 AND :Date2 - 1) INTO M07;
EXECUTE PROCEDURE EncodeDate(TheYear, 9, 1) RETURNING_VALUES (Date1);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date2 AND :Date1 - 1) INTO M08;
EXECUTE PROCEDURE EncodeDate(TheYear, 10, 1) RETURNING_VALUES (Date2);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date1 AND :Date2 - 1) INTO M09;
EXECUTE PROCEDURE EncodeDate(TheYear, 11, 1) RETURNING_VALUES (Date1);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date2 AND :Date1 - 1) INTO M10;
EXECUTE PROCEDURE EncodeDate(TheYear, 12, 1) RETURNING_VALUES (Date2);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date1 AND :Date2 - 1) INTO M11;
EXECUTE PROCEDURE EncodeDate(TheYear, 12, 31) RETURNING_VALUES (Date1);
SELECT SUM(Work_Hours) FROM WorkTime
WHERE Emp_No = :Emp_No AND
(Work_Day BETWEEN :Date2 AND :Date1)
SUSPEND;
END
END