I have question about performance:
I have table Employees
id | name |
---|---|
1 | name surname 1 |
2 | name surname 2 |
And table plan
emp_id | shift_date | shift_begin | shift_end |
---|---|---|---|
1 | 2022-01-01 | 08:00 | 17:00 |
1 | 2022-01-02 | 08:00 | 17:00 |
1 | 2022-01-04 | 08:00 | 17:00 |
2 | 2022-01-01 | 08:00 | 17:00 |
2 | 2022-01-02 | 08:00 | 17:00 |
2 | 2022-01-03 | 08:00 | 17:00 |
Note: emp_id is a id of employee
and I have following query:
SELECT id,name,
(select concat_ws('-',to_char(shift_begin ,'HH24:MI'),to_char(shift_end ,'HH24:MI'),typ) from plan where plan.emp_id = employee.id and shift_date = '2022-01-01') as d0,
(select concat_ws('-',to_char(shift_begin ,'HH24:MI'),to_char(shift_end ,'HH24:MI'),typ) from plan where plan.emp_id = employee.id and shift_date = '2022-01-02') as d1,
(select concat_ws('-',to_char(shift_begin ,'HH24:MI'),to_char(shift_end ,'HH24:MI'),typ) from plan where plan.emp_id = employee.id and shift_date = '2022-01-03') as d2,
(select concat_ws('-',to_char(shift_begin ,'HH24:MI'),to_char(shift_end ,'HH24:MI'),typ) from plan where plan.emp_id = employee.id and shift_date = '2022-01-04') as d3,
(select concat_ws('-',to_char(shift_begin ,'HH24:MI'),to_char(shift_end ,'HH24:MI'),typ) from plan where plan.emp_id = employee.id and shift_date = '2022-01-05') as d4
-- continues to end of month
from employee;
Result is pretty good...
id | name | d0 | d1 | d2 | d3 | d4 | ---> d30 |
---|---|---|---|---|---|---|---|
1 | name surname 1 | 08:00-17:00 | 08:00-17:00 | 08:00-17:00 | ---> d30 | ||
2 | name surname 2 | 08:00-17:00 | 08:00-17:00 | 08:00-17:00 | ---> d30 |
...but when I have for example 50 employees which I would display in table (50 emp * 31 days) it drops in performance (after insert, delete)...
I have a crosstab too but it doesn't show me any results
Now back to the topic: Its a good option ? or use crosstab ? (Fyi in crosstab I have selection from table plan, but I want to select each of table employee).
I appreciate any help.