Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have written a query for mysql database. the result is to display in a Crystal Report.
The problem is, the execution time is too much(more than 30 minutes).
how can i reduce this execution time. Any guidelines is warmly welcome.
below is the c# code...

string strQry = "select distinct category,pay_mode,coalesce((select sum(amount) from sold_items where  invoice_date between '" + dtfrom1 + "' and '" + dtto1 + "' and category = A.category and pay_mode = A.pay_mode and tax_percent = '0.00'),0.00) as taxFree,coalesce((select sum(amount) from sold_items where  invoice_date between '" + dtfrom1 + "' and '" + dtto1 + "' and category = A.category and pay_mode = A.pay_mode and tax_percent = '5.00'),0.00) as amountSub5,coalesce((select sum(amount) from sold_items where  invoice_date between '" + dtfrom1 + "' and '" + dtto1 + "' and category = A.category and pay_mode = A.pay_mode and tax_percent = '14.00'),0.00) as amountSub14 from sold_items A where invoice_date between '" + dtfrom1 + "' and '" + dtto1 + "'";
              
                MySqlDataAdapter da = new MySqlDataAdapter(strQry,con);
               
                DataSet2 ds1 = new DataSet2();
                da.SelectCommand.CommandTimeout = 3600;//60 minutes
                da.Fill(ds1, "saletaxreport1");
                CRSaletaxReport_Initial pr = new CRSaletaxReport_Initial();
                pr.SetDataSource(ds1);
                pr.VerifyDatabase();
                crystalReportViewer1.ReportSource = pr;


below is the value of strQry variable...
select distinct invoice_date,coalesce((select sum(amount) from sold_items B where B.invoice_date = A.invoice_date and B.pay_mode=A.pay_mode and tax_percent = '00.00'),0.00) as taxFree,coalesce((select sum(amount) from sold_items B where B.invoice_date = A.invoice_date and B.pay_mode=A.pay_mode and tax_percent =  '5.00'),0.00) as amountSub5,coalesce((select sum(amount) from sold_items B where B.invoice_date = A.invoice_date and B.pay_mode=A.pay_mode and tax_percent =  '14.00'),0.00) as amountSub14,pay_mode from sold_items A where invoice_date between '2013/11/24' and '2013/11/26' ORDER BY invoice_date


below is my explain plan...
id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, 'PRIMARY', 'A', 'ALL', '', '', '', '', 102746, 'Using where; Using temporary; Using filesort'
4, 'DEPENDENT SUBQUERY', 'B', 'ALL', '', '', '', '', 102746, 'Using where'
3, 'DEPENDENT SUBQUERY', 'B', 'ALL', '', '', '', '', 102746, 'Using where'
2, 'DEPENDENT SUBQUERY', 'B', 'ALL', '', '', '', '', 102746, 'Using where'
Posted
Updated 26-Nov-13 2:30am
v2
Comments
Herman<T>.Instance 26-Nov-13 7:32am    
You are doing a lot of subqueries with SUM argument. Why not first store the result of each SUM() part in a temporary variable and then do the select.

Try to put an index on every field involved in your complex query, and see if it does the trick.
I would use phpmyadmin to do that.
 
Share this answer
 
Comments
[no name] 26-Nov-13 7:29am    
putting the index is to be done on which side programming or database?
phil.o 26-Nov-13 7:52am    
Database one :)
It's a static configuration, I mean you do it once and will not have to come back to it later.
Please execute the below and paste the output


explain select distinct invoice_date,coalesce((select sum(amount) from sold_items B where B.invoice_date = A.invoice_date and B.pay_mode=A.pay_mode and tax_percent = '00.00'),0.00) as taxFree,coalesce((select sum(amount) from sold_items B where B.invoice_date = A.invoice_date and B.pay_mode=A.pay_mode and tax_percent =  '5.00'),0.00) as amountSub5,coalesce((select sum(amount) from sold_items B where B.invoice_date = A.invoice_date and B.pay_mode=A.pay_mode and tax_percent =  '14.00'),0.00) as amountSub14,pay_mode from sold_items A where invoice_date between '2013/11/24' and '2013/11/26' ORDER BY invoice_date.
 
Share this answer
 
Comments
CHill60 26-Nov-13 7:54am    
If you have a question or comment for the OP then use the "Have a Question or Comment?" link. Do not post comments or questions as Solutions. Please remove both of these solutions
try to make subtables then give each one to primary key n foriegn key for access. then alternatively u can reduce the complexity... thanku
 
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