Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have nearly 3 million records in a table. when i select the records from using the below query it takes atleast 45 secs.

SQL
  select a.user_Id,b.name,a.paid_date,
 (select count(user_Id) from tbl_payment_Master 
where user_Id=a.user_Id  and paid_date=a.paid_date group by paid_date) as noofpaymnt,
(select count(user_Id) from tbl_payment_Master 
where user_Id=a.user_Id  and month(paid_Date)=MONTH(a.paid_date) and year(paid_Date)=year(a.paid_date)) as noofpaymonth 
from tbl_payment_master as a 
inner join tbl_user_registration as b on a.user_id=b.user_id 
where a.premium_amount!=0 and a.user_id=@user_id 


How could be improve the performance.
Posted
Updated 2-Oct-15 0:36am
v3
Comments
Patrice T 2-Oct-15 6:10am    
1lakh= 100,000 so 27 lakhs= 2,700,000 records
ManojanbU 2-Oct-15 6:19am    
i have know 27 lakh=2700000 .then I need how to reduce the time when query select.
Patrice T 2-Oct-15 6:28am    
My comment was not for you !
a lakh is not a used unit outside of India.
ManojanbU 2-Oct-15 6:36am    
oh ok...Nearly 3 million records in a table.Hereafter i use this correctly..thanks for this. Then Any solution for this question
ManojanbU 2-Oct-15 6:21am    
if i remove
(
(select count(user_Id) from tbl_payment_Master
where user_Id=a.user_Id and premium_amount!=0 and paid_date=a.paid_date group by paid_date) as noofpaymnt,
(select count(user_Id) from tbl_payment_Master
where user_Id=a.user_Id and premium_amount!=0 and month(paid_Date)=MONTH(a.paid_date) and year(paid_Date)=year(a.paid_date)) as noofpaymonth
)
this part from the query then it takes only fraction of seconds

Hi,

In sub-query instead of using a.userid why don't try using direct user variable, that may change execution plan.try this you will get little bit of improvement

select a.user_Id,b.name,a.paid_date,
(
select count(user_Id) from tbl_payment_Master
where user_Id=@user_id and paid_date=a.paid_date group by paid_date) as noofpaymnt,
(
select count(user_Id) from tbl_payment_Master
where user_Id=@user_id and
month(paid_Date)=MONTH(a.paid_date)
and year(paid_Date)=year(a.paid_date)
)
as noofpaymonth

from tbl_payment_master as a
inner join tbl_user_registration as b on a.user_id=b.user_id
where a.premium_amount!=0 and a.user_id=@user_id


-------and also try with CTE-----------
 
Share this answer
 
v2
I would optimise this clause.
SQL
where user_Id=a.user_Id  and month(paid_Date)=MONTH(a.paid_date) and year(paid_Date)=year(a.paid_date))

I would create a new field paid_Month containing yyyymm from paid_Date in each table.
Then change the clause to:
SQL
where user_Id=a.user_Id  and paid_Month=a.paid_Month
 
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