SQL Server - 2008 : Nested Select, need improve execution faster?










0















Here the problem:



  • First I have a nested query, for which I need to improve the execution time and make it faster. Imagine 47 seconds at first time and second time was 46 / 45 second. Second was running by cache (the second execution always faster than first time but not this).


  • Second that query uses SUM, CAST, CASE in each sub select. Can't dodge that


  • Third I read some another solution from internet using OPTION (FAST 500).


Here the example :



SELECT [cutomerSection],
SUM(DEBIT) AS DEBIT,
SUM(CREDIT) AS CREDIT,
SUM([OTHERS]) AS 'OTHERS'
FROM
(-- SECTOR A
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN j.paymentStat = 'DEBIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN j.paymentStat = 'CREDIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (j.paymentStat = ''
OR j.paymentStat IS NULL) THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbactDetail d WITH(NOLOCK)
LEFT JOIN tbsectorSection j WITH(NOLOCK) ON d.tranCode = j.tranCode
LEFT JOIN tbact t ON (d.actCode=t.actCode
AND t.sectorCode = j.sectorCode)
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
LEFT JOIN tbpaymentList cb WITH(NOLOCK) ON cb.paymentK = a.paymentK
LEFT JOIN tbleading dc ON dc.leadCode = j.leadCode
LEFT JOIN tbsector p ON j.sectorCode = p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted = 0)
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
j.paymentStat
UNION ALL -- SECTOR PARK
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbparkDetail d
LEFT JOIN tbTranPark t ON d.tranCode = t.tranCode
JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted=0)
AND (t.strukPoli IS NOT NULL
OR t.strukPoli <> ''
OR t.strukPoli <> '--')
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat
UNION ALL -- SECTOR FRONT
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbDetailRadioterapi d
LEFT JOIN tbtranFront t ON d.tranCode = t.tranCode
LEFT JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo = j.struk
LEFT JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (t.deleted IS NULL
OR t.deleted=0)
AND (strukPoli IS NOT NULL
OR strukPoli <>''
OR strukPoli <>'--')
AND a.status='FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat) AS a
GROUP BY [cutomerSection]


Third using option (fast 500), I don't know why but always got error :




Incorect Syntax near OPTION




PS : Sorry for my bad English










share|improve this question
























  • When problem solving, your first step is to break into pieces and test individual pieces. First test each piece (3) of the UNION ALL and work out if any one of them contributes to the time taken. Also use CTRL-L and see if it suggests an index

    – Nick.McDermaid
    Nov 13 '18 at 3:23











  • @TogaNama there are some factors to optimize a query. It also depends on your database structure. To improve and optimize the query you need to manage or create indexes on tables and then try to refactor your query.

    – Mohammad Mohabbati
    Nov 13 '18 at 5:16















0















Here the problem:



  • First I have a nested query, for which I need to improve the execution time and make it faster. Imagine 47 seconds at first time and second time was 46 / 45 second. Second was running by cache (the second execution always faster than first time but not this).


  • Second that query uses SUM, CAST, CASE in each sub select. Can't dodge that


  • Third I read some another solution from internet using OPTION (FAST 500).


Here the example :



SELECT [cutomerSection],
SUM(DEBIT) AS DEBIT,
SUM(CREDIT) AS CREDIT,
SUM([OTHERS]) AS 'OTHERS'
FROM
(-- SECTOR A
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN j.paymentStat = 'DEBIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN j.paymentStat = 'CREDIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (j.paymentStat = ''
OR j.paymentStat IS NULL) THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbactDetail d WITH(NOLOCK)
LEFT JOIN tbsectorSection j WITH(NOLOCK) ON d.tranCode = j.tranCode
LEFT JOIN tbact t ON (d.actCode=t.actCode
AND t.sectorCode = j.sectorCode)
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
LEFT JOIN tbpaymentList cb WITH(NOLOCK) ON cb.paymentK = a.paymentK
LEFT JOIN tbleading dc ON dc.leadCode = j.leadCode
LEFT JOIN tbsector p ON j.sectorCode = p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted = 0)
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
j.paymentStat
UNION ALL -- SECTOR PARK
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbparkDetail d
LEFT JOIN tbTranPark t ON d.tranCode = t.tranCode
JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted=0)
AND (t.strukPoli IS NOT NULL
OR t.strukPoli <> ''
OR t.strukPoli <> '--')
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat
UNION ALL -- SECTOR FRONT
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbDetailRadioterapi d
LEFT JOIN tbtranFront t ON d.tranCode = t.tranCode
LEFT JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo = j.struk
LEFT JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (t.deleted IS NULL
OR t.deleted=0)
AND (strukPoli IS NOT NULL
OR strukPoli <>''
OR strukPoli <>'--')
AND a.status='FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat) AS a
GROUP BY [cutomerSection]


Third using option (fast 500), I don't know why but always got error :




Incorect Syntax near OPTION




PS : Sorry for my bad English










share|improve this question
























  • When problem solving, your first step is to break into pieces and test individual pieces. First test each piece (3) of the UNION ALL and work out if any one of them contributes to the time taken. Also use CTRL-L and see if it suggests an index

    – Nick.McDermaid
    Nov 13 '18 at 3:23











  • @TogaNama there are some factors to optimize a query. It also depends on your database structure. To improve and optimize the query you need to manage or create indexes on tables and then try to refactor your query.

    – Mohammad Mohabbati
    Nov 13 '18 at 5:16













0












0








0








Here the problem:



  • First I have a nested query, for which I need to improve the execution time and make it faster. Imagine 47 seconds at first time and second time was 46 / 45 second. Second was running by cache (the second execution always faster than first time but not this).


  • Second that query uses SUM, CAST, CASE in each sub select. Can't dodge that


  • Third I read some another solution from internet using OPTION (FAST 500).


Here the example :



SELECT [cutomerSection],
SUM(DEBIT) AS DEBIT,
SUM(CREDIT) AS CREDIT,
SUM([OTHERS]) AS 'OTHERS'
FROM
(-- SECTOR A
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN j.paymentStat = 'DEBIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN j.paymentStat = 'CREDIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (j.paymentStat = ''
OR j.paymentStat IS NULL) THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbactDetail d WITH(NOLOCK)
LEFT JOIN tbsectorSection j WITH(NOLOCK) ON d.tranCode = j.tranCode
LEFT JOIN tbact t ON (d.actCode=t.actCode
AND t.sectorCode = j.sectorCode)
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
LEFT JOIN tbpaymentList cb WITH(NOLOCK) ON cb.paymentK = a.paymentK
LEFT JOIN tbleading dc ON dc.leadCode = j.leadCode
LEFT JOIN tbsector p ON j.sectorCode = p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted = 0)
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
j.paymentStat
UNION ALL -- SECTOR PARK
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbparkDetail d
LEFT JOIN tbTranPark t ON d.tranCode = t.tranCode
JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted=0)
AND (t.strukPoli IS NOT NULL
OR t.strukPoli <> ''
OR t.strukPoli <> '--')
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat
UNION ALL -- SECTOR FRONT
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbDetailRadioterapi d
LEFT JOIN tbtranFront t ON d.tranCode = t.tranCode
LEFT JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo = j.struk
LEFT JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (t.deleted IS NULL
OR t.deleted=0)
AND (strukPoli IS NOT NULL
OR strukPoli <>''
OR strukPoli <>'--')
AND a.status='FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat) AS a
GROUP BY [cutomerSection]


Third using option (fast 500), I don't know why but always got error :




Incorect Syntax near OPTION




PS : Sorry for my bad English










share|improve this question
















Here the problem:



  • First I have a nested query, for which I need to improve the execution time and make it faster. Imagine 47 seconds at first time and second time was 46 / 45 second. Second was running by cache (the second execution always faster than first time but not this).


  • Second that query uses SUM, CAST, CASE in each sub select. Can't dodge that


  • Third I read some another solution from internet using OPTION (FAST 500).


Here the example :



SELECT [cutomerSection],
SUM(DEBIT) AS DEBIT,
SUM(CREDIT) AS CREDIT,
SUM([OTHERS]) AS 'OTHERS'
FROM
(-- SECTOR A
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN j.paymentStat = 'DEBIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN j.paymentStat = 'CREDIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (j.paymentStat = ''
OR j.paymentStat IS NULL) THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbactDetail d WITH(NOLOCK)
LEFT JOIN tbsectorSection j WITH(NOLOCK) ON d.tranCode = j.tranCode
LEFT JOIN tbact t ON (d.actCode=t.actCode
AND t.sectorCode = j.sectorCode)
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
LEFT JOIN tbpaymentList cb WITH(NOLOCK) ON cb.paymentK = a.paymentK
LEFT JOIN tbleading dc ON dc.leadCode = j.leadCode
LEFT JOIN tbsector p ON j.sectorCode = p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted = 0)
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
j.paymentStat
UNION ALL -- SECTOR PARK
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbparkDetail d
LEFT JOIN tbTranPark t ON d.tranCode = t.tranCode
JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted=0)
AND (t.strukPoli IS NOT NULL
OR t.strukPoli <> ''
OR t.strukPoli <> '--')
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat
UNION ALL -- SECTOR FRONT
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbDetailRadioterapi d
LEFT JOIN tbtranFront t ON d.tranCode = t.tranCode
LEFT JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo = j.struk
LEFT JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (t.deleted IS NULL
OR t.deleted=0)
AND (strukPoli IS NOT NULL
OR strukPoli <>''
OR strukPoli <>'--')
AND a.status='FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat) AS a
GROUP BY [cutomerSection]


Third using option (fast 500), I don't know why but always got error :




Incorect Syntax near OPTION




PS : Sorry for my bad English







sql sql-server performance sql-server-2008






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:43









marc_s

575k12811091256




575k12811091256










asked Nov 13 '18 at 2:44









Toga NamaToga Nama

34




34












  • When problem solving, your first step is to break into pieces and test individual pieces. First test each piece (3) of the UNION ALL and work out if any one of them contributes to the time taken. Also use CTRL-L and see if it suggests an index

    – Nick.McDermaid
    Nov 13 '18 at 3:23











  • @TogaNama there are some factors to optimize a query. It also depends on your database structure. To improve and optimize the query you need to manage or create indexes on tables and then try to refactor your query.

    – Mohammad Mohabbati
    Nov 13 '18 at 5:16

















  • When problem solving, your first step is to break into pieces and test individual pieces. First test each piece (3) of the UNION ALL and work out if any one of them contributes to the time taken. Also use CTRL-L and see if it suggests an index

    – Nick.McDermaid
    Nov 13 '18 at 3:23











  • @TogaNama there are some factors to optimize a query. It also depends on your database structure. To improve and optimize the query you need to manage or create indexes on tables and then try to refactor your query.

    – Mohammad Mohabbati
    Nov 13 '18 at 5:16
















When problem solving, your first step is to break into pieces and test individual pieces. First test each piece (3) of the UNION ALL and work out if any one of them contributes to the time taken. Also use CTRL-L and see if it suggests an index

– Nick.McDermaid
Nov 13 '18 at 3:23





When problem solving, your first step is to break into pieces and test individual pieces. First test each piece (3) of the UNION ALL and work out if any one of them contributes to the time taken. Also use CTRL-L and see if it suggests an index

– Nick.McDermaid
Nov 13 '18 at 3:23













@TogaNama there are some factors to optimize a query. It also depends on your database structure. To improve and optimize the query you need to manage or create indexes on tables and then try to refactor your query.

– Mohammad Mohabbati
Nov 13 '18 at 5:16





@TogaNama there are some factors to optimize a query. It also depends on your database structure. To improve and optimize the query you need to manage or create indexes on tables and then try to refactor your query.

– Mohammad Mohabbati
Nov 13 '18 at 5:16












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273032%2fsql-server-2008-nested-select-need-improve-execution-faster%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273032%2fsql-server-2008-nested-select-need-improve-execution-faster%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus