Click here to Skip to main content
15,921,169 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 5 tables to get my data. Currently, it runs at 8-10 minutes. Can it process a bit faster? See below my code. Not exactly the names of my tables but I apply the same logic. I got the correct results, I just need to make it faster, if possible.
Please ask if you need more information.

select 
B.PD_idno,
B.Fullname,
C.datetime_process,
C.detail_1, 
C.detail_2,

--/get the quantity in tbl_DT2 if exists, else, just get the one in tbl_Process/--
(case when (C.PD_idno = D.PD_idno)
	then E.DT_quantity
	else C.process_quantity
end) as Quantity

from tbl_InOut A
inner join tbl_PersonDetail B on A.PD_idno = B.PD_idno
inner join tbl_Process C on B.PD_idno = C.PD_idno 
--/get only process based on time_in and time_out/--
and C.datetime_process between A.time_in and A.time_out

left join tbl_DT1 D on C.detail_1 = D.detail_1 
and C.detail_2 = D.detail_2
left join tbl_DT2 E on D.DT_code = E.DT_code
and D.datetime_process = E.datetime_process

--/get process items between current date and past 7 days/--
where C.datetime_process between CONVERT(date, GETDATE() - 7)  and CONVERT(date, GETDATE() )	


What I have tried:

Tried the normal tips here: Speed up SQL Server database queries with these tips - TechRepublic[^]
and Performance Tuning SQL Queries | SQL Tutorial - Mode Analytics[^]
Posted
Updated 3-Jun-18 22:42pm
Comments
Mohibur Rashid 4-Jun-18 0:10am    
8 minutes seems too long for this query. What database to be exact?

How many rows is each table?
heyitsPJ 4-Jun-18 1:12am    
A thousand rows, especially in tbl_Process. As of now, tbl_Process has 97,158 and it will grow everyday because there's a lot of process transacted.
I only have one database and it has 30 tables, but for this specific query I only used 5 tables. The query will only return data from past 7 days including the current date.
heyitsPJ 4-Jun-18 1:23am    
see image here: https://pasteboard.co/HohQ7fR.png
that is sample data, but in my database I got thousands.
Mohibur Rashid 4-Jun-18 2:52am    
What is the name of your database?
heyitsPJ 4-Jun-18 3:12am    
Database name: PROCESSDB
This is on my local server. I'm using MS SQL 2008

1 solution

You have to pay attention to your joins, you have a three way join.
The PD_idno is the same in the first three tables, so change the joins so that tbl_Process only joins to tbl_InOut.
Like this:
SQL
SELECT  B.PD_idno
       ,B.Fullname
       ,C.datetime_process
       ,C.detail_1
       ,C.detail_2
       /*get the quantity in tbl_DT2 if exists, else, just get the one in tbl_Process*/
       ,CASE 
            WHEN (C.PD_idno = D.PD_idno) THEN E.DT_quantity
            ELSE C.process_quantity
        END AS Quantity
FROM    tbl_PersonDetail B
JOIN    tbl_InOut A ON A.PD_idno = B.PD_idno
JOIN    tbl_Process C 
    ON  a.PD_idno = C.PD_idno
    /*get only process based on time_in and time_out*/
    AND C.datetime_process BETWEEN A.time_in AND A.time_out
LEFT JOIN tbl_DT1 D 
    ON  C.detail_1 = D.detail_1
    AND C.detail_2 = D.detail_2
LEFT JOIN tbl_DT2 E
    ON  D.DT_code = E.DT_code
    AND D.datetime_process = E.datetime_process
/*get process items between current date and past 7 days*/
WHERE   C.datetime_process BETWEEN CONVERT(DATE, GETDATE() - 7) AND CONVERT(DATE, GETDATE())

Also make sure you have an index on tbl_Process (PD_idno,datetime_process)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900