I am working on MySql database. Where I need to merge information form multiple(more than 10) tables into a single one. In order to do that I am following a typical joining style.
Select * from
table_1
Join table_2
on(table_1.id = table_2.id)
Join table_3
on(table_1.id = table_3.id)
It works but I suffer a lot during execution time. Is there any other good way to optimize my code? Following is the sample of my code:
SELECT
distinct
u.Id,
oc.dt,
Daily_Number_Outgoing_Calls,
Daily_Number_Incoming_Calls,
Daily_duration_Outgoing_Calls
FROM
creditfix.users u
JOIN
#1 Daily_No_Out_Calls
(
SELECT
cl.uId,SUBSTRING(DATE,1,10) as dt,
count(1) as Daily_Number_Outgoing_Calls
From creditfix.call_logs as cl
WHERE
cl.`type`=2 #out going calls only
GROUP by cl.uId,dt
) oc
ON (u.Id=oc.Uid)
#2 Daily_No_In_Calls
JOIN
(
SELECT
cl.uId, SUBSTRING(DATE,1,10) as dt,
count(1) as Daily_Number_Incoming_Calls
From creditfix.call_logs as cl
WHERE
cl.`type`=1 #incoming calls only
GROUP by cl.uId,dt
) ic
ON (u.Id=ic.Uid)
#3 Daily_duration_Out_Calls
JOIN
(
SELECT
cl.uId,SUBSTRING(DATE,1,10) as dt,
(sum(duration)) as Daily_duration_Outgoing_Calls
From creditfix.call_logs as cl
WHERE
cl.`type`=2 #out going calls only
GROUP by cl.uId,dt
) od
ON (u.Id=od.uid)
# It goes on like this...