patternsqlMinor
Supervisors and employees query, with a subselect and inner join
Viewed 0 times
employeeswithquerysubselectjoinsupervisorsandinner
Problem
I want to replace or improve the
Is there any way to improve this SQL command?
SELECT command of tblVisor to make it faster.Is there any way to improve this SQL command?
SELECT tblVisor.supervisor_id,
tblVisor.last_name,
tblVisor.first_names,
tblVisor.employee_job_profile_id,
org_employees.last_name,
org_employees.first_name,
org_employees.job_code
FROM (SELECT cp_supervisor_properties.supervisor_id,
persons.last_name,
persons.first_names,
cp_supervisor_properties.employee_job_profile_id
FROM cp_supervisor_properties
INNER JOIN persons ON persons.person_id = cp_supervisor_properties.supervisor_id) as tblVisor
INNER JOIN org_employees ON org_employees.employee_number = tblVisor.employee_job_profile_id
LIMIT 100Solution
Unless I'm missing something there doesn't seem to be a reason for the
subselect, just join all three tables directly. Also,
default, so you could drop that prefix as well.
The query would then become something like the following:
Without more information (about how the data looks and so) I don't see possible performance improvements. If you have problems with the query time you should probably check for missing indexes on the join columns and in general look into query optimisation.
subselect, just join all three tables directly. Also,
INNER is thedefault, so you could drop that prefix as well.
The query would then become something like the following:
SELECT cp_supervisor_properties.supervisor_id,
persons.last_name,
persons.first_names,
cp_supervisor_properties.employee_job_profile_id,
org_employees.last_name,
org_employees.first_name,
org_employees.job_code
FROM cp_supervisor_properties
INNER JOIN persons
ON persons.person_id = cp_supervisor_properties.supervisor_id
INNER JOIN org_employees
ON org_employees.employee_number = cp_supervisor_properties.employee_job_profile_id
LIMIT 100;Without more information (about how the data looks and so) I don't see possible performance improvements. If you have problems with the query time you should probably check for missing indexes on the join columns and in general look into query optimisation.
Code Snippets
SELECT cp_supervisor_properties.supervisor_id,
persons.last_name,
persons.first_names,
cp_supervisor_properties.employee_job_profile_id,
org_employees.last_name,
org_employees.first_name,
org_employees.job_code
FROM cp_supervisor_properties
INNER JOIN persons
ON persons.person_id = cp_supervisor_properties.supervisor_id
INNER JOIN org_employees
ON org_employees.employee_number = cp_supervisor_properties.employee_job_profile_id
LIMIT 100;Context
StackExchange Code Review Q#87719, answer score: 5
Revisions (0)
No revisions yet.