on 2010 Jun 30 4:09 PM
So I am trying to optimize this query so it can run more efficient... I feel that the nested query is what is really dragging the time on. Any suggestions to optimize it?
SELECT
CAST (f.pc_id AS INT) AS 'PC_ID'
,42 AS account_id
,CAST (CAST ([year] AS VARCHAR (4)) + RIGHT('0' + CAST (period AS VARCHAR (2)), 2) AS INT) AS period_pk
,CASE WHEN f.totalSales <> 0
THEN ( SELECT SUM(amount)
FROM dbo.factActualData AS fa with (nolock)
WHERE f.pc_id = fa.pc_id
AND f.year = fa.year
AND fa.period = fa.period
AND fa.account_id in (8,34)) / f.totalSales
ELSE 0
END AS 'amount'
FROM dbo.factActualData AS f WITH (NOLOCK)
inner join dbo.factActualData as f1 with (NOLOCK)
WHERE (CAST (CAST ([year] AS VARCHAR (4)) + RIGHT('0' + CAST (period AS VARCHAR (2)), 2) AS INT))
<> (SELECT FISCAL_PERIOD_PK FROM bs_tracking.CurrentFiscalPeriod WITH (NOLOCK))
Request clarification before answering.
What SQL Anywhere version are you running?
Can you post a graphical plan with statistics of the query, along with the CREATE TABLE statements for the 3 tables involved?
I also note you are using the "dbo" userid - this isn't recommended. The "dbo" user is used as a system userid in SQL Anywhere that has fewer restrictions than SYS.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
61 | |
8 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.