Showing posts with label improve. Show all posts
Showing posts with label improve. Show all posts

Wednesday, March 21, 2012

how to improve this transaction

Hello,

I have four different transactions such as below and I do one insert and one update in each transaction and it seem it is slow and creates deadlock with the user interface.

These transactions are performed against the tables that users are accessing with another user interface. I have following two questions:

1. T2.TextField1 and TextField2 = @.TextField2 are Ok, Nok fields so I did not put index since only two distinct values. Should I put indexes on these fields?

2. Can I make this transaction let user interface do its task in case accessing the same rows, I can start transaction again but I do not want users get disturbed?

.

BEGIN TRANSACTION pTrans

BEGIN

INSERT INTO T1

(fields)

SELECT (fields)

FROM T2 INNER JOIN View1 ON T2.TrID = View1.MyTableID

WHERE (T2.TextField1 = @.TrType AND T2.TextField2 = @.TextField2)

UPDATE T2

SET TextField2 = 'Ok', TextField2Date=@.MyRunDateTime

FROM T2

WHERE (TextField1 = @.TrType AND TextField2 = @.TextField2)

IF @.@.ERROR <> 0

BEGIN

rollback transaction pTrans

return(-1)

END

ELSE

BEGIN

commit transaction pTrans

END

END

What's the schemas of the 3 tables T1,T2 and View1? I tried a test script using your statements, the query went fine and no deadlock occurred.

Q1. Do not put index on low selective column (as you said only 2 distinct values), because it dosen't make sense. Here are some guidelines for choosing columns to index from "Programming a Microsoft? SQL Server? 2000 Database":

Indexes are useful, but they consume disk space and incur overhead and maintenance costs. Consider the following facts and guidelines about indexes:

When you modify data on an indexed column, SQL Server updates the associated indexes.
|||

Thanks for your help, can you let me know how I can make this transaction low isolation level? I am quite new in the transaction isolation level.

|||

Simply use the SET TRANSACTION ISOLATION LEVEL command to set the session option, you can refer to this link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_set-set_74bw.asp

how to improve this transaction

Hello,

I have four different transactions such as below and I do one insert and one update in each transaction and it seem it is slow and creates deadlock with the user interface.

These transactions are performed against the tables that users are accessing with another user interface. I have following two questions:

1. T2.TextField1 and TextField2 = @.TextField2 are Ok, Nok fields so I did not put index since only two distinct values. Should I put indexes on these fields?

2. Can I make this transaction let user interface do its task in case accessing the same rows, I can start transaction again but I do not want users get disturbed?

.

BEGIN TRANSACTION pTrans

BEGIN

INSERT INTO T1

(fields)

SELECT(fields)

FROMT2 INNER JOIN View1 ON T2.TrID = View1.MyTableID

WHERE (T2.TextField1 = @.TrType AND T2.TextField2 = @.TextField2)

UPDATET2

SETTextField2 = 'Ok',TextField2Date=@.MyRunDateTime

FROMT2

WHERE (TextField1 = @.TrType AND TextField2 = @.TextField2)

IF @.@.ERROR <> 0

BEGIN

rollback transaction pTrans

return(-1)

END

ELSE

BEGIN

commit transaction pTrans

END

END

i think youre just maintaing the data on T2

based from the inserted values on T1.

how about just doing the insert on T1

and placing the update codes on

an "after insert trigger" in t1.

a transaction may not be necessary in this case

the previous design will cause a lot of deadlock in the system

|||

Jim,

You should consider defining indexes on fields listed in "where" clause.
If the problem persists after that, then more detailed examination will be needed.

Indeed, you can invoke asynchronous calls to SQL Server. You can use separate thread to handle the calls to SQL, or you can use built-in support for asynchronous SQL calls. For example, look at SqlCommand.BeginExecuteNonQuery Method at MSDN.

How to improve this SQL statement?

I have a query in MS Access that will select the only record from a table DECISION, given three input parameters: pMonth, pLoansize and pLTV. The record should have the maximal possible value for MONTH, LOAN_SIZE and PERCENTAGE, in this order.

My query is working, but a bit slow since it seems joining four tables. I'd appreicate for any idea about how to improve this query, which is as
follow. Thanks.

------------------------
SELECT distinct C.DECISION_ID, C.DECISION_NAME_ID, C.MONTH, C.LOAN_SIZE, C.percentage, C.ADMIN_FEE_HL_ID, C.PNTY_ID, C.OFFER_RATE_ID
FROM DECISION AS A, DECISION AS B, DECISION AS C, DECISION AS D
WHERE
(D.MONTH = (SELECT max(MONTH) FROM DECISION D WHERE pLoansize>=D.LOAN_SIZEand pMONTH >= D.MONTH AND pLTV >= D.percentage))
AND
(B.LOAN_SIZE= (SELECT max(loan_size) FROM DECISION B WHERE B.MONTH = D.MONTH AND pLoansize>=B.LOAN_SIZEAND pLTV >= B.percentage))
AND
(A.percentage = (SELECT max(percentage) FROM DECISION A WHERE A.MONTH = D.MONTH and A.loan_size=B.LOAN_SIZE
AND pLTV >= A.percentage))
AND C.MONTH = D.MONTH
AND C.LOAN_SIZE= B.loan_size
AND C.percentage = A.percentagetotal shot in the dark, please let me know if this works:SELECT DECISION_ID
, DECISION_NAME_ID
, MONTH
, LOAN_SIZE
, percentage
, ADMIN_FEE_HL_ID
, PNTY_ID
, OFFER_RATE_ID
FROM DECISION AS A
WHERE MONTH = (
SELECT max(MONTH)
FROM DECISION
)
AND LOAN_SIZE = (
SELECT max(loan_size)
FROM DECISION
WHERE MONTH = A.MONTH
)
AND percentage = (
SELECT max(percentage)
FROM DECISION
WHERE MONTH = A.MONTH
and loan_size = a.LOAN_SIZE
)
rudy
http://r937.com/|||Hi r937,

Thanks for the try. But I am afraid that it is not working. Your SQL may end up with no record selected. For example, a simplified DECISION table:

Month, Loan_size, Percentage
8, 100, 70
4, 200, 50
6, 150, 80|||did you try it?

i tried it on your sample of 3 and it gave the row with

8, 100, 70

if you just wanted the max of all three colulmns, that's a totally different query (and a lot simpler, too)

i thought you wanted row integrity

for example, suppose there are 80 rows in the table, of which 20 belong to the highest month

then out of those 20 rows which have the highest month, 6 of those rows have the highest loan_size for that month

then out of those 6 rows which have the highest loan_size for the highest month, one of them has the highest percentage

my query will always return a row, as long as there is at least one row in the table

perhaps you did not explain your problem correctly?

i tried to see what your query was attempting to do, but it's seriously messed up

or maybe i totally misunderstood you

rudy|||Hi Rudy,

Sorry it was my mistake. The query is working now since I missed
out something. Thank you very much.

How to improve this query?

Aliquot table contains columns PatientID, AliquotTypeID, LocationCode,
AliquotNumber. The query is to get the locationCode with minimum
AliquotNumber in a (PatientID and AliquotTypeID) Group.
select locationCode from aliquot a
where a.patientID = 1 and aliquotTypeID = 1 and
a.aliquotNumber = (Select min(aliquotNumber) from aliquot where patientID =
1 and aliquotTypeID= 1 and locationCode is not null)
thanks,Try:
SELECT LOCATIONCODE, MIN(ALIQUOTNUMBER)
FROM ALIQUOT
WHERE PATIENTID = 1 AND ALIQUOTTYPEID = 1 AND LOCATIONCODE IS NOT NULL
GROUP BY LOCATIONCODE
HTH
Jerry
"Caspy" <caspases@.yahoo.com> wrote in message
news:O2fa1rQyFHA.700@.TK2MSFTNGP11.phx.gbl...
> Aliquot table contains columns PatientID, AliquotTypeID, LocationCode,
> AliquotNumber. The query is to get the locationCode with minimum
> AliquotNumber in a (PatientID and AliquotTypeID) Group.
> select locationCode from aliquot a
> where a.patientID = 1 and aliquotTypeID = 1 and
> a.aliquotNumber = (Select min(aliquotNumber) from aliquot where patientID
> = 1 and aliquotTypeID= 1 and locationCode is not null)
> thanks,
>
>|||Thanks for your reply. Because the locationCode is unique for each
AliquotNumber, your query returns multiple rows.
Since the Aliquot table has half million tupples, I just want to limit to 1
visit to this big table when try to retrieve the locationCode with minimum
aliquotNumber in a (patientID and AliquotTypeID) group.
"Jerry Spivey" <jspivey@.vestas-awt.com> wrote in message
news:%23lXiB3QyFHA.1256@.TK2MSFTNGP09.phx.gbl...
> Try:
> SELECT LOCATIONCODE, MIN(ALIQUOTNUMBER)
> FROM ALIQUOT
> WHERE PATIENTID = 1 AND ALIQUOTTYPEID = 1 AND LOCATIONCODE IS NOT NULL
> GROUP BY LOCATIONCODE
> HTH
> Jerry
> "Caspy" <caspases@.yahoo.com> wrote in message
> news:O2fa1rQyFHA.700@.TK2MSFTNGP11.phx.gbl...
>

how to improve this query, thanks

I have a pretty good db server with four CPUs, it has not any other loads on it, but the following query takes 4ms to return. I use the syscolumns this way quite often, I am not sure why it takes it that long to return, any idea?

select 'master',id,colid,name,xtype,length,xprec,xscale,status from [ablestatic].[dbo].syscolumns
where id=(select id from [ablestatic].[dbo].sysobjects where name='link_data_ezregs')

You might want to run profiler to see whats happening. The query by itself doesnt seem to be doing much. See if this makes any diff (although it shoudnt)

SELECT 'master',id,colid,name,xtype,length,xprec,xscale,status

FROM [ablestatic].[dbo].syscolumns sc

JOIN sysobjects so on sc.id = so.id
WHERE so.name='link_data_ezregs' and so.xtype = 'U'

|||

thanks.

I got the 2 - 4 ms when I run the query in one of my SP and watch it with profiler.

If I run it in query analyzer and in profile I got 15 ms.

|||15ms = milli secs or minutes?|||

millisecond for sure

|||I doubt if theres much you can do. you could use the stored proc as you suggested but even T-SQL should be as efficient, if its a single T-SQL statement.|||Check the execution plan when the query is executed inside/outside a sp, it may be caused by parameter sniffing, here are some related articles:

http://forums.asp.net/2/1377161/ShowThread.aspx

Understanding Plan Guides

how to improve the speed of sort?

i have found the speed of sort is very slow in my sql (because sql is very complicated, i can't paste it on this page), how can i improve the speed of sort ?

are there better methods?

thks

Look at the execution plan if it shows slow retrieval speed on the ordered columns. If so, you should think about using indexes on the appropiate columns. But you should be able to see this within the execution plan which is a accessible through the Query editor.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||i have saw the execution plan, and i have build index for the table, but i must query result from sub query, the sub query spent most of time. i don't know how to let the sub query more quick?|||The best thing would be *eventually *to use correlated queries, but its hard to tell without knowing the query. Is it not possible to post it in here to help you ?

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||

--Comment: the last aggregate query, it spents 4.44 minutes
--Start for ObjectAgg:LocalNetID,BSCID,SiteID,CellID,StrTime
select
LocalNetID,BSCID,SiteID,CellID,
StrTime,
sum([11601]) as '11601', sum([11602]) as '11602', sum([11603]) as '11603', sum([11604]) as '11604', sum([11605]) as '11605', sum([11606]) as '11606', sum([116060]) as '116060', sum([116061]) as '116061', sum([11607]) as '11607', sum([11608]) as '11608', sum([11609]) as '11609', sum([11610]) as '11610', sum([116100]) as '116100', sum([116101]) as '116101', sum([116102]) as '116102', sum([116103]) as '116103', sum([116104]) as '116104', sum([116105]) as '116105', sum([116106]) as '116106', sum([116107]) as '116107', sum([116108]) as '116108', sum([116109]) as '116109', sum([11611]) as '11611', sum([116110]) as '116110', sum([116111]) as '116111', sum([116112]) as '116112', sum([116113]) as '116113', sum([116114]) as '116114', sum([116115]) as '116115', sum([116116]) as '116116', sum([116117]) as '116117', sum([116118]) as '116118', sum([116119]) as '116119', sum([11612]) as '11612', sum([116120]) as '116120', sum([116121]) as '116121', sum([116122]) as '116122', sum([116123]) as '116123', sum([116124]) as '116124', sum([116125]) as '116125', sum([116126]) as '116126', sum([116127]) as '116127', sum([116128]) as '116128', sum([116129]) as '116129', sum([11613]) as '11613', sum([116130]) as '116130', sum([116131]) as '116131', sum([116132]) as '116132', sum([116133]) as '116133', sum([116134]) as '116134', sum([116135]) as '116135', sum([116136]) as '116136', sum([11614]) as '11614', sum([11615]) as '11615', sum([11616]) as '11616', sum([116160]) as '116160', sum([116161]) as '116161', sum([11617]) as '11617', sum([11618]) as '11618', sum([11619]) as '11619', sum([11620]) as '11620', sum([11621]) as '11621', sum([11622]) as '11622', sum([11623]) as '11623', sum([11624]) as '11624', sum([11625]) as '11625', sum([11626]) as '11626', sum([11627]) as '11627', sum([11628]) as '11628', sum([11629]) as '11629', sum([11630]) as '11630', sum([11631]) as '11631', sum([11632]) as '11632', sum([11633]) as '11633', sum([11634]) as '11634', sum([11635]) as '11635', sum([11636]) as '11636', sum([11637]) as '11637', sum([11638]) as '11638', sum([11639]) as '11639', sum([11640]) as '11640', sum([11641]) as '11641', sum([11642]) as '11642', sum([11643]) as '11643', sum([11644]) as '11644', sum([11645]) as '11645', sum([11646]) as '11646', sum([11647]) as '11647', sum([11648]) as '11648', sum([11649]) as '11649', sum([11650]) as '11650', sum([11651]) as '11651', sum([11652]) as '11652', sum([11653]) as '11653', sum([11654]) as '11654', sum([11655]) as '11655', sum([11656]) as '11656', sum([11657]) as '11657', sum([11658]) as '11658', sum([11659]) as '11659', sum([11660]) as '11660', sum([11661]) as '11661', sum([11662]) as '11662', sum([11663]) as '11663', sum([11664]) as '11664', sum([11665]) as '11665', sum([11666]) as '11666', sum([11667]) as '11667', sum([11668]) as '11668', sum([11669]) as '11669', sum([11670]) as '11670', sum([11671]) as '11671', sum([11672]) as '11672', sum([11673]) as '11673', sum([11674]) as '11674', sum([11675]) as '11675', sum([11676]) as '11676', sum([11677]) as '11677', sum([11678]) as '11678', sum([11679]) as '11679', sum([11680]) as '11680', sum([11681]) as '11681', sum([11682]) as '11682', sum([11683]) as '11683', sum([11684]) as '11684', sum([11685]) as '11685', sum([11686]) as '11686', sum([11687]) as '11687', sum([11688]) as '11688', sum([11689]) as '11689', sum([11690]) as '11690', sum([11691]) as '11691', sum([11692]) as '11692', sum([11693]) as '11693', sum([11694]) as '11694', sum([11695]) as '11695', sum([11696]) as '11696', sum([11697]) as '11697', sum([11698]) as '11698', sum([11699]) as '11699'
from
(
--Comment:the second aggreage query, it spends 2 seconds
--Start for TimeAgg:LocalNetID,BSCID,SiteID,CellID,left(StrTime,12)
select
LocalNetID,BSCID,SiteID,CellID,
left(StrTime,12) as StrTime,
avg([11601] * 1.0) as '11601', avg([11602] * 1.0) as '11602', sum([11603]) as '11603', sum([11604]) as '11604', sum([11605]) as '11605', avg([11606] * 1.0) as '11606', sum([116060]) as '116060', avg([116061] * 1.0) as '116061', avg([11607] * 1.0) as '11607', avg([11608] * 1.0) as '11608', sum([11609]) as '11609', sum([11610]) as '11610', sum([116100]) as '116100', sum([116101]) as '116101', sum([116102]) as '116102', sum([116103]) as '116103', sum([116104]) as '116104', sum([116105]) as '116105', sum([116106]) as '116106', sum([116107]) as '116107', sum([116108]) as '116108', sum([116109]) as '116109', sum([11611]) as '11611', sum([116110]) as '116110', sum([116111]) as '116111', sum([116112]) as '116112', sum([116113]) as '116113', sum([116114]) as '116114', sum([116115]) as '116115', sum([116116]) as '116116', sum([116117]) as '116117', sum([116118]) as '116118', sum([116119]) as '116119', sum([11612]) as '11612', sum([116120]) as '116120', sum([116121]) as '116121', sum([116122]) as '116122', sum([116123]) as '116123', sum([116124]) as '116124', sum([116125]) as '116125', sum([116126]) as '116126', sum([116127]) as '116127', sum([116128]) as '116128', sum([116129]) as '116129', sum([11613]) as '11613', sum([116130]) as '116130', avg([116131] * 1.0) as '116131', sum([116132]) as '116132', sum([116133]) as '116133', sum([116134]) as '116134', sum([116135]) as '116135', sum([116136]) as '116136', sum([11614]) as '11614', sum([11615]) as '11615', avg([11616] * 1.0) as '11616', sum([116160]) as '116160', avg([116161] * 1.0) as '116161', sum([11617]) as '11617', sum([11618]) as '11618', sum([11619]) as '11619', sum([11620]) as '11620', sum([11621]) as '11621', sum([11622]) as '11622', sum([11623]) as '11623', sum([11624]) as '11624', sum([11625]) as '11625', sum([11626]) as '11626', max([11627]) as '11627', avg([11628] * 1.0) as '11628', sum([11629]) as '11629', max([11630]) as '11630', avg([11631] * 1.0) as '11631', sum([11632]) as '11632', avg([11633] * 1.0) as '11633', sum([11634]) as '11634', sum([11635]) as '11635', sum([11636]) as '11636', sum([11637]) as '11637', sum([11638]) as '11638', sum([11639]) as '11639', sum([11640]) as '11640', sum([11641]) as '11641', sum([11642]) as '11642', sum([11643]) as '11643', sum([11644]) as '11644', sum([11645]) as '11645', sum([11646]) as '11646', sum([11647]) as '11647', sum([11648]) as '11648', sum([11649]) as '11649', sum([11650]) as '11650', sum([11651]) as '11651', sum([11652]) as '11652', sum([11653]) as '11653', sum([11654]) as '11654', sum([11655]) as '11655', sum([11656]) as '11656', sum([11657]) as '11657', sum([11658]) as '11658', sum([11659]) as '11659', sum([11660]) as '11660', sum([11661]) as '11661', sum([11662]) as '11662', sum([11663]) as '11663', sum([11664]) as '11664', sum([11665]) as '11665', sum([11666]) as '11666', sum([11667]) as '11667', sum([11668]) as '11668', sum([11669]) as '11669', avg([11670] * 1.0) as '11670', avg([11671] * 1.0) as '11671', avg([11672] * 1.0) as '11672', avg([11673] * 1.0) as '11673', avg([11674] * 1.0) as '11674', sum([11675]) as '11675', sum([11676]) as '11676', sum([11677]) as '11677', sum([11678]) as '11678', sum([11679]) as '11679', sum([11680]) as '11680', avg([11681] * 1.0) as '11681', sum([11682]) as '11682', sum([11683]) as '11683', sum([11684]) as '11684', sum([11685]) as '11685', sum([11686]) as '11686', sum([11687]) as '11687', sum([11688]) as '11688', sum([11689]) as '11689', sum([11690]) as '11690', sum([11691]) as '11691', sum([11692]) as '11692', sum([11693]) as '11693', sum([11694]) as '11694', sum([11695]) as '11695', sum([11696]) as '11696', sum([11697]) as '11697', sum([11698]) as '11698', sum([11699]) as '11699'
from
(
--Comment:first aggregate query, the speed is very quick and it spends no time
--Start for TimeAgg:LocalNetID,BSCID,SiteID,CellID,left(StrTime,14)
select
LocalNetID,BSCID,SiteID,CellID,
left(StrTime,14) as StrTime,
avg([11601] * 1.0) as '11601', avg([11602] * 1.0) as '11602', sum([11603]) as '11603', sum([11604]) as '11604', sum([11605]) as '11605', sum([11606]) as '11606', sum([116060]) as '116060', avg([116061] * 1.0) as '116061', avg([11607] * 1.0) as '11607', avg([11608] * 1.0) as '11608', sum([11609]) as '11609', sum([11610]) as '11610', sum([116100]) as '116100', sum([116101]) as '116101', sum([116102]) as '116102', sum([116103]) as '116103', sum([116104]) as '116104', sum([116105]) as '116105', sum([116106]) as '116106', sum([116107]) as '116107', sum([116108]) as '116108', sum([116109]) as '116109', sum([11611]) as '11611', sum([116110]) as '116110', sum([116111]) as '116111', sum([116112]) as '116112', sum([116113]) as '116113', sum([116114]) as '116114', sum([116115]) as '116115', sum([116116]) as '116116', sum([116117]) as '116117', sum([116118]) as '116118', sum([116119]) as '116119', sum([11612]) as '11612', sum([116120]) as '116120', sum([116121]) as '116121', sum([116122]) as '116122', sum([116123]) as '116123', sum([116124]) as '116124', sum([116125]) as '116125', sum([116126]) as '116126', sum([116127]) as '116127', sum([116128]) as '116128', sum([116129]) as '116129', sum([11613]) as '11613', sum([116130]) as '116130', sum([116131]) as '116131', sum([116132]) as '116132', sum([116133]) as '116133', sum([116134]) as '116134', sum([116135]) as '116135', sum([116136]) as '116136', sum([11614]) as '11614', sum([11615]) as '11615', sum([11616]) as '11616', sum([116160]) as '116160', avg([116161] * 1.0) as '116161', sum([11617]) as '11617', sum([11618]) as '11618', sum([11619]) as '11619', sum([11620]) as '11620', sum([11621]) as '11621', sum([11622]) as '11622', sum([11623]) as '11623', sum([11624]) as '11624', sum([11625]) as '11625', sum([11626]) as '11626', max([11627]) as '11627', avg([11628] * 1.0) as '11628', sum([11629]) as '11629', max([11630]) as '11630', avg([11631] * 1.0) as '11631', sum([11632]) as '11632', avg([11633] * 1.0) as '11633', sum([11634]) as '11634', sum([11635]) as '11635', sum([11636]) as '11636', sum([11637]) as '11637', sum([11638]) as '11638', sum([11639]) as '11639', sum([11640]) as '11640', sum([11641]) as '11641', sum([11642]) as '11642', sum([11643]) as '11643', sum([11644]) as '11644', sum([11645]) as '11645', sum([11646]) as '11646', sum([11647]) as '11647', sum([11648]) as '11648', sum([11649]) as '11649', sum([11650]) as '11650', sum([11651]) as '11651', sum([11652]) as '11652', sum([11653]) as '11653', sum([11654]) as '11654', sum([11655]) as '11655', sum([11656]) as '11656', sum([11657]) as '11657', sum([11658]) as '11658', sum([11659]) as '11659', sum([11660]) as '11660', sum([11661]) as '11661', sum([11662]) as '11662', sum([11663]) as '11663', sum([11664]) as '11664', sum([11665]) as '11665', sum([11666]) as '11666', sum([11667]) as '11667', sum([11668]) as '11668', sum([11669]) as '11669', avg([11670] * 1.0) as '11670', avg([11671] * 1.0) as '11671', avg([11672] * 1.0) as '11672', avg([11673] * 1.0) as '11673', avg([11674] * 1.0) as '11674', sum([11675]) as '11675', sum([11676]) as '11676', sum([11677]) as '11677', sum([11678]) as '11678', sum([11679]) as '11679', sum([11680]) as '11680', avg([11681] * 1.0) as '11681', sum([11682]) as '11682', sum([11683]) as '11683', sum([11684]) as '11684', sum([11685]) as '11685', sum([11686]) as '11686', sum([11687]) as '11687', sum([11688]) as '11688', sum([11689]) as '11689', sum([11690]) as '11690', sum([11691]) as '11691', sum([11692]) as '11692', sum([11693]) as '11693', sum([11694]) as '11694', sum([11695]) as '11695', sum([11696]) as '11696', sum([11697]) as '11697', sum([11698]) as '11698', sum([11699]) as '11699'
from
(
--Start for first join--
select
coalesce(T1.LocalNetID,NULL)as LocalNetID,coalesce(T1.BSCID,NULL)as BSCID,coalesce(T1.SiteID,NULL)as SiteID,coalesce(T1.CellID,NULL)as CellID,
coalesce(T1.StrTime,NULL) as StrTime,
C11601 as '11601',C11602 as '11602',C11603 as '11603',C11604 as '11604',C11605 as '11605',C11606 as '11606',I116061 as '116060',C11606 as '116061',C11607 as '11607',C11608 as '11608',C11609 as '11609',C11610 as '11610',C116100 as '116100',C116101 as '116101',C116102 as '116102',C116103 as '116103',C116104 as '116104',C116105 as '116105',C116106 as '116106',C116107 as '116107',C116108 as '116108',C116109 as '116109',C11611 as '11611',C116110 as '116110',C116111 as '116111',C116112 as '116112',C116113 as '116113',C116114 as '116114',C116115 as '116115',C116116 as '116116',C116117 as '116117',C116118 as '116118',C116119 as '116119',C11612 as '11612',C116120 as '116120',C116121 as '116121',C116122 as '116122',C116123 as '116123',C116124 as '116124',C116125 as '116125',C116126 as '116126',C116127 as '116127',C116128 as '116128',C116129 as '116129',C11613 as '11613',C116130 as '116130',C116131 as '116131',C116132 as '116132',C116133 as '116133',C116134 as '116134',C116135 as '116135',C116136 as '116136',C11614 as '11614',C11615 as '11615',C11616 as '11616',I116161 as '116160',C11616 as '116161',C11617 as '11617',C11618 as '11618',C11619 as '11619',C11620 as '11620',C11621 as '11621',C11622 as '11622',C11623 as '11623',C11624 as '11624',C11625 as '11625',C11626 as '11626',C11627 as '11627',C11628 as '11628',C11629 as '11629',C11630 as '11630',C11631 as '11631',C11632 as '11632',C11633 as '11633',C11634 as '11634',C11635 as '11635',C11636 as '11636',C11637 as '11637',C11638 as '11638',C11639 as '11639',C11640 as '11640',C11641 as '11641',C11642 as '11642',C11643 as '11643',C11644 as '11644',C11645 as '11645',C11646 as '11646',C11647 as '11647',C11648 as '11648',C11649 as '11649',C11650 as '11650',C11651 as '11651',C11652 as '11652',C11653 as '11653',C11654 as '11654',C11655 as '11655',C11656 as '11656',C11657 as '11657',C11658 as '11658',C11659 as '11659',C11660 as '11660',C11661 as '11661',C11662 as '11662',C11663 as '11663',C11664 as '11664',C11665 as '11665',C11666 as '11666',C11667 as '11667',C11668 as '11668',C11669 as '11669',C11670 as '11670',C11671 as '11671',C11672 as '11672',C11673 as '11673',C11674 as '11674',C11675 as '11675',C11676 as '11676',C11677 as '11677',C11678 as '11678',C11679 as '11679',C11680 as '11680',C11681 as '11681',C11682 as '11682',C11683 as '11683',C11684 as '11684',C11685 as '11685',C11686 as '11686',C11687 as '11687',C11688 as '11688',C11689 as '11689',C11690 as '11690',C11691 as '11691',C11692 as '11692',C11693 as '11693',C11694 as '11694',C11695 as '11695',C11696 as '11696',C11697 as '11697',C11698 as '11698',C11699 as '11699'from
--Start for Opt_Cell_pBasicM of inner table--
(
select
LocalNetID,BSCID,SiteID,CellID,
StrTime,RecordDataTime,
C11601,C11602,C11603,C11604,C11605,C11606,I116061,C11607,C11608,C11609,C11610,C116100,C116101,C116102,C116103,C116104,C116105,C116106,C116107,C116108,C116109,C11611,C116110,C116111,C116112,C116113,C116114,C116115,C116116,C116117,C116118,C116119,C11612,C116120,C116121,C116122,C116123,C116124,C116125,C116126,C116127,C116128,C116129,C11613,C116130,C116131,C116132,C116133,C116134,C116135,C116136,C11614,C11615,C11616,I116161,C11617,C11618,C11619,C11620,C11621,C11622,C11623,C11624,C11625,C11626,C11627,C11628,C11629,C11630,C11631,C11632,C11633,C11634,C11635,C11636,C11637,C11638,C11639,C11640,C11641,C11642,C11643,C11644,C11645,C11646,C11647,C11648,C11649,C11650,C11651,C11652,C11653,C11654,C11655,C11656,C11657,C11658,C11659,C11660,C11661,C11662,C11663,C11664,C11665,C11666,C11667,C11668,C11669,C11670,C11671,C11672,C11673,C11674,C11675,C11676,C11677,C11678,C11679,C11680,C11681,C11682,C11683,C11684,C11685,C11686,C11687,C11688,C11689,C11690,C11691,C11692,C11693,C11694,C11695,C11696,C11697,C11698,C11699
from
Opt_Cell_pBasicM
where
((LocalNetID = 1 and BSCID = 5))
and
((RecordDataTime between '2006-11-16 00:01:00' and '2006-11-16 13:00:00'))
and
(GranularityPeriod <= 12)
)
--End for Opt_Cell_pBasicM of inner table--
as T1
--End for first join--
) as T
group by
LocalNetID,BSCID,SiteID,CellID,
left(StrTime,14)
--End for TimeAgg:LocalNetID,BSCID,SiteID,CellID,left(StrTime,14)
) as T
group by
LocalNetID,BSCID,SiteID,CellID,
left(StrTime,12)
--End for TimeAgg:LocalNetID,BSCID,SiteID,CellID,left(StrTime,12)
) as T
group by
LocalNetID,BSCID,SiteID,CellID,
StrTime

--the above sql's execution time is got when there is no data in table Opt_Cell_pBasicM
--because of no data in table, so i am very curious why the last aggregate query need so
--long time, i have saw the execution plan and found the time is spent on sort, but there is
--no data, why to sort?

How to improve the query performance on cubes?

Hi,all experts here,

How could we effectively improve the query performance on cubes from a client? Can we create indexes on them? Or if not, what else can we try?

Thanks a lot and I am looking forward to hearing from you shortly.

With best regards,

Yours sincerely,

There are a lot of things to try - partitioning, aggregations.... too much to discuss in a forums answer. Your best bet is to read the AS performance guide:

http://www.microsoft.com/technet/prodtechnol/sql/2005/ssas2005perfguide.mspx

Chris

|||

Thanks a lot.

Best regards,

sql

How to improve the query performance for my reports?

Hi, all here,

Thank you very much for your kind attention.

I am having a problem with the performance of my reports. The data for my reports is retrieved from two large transaction joined tables thus resulting in the poor qeury performance for my reports. How can I effectively improve the query performance? It is that I'd better to create a data view for the report data to retrive the data for the reports based on the data view? Or what is the best pratice for it?

Thanks a lot in advance for any guidance and help.

With best regards,

Yours sincerely,

This is the same as tuning any SQL. Drop the SQL from your dataset into SQL Server Management Studio in a New Query and check the Include Actual Execution Plan. Run the query and look at the execution plan. If you see any 'Table Scans' look for ways to eliminate them (different join criteria, different select criteria, create new indexes, etc). If you are using parameters you may need to 'hard code them' or declare SQL variables for them (I prefer the later so the query can be dropped right back into reporting services once I get it tuned).

|||

Hi, Lonnie, thank you very much for your advices.

With best regards,

Yours sincerely,

|||Further to Lonnie's good advice I would also suggest as a general good practice returning as little data to RS as possible. Utilise parameters to filter the query, use a group by to presummarise the data if possible. RS will be performign further processing of your data in ReportServerTempDB for report formatting and paging so reducing the amount of data it needs to deal with can have a huge impact on performance.

How to improve the query performance for my reports?

Hi, all here,

Thank you very much for your kind attention.

I am having a problem with the performance of my reports. The data for my reports is retrieved from two large transaction joined tables thus resulting in the poor qeury performance for my reports. How can I effectively improve the query performance? It is that I'd better to create a data view for the report data to retrive the data for the reports based on the data view? Or what is the best pratice for it?

Thanks a lot in advance for any guidance and help.

With best regards,

Yours sincerely,

This is the same as tuning any SQL. Drop the SQL from your dataset into SQL Server Management Studio in a New Query and check the Include Actual Execution Plan. Run the query and look at the execution plan. If you see any 'Table Scans' look for ways to eliminate them (different join criteria, different select criteria, create new indexes, etc). If you are using parameters you may need to 'hard code them' or declare SQL variables for them (I prefer the later so the query can be dropped right back into reporting services once I get it tuned).

|||

Hi, Lonnie, thank you very much for your advices.

With best regards,

Yours sincerely,

|||Further to Lonnie's good advice I would also suggest as a general good practice returning as little data to RS as possible. Utilise parameters to filter the query, use a group by to presummarise the data if possible. RS will be performign further processing of your data in ReportServerTempDB for report formatting and paging so reducing the amount of data it needs to deal with can have a huge impact on performance.

How to improve the performance,

Hi *.*
I've a problem with my query, perhaps you professionals can kindly
Help me to overcome this, let me explain the problem, We have an
Inventory software with about 8500 of Goods (we stored it in table
Called tblPart), every input/output of these Goods saves in a Table
Called tblInvRate, we have a field called Type, if it's input then
Type=1, if that's output the Type=-1, Qty is the number of input or
Output.
Now with the following query we get the remain of Goods in the
Inventory:
SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
Rate.Type) AS PriorRemain
FROM dbo.tblInvRate Rate INNER JOIN
dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
WHERE Rate.[Date] < '850515'
GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING SUM(Rate.Qty * Rate.Type)>0
This query takes about 1 sec to execute,
In tblInvRate we have a field called IsRated and Rate, Rated and
IsRated sets in a sp named prRatingInv which calculate the rate of the
Goods and...
We need to have the last rate of Goods in the remain query so we use
This:
SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
SUM(Rate.Qty * Rate.Type) AS PriorRemain,
(SELECT TOP 1 Rate
FROM dbo.tblInvRate
WHERE (fkPart = dbo.tblPart.pkCode
AND [Date]<='850515' And IsRated = 1)
ORDER BY [Date] DESC, [Time] DESC) AS
Rate
FROM dbo.tblInvRate Rate INNER JOIN
dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
WHERE Rate.[Date] < '850515'
GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING SUM(Rate.Qty * Rate.Type)>0
Above query may take about 40 to 80 secs to execute!!! (Total number of
rows in the tblInvRate is about 1.000.000 records)
H've run the Index Wizard but it can't find any more index on the
Tables, now would you please help me in this situation,
here is the schema of these tables:
CREATE TABLE [tblPart] (
[pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
NULL ,
[Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
NULL ,
[fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] DEFAULT
(1),
[Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
[TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
NULL ,
[OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
,
[fkPartAccGroup] [int] NULL ,
CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
(
[pkCode]
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [tblInvRate] (
[pkID] [int] NOT NULL ,
[Source] [int] NOT NULL ,
[Dest] [int] NOT NULL ,
[fkInvHdr] [int] NULL ,
[fkInvItm] [int] NULL ,
[fkChangePart] [int] NULL ,
[fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
NULL ,
[Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
[Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
[Qty] [int] NOT NULL ,
[Rate] [decimal](24, 4) NULL ,
[Amount] [decimal](24, 0) NULL ,
[OldRate] [decimal](24, 4) NULL ,
[MainRate] [decimal](24, 4) NULL ,
[Type] [int] NOT NULL ,
[Code] [int] NOT NULL ,
[IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
DEFAULT (0),
[fkCurrency] [int] NULL ,
[CurrencyRate] [int] NULL ,
[CurrencyAmount] [decimal](24, 0) NULL ,
[fkShop] [int] NULL ,
[fkPerson] [int] NULL ,
[RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DEFAULT
(0),
[IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFAULT
(0),
[fkAccVchHdr] [int] NULL ,
[AccTempNum] [int] NULL ,
CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
(
[pkID],
[Source]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
(
[fkChangePart],
[Source]
) REFERENCES [tblChangePart] (
[pkID],
[Source]
) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
(
[fkInvHdr],
[Source]
) REFERENCES [tblInvHdr] (
[pkID],
[Source]
) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
) ON [PRIMARY]
GO
Thanks in advance,
Waiting for your tips and advices,
HamedHamed,
Try using the correlated query after the remain of Goods in the Inventory
has been calculated.
select
a.*,
(
SELECT TOP 1
b.Rate
FROM
dbo.tblInvRate as b
WHERE
b.fkPart = a.pkCode
AND b.[Date] <= '850515'
And b.IsRated = 1
) as Rate
from
(
SELECT
dbo.tblPart.pkCode,
dbo.tblPart.Title,
SUM(Rate.Qty * Rate.Type) AS PriorRemain
FROM
dbo.tblInvRate as Rate
INNER JOIN
dbo.tblPart
ON Rate.fkPart = dbo.tblPart.pkCode
WHERE
Rate.[Date] < '850515'
GROUP BY
dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING
SUM(Rate.Qty * Rate.Type)>0
) as a
go
AMB
"Hamed" wrote:
> Hi *.*
> I've a problem with my query, perhaps you professionals can kindly
> Help me to overcome this, let me explain the problem, We have an
> Inventory software with about 8500 of Goods (we stored it in table
> Called tblPart), every input/output of these Goods saves in a Table
> Called tblInvRate, we have a field called Type, if it's input then
> Type=1, if that's output the Type=-1, Qty is the number of input or
> Output.
> Now with the following query we get the remain of Goods in the
> Inventory:
> SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
> Rate.Type) AS PriorRemain
> FROM dbo.tblInvRate Rate INNER JOIN
> dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> WHERE Rate.[Date] < '850515'
> GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> HAVING SUM(Rate.Qty * Rate.Type)>0
> This query takes about 1 sec to execute,
> In tblInvRate we have a field called IsRated and Rate, Rated and
> IsRated sets in a sp named prRatingInv which calculate the rate of the
> Goods and...
> We need to have the last rate of Goods in the remain query so we use
> This:
> SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
> SUM(Rate.Qty * Rate.Type) AS PriorRemain,
> (SELECT TOP 1 Rate
> FROM dbo.tblInvRate
> WHERE (fkPart = dbo.tblPart.pkCode
> AND [Date]<='850515' And IsRated = 1)
> ORDER BY [Date] DESC, [Time] DESC) AS
> Rate
> FROM dbo.tblInvRate Rate INNER JOIN
> dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> WHERE Rate.[Date] < '850515'
> GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> HAVING SUM(Rate.Qty * Rate.Type)>0
> Above query may take about 40 to 80 secs to execute!!! (Total number of
> rows in the tblInvRate is about 1.000.000 records)
> H've run the Index Wizard but it can't find any more index on the
> Tables, now would you please help me in this situation,
> here is the schema of these tables:
> CREATE TABLE [tblPart] (
> [pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> NULL ,
> [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> NULL ,
> [fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] DEFAULT
> (1),
> [Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
> [TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
> NULL ,
> [OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
> ,
> [fkPartAccGroup] [int] NULL ,
> CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
> (
> [pkCode]
> ) ON [PRIMARY]
> ) ON [PRIMARY]
> GO
>
> CREATE TABLE [tblInvRate] (
> [pkID] [int] NOT NULL ,
> [Source] [int] NOT NULL ,
> [Dest] [int] NOT NULL ,
> [fkInvHdr] [int] NULL ,
> [fkInvItm] [int] NULL ,
> [fkChangePart] [int] NULL ,
> [fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> NULL ,
> [Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> [Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> [Qty] [int] NOT NULL ,
> [Rate] [decimal](24, 4) NULL ,
> [Amount] [decimal](24, 0) NULL ,
> [OldRate] [decimal](24, 4) NULL ,
> [MainRate] [decimal](24, 4) NULL ,
> [Type] [int] NOT NULL ,
> [Code] [int] NOT NULL ,
> [IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
> DEFAULT (0),
> [fkCurrency] [int] NULL ,
> [CurrencyRate] [int] NULL ,
> [CurrencyAmount] [decimal](24, 0) NULL ,
> [fkShop] [int] NULL ,
> [fkPerson] [int] NULL ,
> [RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DEFAULT
> (0),
> [IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFAULT
> (0),
> [fkAccVchHdr] [int] NULL ,
> [AccTempNum] [int] NULL ,
> CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
> (
> [pkID],
> [Source]
> ) ON [PRIMARY] ,
> CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
> (
> [fkChangePart],
> [Source]
> ) REFERENCES [tblChangePart] (
> [pkID],
> [Source]
> ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
> CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
> (
> [fkInvHdr],
> [Source]
> ) REFERENCES [tblInvHdr] (
> [pkID],
> [Source]
> ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
> ) ON [PRIMARY]
> GO
>
> Thanks in advance,
> Waiting for your tips and advices,
> Hamed
>|||Thanks for your reply, it take 135 secs to execute,
Alejandro Mesa wrote:
> Hamed,
> Try using the correlated query after the remain of Goods in the Inventory
> has been calculated.
> select
> a.*,
> (
> SELECT TOP 1
> b.Rate
> FROM
> dbo.tblInvRate as b
> WHERE
> b.fkPart = a.pkCode
> AND b.[Date] <= '850515'
> And b.IsRated = 1
> ) as Rate
> from
> (
> SELECT
> dbo.tblPart.pkCode,
> dbo.tblPart.Title,
> SUM(Rate.Qty * Rate.Type) AS PriorRemain
> FROM
> dbo.tblInvRate as Rate
> INNER JOIN
> dbo.tblPart
> ON Rate.fkPart = dbo.tblPart.pkCode
> WHERE
> Rate.[Date] < '850515'
> GROUP BY
> dbo.tblPart.pkCode, dbo.tblPart.Title
> HAVING
> SUM(Rate.Qty * Rate.Type)>0
> ) as a
> go
>
> AMB
> "Hamed" wrote:
> > Hi *.*
> >
> > I've a problem with my query, perhaps you professionals can kindly
> > Help me to overcome this, let me explain the problem, We have an
> > Inventory software with about 8500 of Goods (we stored it in table
> > Called tblPart), every input/output of these Goods saves in a Table
> > Called tblInvRate, we have a field called Type, if it's input then
> > Type=1, if that's output the Type=-1, Qty is the number of input or
> > Output.
> >
> > Now with the following query we get the remain of Goods in the
> > Inventory:
> >
> > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
> > Rate.Type) AS PriorRemain
> > FROM dbo.tblInvRate Rate INNER JOIN
> > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > WHERE Rate.[Date] < '850515'
> > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > HAVING SUM(Rate.Qty * Rate.Type)>0
> >
> > This query takes about 1 sec to execute,
> >
> > In tblInvRate we have a field called IsRated and Rate, Rated and
> > IsRated sets in a sp named prRatingInv which calculate the rate of the
> > Goods and...
> >
> > We need to have the last rate of Goods in the remain query so we use
> > This:
> >
> > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
> > SUM(Rate.Qty * Rate.Type) AS PriorRemain,
> > (SELECT TOP 1 Rate
> > FROM dbo.tblInvRate
> > WHERE (fkPart = dbo.tblPart.pkCode
> > AND [Date]<='850515' And IsRated = 1)
> > ORDER BY [Date] DESC, [Time] DESC) AS
> > Rate
> > FROM dbo.tblInvRate Rate INNER JOIN
> > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > WHERE Rate.[Date] < '850515'
> > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > HAVING SUM(Rate.Qty * Rate.Type)>0
> >
> > Above query may take about 40 to 80 secs to execute!!! (Total number of
> > rows in the tblInvRate is about 1.000.000 records)
> >
> > H've run the Index Wizard but it can't find any more index on the
> > Tables, now would you please help me in this situation,
> >
> > here is the schema of these tables:
> >
> > CREATE TABLE [tblPart] (
> > [pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > NULL ,
> > [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > NULL ,
> > [fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] DEFAULT
> > (1),
> > [Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
> > [TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
> > NULL ,
> > [OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
> > ,
> > [fkPartAccGroup] [int] NULL ,
> > CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
> > (
> > [pkCode]
> > ) ON [PRIMARY]
> > ) ON [PRIMARY]
> > GO
> >
> >
> > CREATE TABLE [tblInvRate] (
> > [pkID] [int] NOT NULL ,
> > [Source] [int] NOT NULL ,
> > [Dest] [int] NOT NULL ,
> > [fkInvHdr] [int] NULL ,
> > [fkInvItm] [int] NULL ,
> > [fkChangePart] [int] NULL ,
> > [fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > NULL ,
> > [Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > [Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > [Qty] [int] NOT NULL ,
> > [Rate] [decimal](24, 4) NULL ,
> > [Amount] [decimal](24, 0) NULL ,
> > [OldRate] [decimal](24, 4) NULL ,
> > [MainRate] [decimal](24, 4) NULL ,
> > [Type] [int] NOT NULL ,
> > [Code] [int] NOT NULL ,
> > [IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
> > DEFAULT (0),
> > [fkCurrency] [int] NULL ,
> > [CurrencyRate] [int] NULL ,
> > [CurrencyAmount] [decimal](24, 0) NULL ,
> > [fkShop] [int] NULL ,
> > [fkPerson] [int] NULL ,
> > [RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DEFAULT
> > (0),
> > [IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFAULT
> > (0),
> > [fkAccVchHdr] [int] NULL ,
> > [AccTempNum] [int] NULL ,
> > CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
> > (
> > [pkID],
> > [Source]
> > ) ON [PRIMARY] ,
> > CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
> > (
> > [fkChangePart],
> > [Source]
> > ) REFERENCES [tblChangePart] (
> > [pkID],
> > [Source]
> > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
> > CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
> > (
> > [fkInvHdr],
> > [Source]
> > ) REFERENCES [tblInvHdr] (
> > [pkID],
> > [Source]
> > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
> > ) ON [PRIMARY]
> > GO
> >
> >
> > Thanks in advance,
> > Waiting for your tips and advices,
> > Hamed
> >
> >|||Hamed,
can you post the execution plan?
AMB
"Hamed" wrote:
> Thanks for your reply, it take 135 secs to execute,
> Alejandro Mesa wrote:
> > Hamed,
> >
> > Try using the correlated query after the remain of Goods in the Inventory
> > has been calculated.
> >
> > select
> > a.*,
> > (
> > SELECT TOP 1
> > b.Rate
> > FROM
> > dbo.tblInvRate as b
> > WHERE
> > b.fkPart = a.pkCode
> > AND b.[Date] <= '850515'
> > And b.IsRated = 1
> > ) as Rate
> > from
> > (
> > SELECT
> > dbo.tblPart.pkCode,
> > dbo.tblPart.Title,
> > SUM(Rate.Qty * Rate.Type) AS PriorRemain
> > FROM
> > dbo.tblInvRate as Rate
> > INNER JOIN
> > dbo.tblPart
> > ON Rate.fkPart = dbo.tblPart.pkCode
> > WHERE
> > Rate.[Date] < '850515'
> > GROUP BY
> > dbo.tblPart.pkCode, dbo.tblPart.Title
> > HAVING
> > SUM(Rate.Qty * Rate.Type)>0
> > ) as a
> > go
> >
> >
> > AMB
> >
> > "Hamed" wrote:
> >
> > > Hi *.*
> > >
> > > I've a problem with my query, perhaps you professionals can kindly
> > > Help me to overcome this, let me explain the problem, We have an
> > > Inventory software with about 8500 of Goods (we stored it in table
> > > Called tblPart), every input/output of these Goods saves in a Table
> > > Called tblInvRate, we have a field called Type, if it's input then
> > > Type=1, if that's output the Type=-1, Qty is the number of input or
> > > Output.
> > >
> > > Now with the following query we get the remain of Goods in the
> > > Inventory:
> > >
> > > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
> > > Rate.Type) AS PriorRemain
> > > FROM dbo.tblInvRate Rate INNER JOIN
> > > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > > WHERE Rate.[Date] < '850515'
> > > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > > HAVING SUM(Rate.Qty * Rate.Type)>0
> > >
> > > This query takes about 1 sec to execute,
> > >
> > > In tblInvRate we have a field called IsRated and Rate, Rated and
> > > IsRated sets in a sp named prRatingInv which calculate the rate of the
> > > Goods and...
> > >
> > > We need to have the last rate of Goods in the remain query so we use
> > > This:
> > >
> > > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
> > > SUM(Rate.Qty * Rate.Type) AS PriorRemain,
> > > (SELECT TOP 1 Rate
> > > FROM dbo.tblInvRate
> > > WHERE (fkPart = dbo.tblPart.pkCode
> > > AND [Date]<='850515' And IsRated = 1)
> > > ORDER BY [Date] DESC, [Time] DESC) AS
> > > Rate
> > > FROM dbo.tblInvRate Rate INNER JOIN
> > > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > > WHERE Rate.[Date] < '850515'
> > > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > > HAVING SUM(Rate.Qty * Rate.Type)>0
> > >
> > > Above query may take about 40 to 80 secs to execute!!! (Total number of
> > > rows in the tblInvRate is about 1.000.000 records)
> > >
> > > H've run the Index Wizard but it can't find any more index on the
> > > Tables, now would you please help me in this situation,
> > >
> > > here is the schema of these tables:
> > >
> > > CREATE TABLE [tblPart] (
> > > [pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > NULL ,
> > > [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > NULL ,
> > > [fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] DEFAULT
> > > (1),
> > > [Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
> > > [TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
> > > NULL ,
> > > [OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
> > > ,
> > > [fkPartAccGroup] [int] NULL ,
> > > CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
> > > (
> > > [pkCode]
> > > ) ON [PRIMARY]
> > > ) ON [PRIMARY]
> > > GO
> > >
> > >
> > > CREATE TABLE [tblInvRate] (
> > > [pkID] [int] NOT NULL ,
> > > [Source] [int] NOT NULL ,
> > > [Dest] [int] NOT NULL ,
> > > [fkInvHdr] [int] NULL ,
> > > [fkInvItm] [int] NULL ,
> > > [fkChangePart] [int] NULL ,
> > > [fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > NULL ,
> > > [Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > > [Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > > [Qty] [int] NOT NULL ,
> > > [Rate] [decimal](24, 4) NULL ,
> > > [Amount] [decimal](24, 0) NULL ,
> > > [OldRate] [decimal](24, 4) NULL ,
> > > [MainRate] [decimal](24, 4) NULL ,
> > > [Type] [int] NOT NULL ,
> > > [Code] [int] NOT NULL ,
> > > [IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
> > > DEFAULT (0),
> > > [fkCurrency] [int] NULL ,
> > > [CurrencyRate] [int] NULL ,
> > > [CurrencyAmount] [decimal](24, 0) NULL ,
> > > [fkShop] [int] NULL ,
> > > [fkPerson] [int] NULL ,
> > > [RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DEFAULT
> > > (0),
> > > [IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFAULT
> > > (0),
> > > [fkAccVchHdr] [int] NULL ,
> > > [AccTempNum] [int] NULL ,
> > > CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
> > > (
> > > [pkID],
> > > [Source]
> > > ) ON [PRIMARY] ,
> > > CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
> > > (
> > > [fkChangePart],
> > > [Source]
> > > ) REFERENCES [tblChangePart] (
> > > [pkID],
> > > [Source]
> > > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
> > > CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
> > > (
> > > [fkInvHdr],
> > > [Source]
> > > ) REFERENCES [tblInvHdr] (
> > > [pkID],
> > > [Source]
> > > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
> > > ) ON [PRIMARY]
> > > GO
> > >
> > >
> > > Thanks in advance,
> > > Waiting for your tips and advices,
> > > Hamed
> > >
> > >
>|||I don't know how to post the execution plan to this group,
Alejandro Mesa wrote:
> Hamed,
> can you post the execution plan?
>
> AMB
> "Hamed" wrote:
> > Thanks for your reply, it take 135 secs to execute,
> >
> > Alejandro Mesa wrote:
> > > Hamed,
> > >
> > > Try using the correlated query after the remain of Goods in the Inventory
> > > has been calculated.
> > >
> > > select
> > > a.*,
> > > (
> > > SELECT TOP 1
> > > b.Rate
> > > FROM
> > > dbo.tblInvRate as b
> > > WHERE
> > > b.fkPart = a.pkCode
> > > AND b.[Date] <= '850515'
> > > And b.IsRated = 1
> > > ) as Rate
> > > from
> > > (
> > > SELECT
> > > dbo.tblPart.pkCode,
> > > dbo.tblPart.Title,
> > > SUM(Rate.Qty * Rate.Type) AS PriorRemain
> > > FROM
> > > dbo.tblInvRate as Rate
> > > INNER JOIN
> > > dbo.tblPart
> > > ON Rate.fkPart = dbo.tblPart.pkCode
> > > WHERE
> > > Rate.[Date] < '850515'
> > > GROUP BY
> > > dbo.tblPart.pkCode, dbo.tblPart.Title
> > > HAVING
> > > SUM(Rate.Qty * Rate.Type)>0
> > > ) as a
> > > go
> > >
> > >
> > > AMB
> > >
> > > "Hamed" wrote:
> > >
> > > > Hi *.*
> > > >
> > > > I've a problem with my query, perhaps you professionals can kindly
> > > > Help me to overcome this, let me explain the problem, We have an
> > > > Inventory software with about 8500 of Goods (we stored it in table
> > > > Called tblPart), every input/output of these Goods saves in a Table
> > > > Called tblInvRate, we have a field called Type, if it's input then
> > > > Type=1, if that's output the Type=-1, Qty is the number of input or
> > > > Output.
> > > >
> > > > Now with the following query we get the remain of Goods in the
> > > > Inventory:
> > > >
> > > > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
> > > > Rate.Type) AS PriorRemain
> > > > FROM dbo.tblInvRate Rate INNER JOIN
> > > > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > > > WHERE Rate.[Date] < '850515'
> > > > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > > > HAVING SUM(Rate.Qty * Rate.Type)>0
> > > >
> > > > This query takes about 1 sec to execute,
> > > >
> > > > In tblInvRate we have a field called IsRated and Rate, Rated and
> > > > IsRated sets in a sp named prRatingInv which calculate the rate of the
> > > > Goods and...
> > > >
> > > > We need to have the last rate of Goods in the remain query so we use
> > > > This:
> > > >
> > > > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
> > > > SUM(Rate.Qty * Rate.Type) AS PriorRemain,
> > > > (SELECT TOP 1 Rate
> > > > FROM dbo.tblInvRate
> > > > WHERE (fkPart = dbo.tblPart.pkCode
> > > > AND [Date]<='850515' And IsRated = 1)
> > > > ORDER BY [Date] DESC, [Time] DESC) AS
> > > > Rate
> > > > FROM dbo.tblInvRate Rate INNER JOIN
> > > > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > > > WHERE Rate.[Date] < '850515'
> > > > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > > > HAVING SUM(Rate.Qty * Rate.Type)>0
> > > >
> > > > Above query may take about 40 to 80 secs to execute!!! (Total number of
> > > > rows in the tblInvRate is about 1.000.000 records)
> > > >
> > > > H've run the Index Wizard but it can't find any more index on the
> > > > Tables, now would you please help me in this situation,
> > > >
> > > > here is the schema of these tables:
> > > >
> > > > CREATE TABLE [tblPart] (
> > > > [pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > > NULL ,
> > > > [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > > NULL ,
> > > > [fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] DEFAULT
> > > > (1),
> > > > [Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
> > > > [TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
> > > > NULL ,
> > > > [OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
> > > > ,
> > > > [fkPartAccGroup] [int] NULL ,
> > > > CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
> > > > (
> > > > [pkCode]
> > > > ) ON [PRIMARY]
> > > > ) ON [PRIMARY]
> > > > GO
> > > >
> > > >
> > > > CREATE TABLE [tblInvRate] (
> > > > [pkID] [int] NOT NULL ,
> > > > [Source] [int] NOT NULL ,
> > > > [Dest] [int] NOT NULL ,
> > > > [fkInvHdr] [int] NULL ,
> > > > [fkInvItm] [int] NULL ,
> > > > [fkChangePart] [int] NULL ,
> > > > [fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > > NULL ,
> > > > [Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > > > [Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > > > [Qty] [int] NOT NULL ,
> > > > [Rate] [decimal](24, 4) NULL ,
> > > > [Amount] [decimal](24, 0) NULL ,
> > > > [OldRate] [decimal](24, 4) NULL ,
> > > > [MainRate] [decimal](24, 4) NULL ,
> > > > [Type] [int] NOT NULL ,
> > > > [Code] [int] NOT NULL ,
> > > > [IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
> > > > DEFAULT (0),
> > > > [fkCurrency] [int] NULL ,
> > > > [CurrencyRate] [int] NULL ,
> > > > [CurrencyAmount] [decimal](24, 0) NULL ,
> > > > [fkShop] [int] NULL ,
> > > > [fkPerson] [int] NULL ,
> > > > [RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DEFAULT
> > > > (0),
> > > > [IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFAULT
> > > > (0),
> > > > [fkAccVchHdr] [int] NULL ,
> > > > [AccTempNum] [int] NULL ,
> > > > CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
> > > > (
> > > > [pkID],
> > > > [Source]
> > > > ) ON [PRIMARY] ,
> > > > CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
> > > > (
> > > > [fkChangePart],
> > > > [Source]
> > > > ) REFERENCES [tblChangePart] (
> > > > [pkID],
> > > > [Source]
> > > > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
> > > > CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
> > > > (
> > > > [fkInvHdr],
> > > > [Source]
> > > > ) REFERENCES [tblInvHdr] (
> > > > [pkID],
> > > > [Source]
> > > > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
> > > > ) ON [PRIMARY]
> > > > GO
> > > >
> > > >
> > > > Thanks in advance,
> > > > Waiting for your tips and advices,
> > > > Hamed
> > > >
> > > >
> >
> >|||Hamed,
Use "set showplan_text on" to get the execution plan as text.
set showplan_text on
go
select ...
go
set showplan_text off
go
AMB
"Hamed" wrote:
> I don't know how to post the execution plan to this group,
> Alejandro Mesa wrote:
> > Hamed,
> >
> > can you post the execution plan?
> >
> >
> > AMB
> >
> > "Hamed" wrote:
> >
> > > Thanks for your reply, it take 135 secs to execute,
> > >
> > > Alejandro Mesa wrote:
> > > > Hamed,
> > > >
> > > > Try using the correlated query after the remain of Goods in the Inventory
> > > > has been calculated.
> > > >
> > > > select
> > > > a.*,
> > > > (
> > > > SELECT TOP 1
> > > > b.Rate
> > > > FROM
> > > > dbo.tblInvRate as b
> > > > WHERE
> > > > b.fkPart = a.pkCode
> > > > AND b.[Date] <= '850515'
> > > > And b.IsRated = 1
> > > > ) as Rate
> > > > from
> > > > (
> > > > SELECT
> > > > dbo.tblPart.pkCode,
> > > > dbo.tblPart.Title,
> > > > SUM(Rate.Qty * Rate.Type) AS PriorRemain
> > > > FROM
> > > > dbo.tblInvRate as Rate
> > > > INNER JOIN
> > > > dbo.tblPart
> > > > ON Rate.fkPart = dbo.tblPart.pkCode
> > > > WHERE
> > > > Rate.[Date] < '850515'
> > > > GROUP BY
> > > > dbo.tblPart.pkCode, dbo.tblPart.Title
> > > > HAVING
> > > > SUM(Rate.Qty * Rate.Type)>0
> > > > ) as a
> > > > go
> > > >
> > > >
> > > > AMB
> > > >
> > > > "Hamed" wrote:
> > > >
> > > > > Hi *.*
> > > > >
> > > > > I've a problem with my query, perhaps you professionals can kindly
> > > > > Help me to overcome this, let me explain the problem, We have an
> > > > > Inventory software with about 8500 of Goods (we stored it in table
> > > > > Called tblPart), every input/output of these Goods saves in a Table
> > > > > Called tblInvRate, we have a field called Type, if it's input then
> > > > > Type=1, if that's output the Type=-1, Qty is the number of input or
> > > > > Output.
> > > > >
> > > > > Now with the following query we get the remain of Goods in the
> > > > > Inventory:
> > > > >
> > > > > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
> > > > > Rate.Type) AS PriorRemain
> > > > > FROM dbo.tblInvRate Rate INNER JOIN
> > > > > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > > > > WHERE Rate.[Date] < '850515'
> > > > > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > > > > HAVING SUM(Rate.Qty * Rate.Type)>0
> > > > >
> > > > > This query takes about 1 sec to execute,
> > > > >
> > > > > In tblInvRate we have a field called IsRated and Rate, Rated and
> > > > > IsRated sets in a sp named prRatingInv which calculate the rate of the
> > > > > Goods and...
> > > > >
> > > > > We need to have the last rate of Goods in the remain query so we use
> > > > > This:
> > > > >
> > > > > SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
> > > > > SUM(Rate.Qty * Rate.Type) AS PriorRemain,
> > > > > (SELECT TOP 1 Rate
> > > > > FROM dbo.tblInvRate
> > > > > WHERE (fkPart = dbo.tblPart.pkCode
> > > > > AND [Date]<='850515' And IsRated = 1)
> > > > > ORDER BY [Date] DESC, [Time] DESC) AS
> > > > > Rate
> > > > > FROM dbo.tblInvRate Rate INNER JOIN
> > > > > dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> > > > > WHERE Rate.[Date] < '850515'
> > > > > GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> > > > > HAVING SUM(Rate.Qty * Rate.Type)>0
> > > > >
> > > > > Above query may take about 40 to 80 secs to execute!!! (Total number of
> > > > > rows in the tblInvRate is about 1.000.000 records)
> > > > >
> > > > > H've run the Index Wizard but it can't find any more index on the
> > > > > Tables, now would you please help me in this situation,
> > > > >
> > > > > here is the schema of these tables:
> > > > >
> > > > > CREATE TABLE [tblPart] (
> > > > > [pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > > > NULL ,
> > > > > [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > > > NULL ,
> > > > > [fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] DEFAULT
> > > > > (1),
> > > > > [Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL ,
> > > > > [TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
> > > > > NULL ,
> > > > > [OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
> > > > > ,
> > > > > [fkPartAccGroup] [int] NULL ,
> > > > > CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
> > > > > (
> > > > > [pkCode]
> > > > > ) ON [PRIMARY]
> > > > > ) ON [PRIMARY]
> > > > > GO
> > > > >
> > > > >
> > > > > CREATE TABLE [tblInvRate] (
> > > > > [pkID] [int] NOT NULL ,
> > > > > [Source] [int] NOT NULL ,
> > > > > [Dest] [int] NOT NULL ,
> > > > > [fkInvHdr] [int] NULL ,
> > > > > [fkInvItm] [int] NULL ,
> > > > > [fkChangePart] [int] NULL ,
> > > > > [fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
> > > > > NULL ,
> > > > > [Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > > > > [Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
> > > > > [Qty] [int] NOT NULL ,
> > > > > [Rate] [decimal](24, 4) NULL ,
> > > > > [Amount] [decimal](24, 0) NULL ,
> > > > > [OldRate] [decimal](24, 4) NULL ,
> > > > > [MainRate] [decimal](24, 4) NULL ,
> > > > > [Type] [int] NOT NULL ,
> > > > > [Code] [int] NOT NULL ,
> > > > > [IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
> > > > > DEFAULT (0),
> > > > > [fkCurrency] [int] NULL ,
> > > > > [CurrencyRate] [int] NULL ,
> > > > > [CurrencyAmount] [decimal](24, 0) NULL ,
> > > > > [fkShop] [int] NULL ,
> > > > > [fkPerson] [int] NULL ,
> > > > > [RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DEFAULT
> > > > > (0),
> > > > > [IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFAULT
> > > > > (0),
> > > > > [fkAccVchHdr] [int] NULL ,
> > > > > [AccTempNum] [int] NULL ,
> > > > > CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
> > > > > (
> > > > > [pkID],
> > > > > [Source]
> > > > > ) ON [PRIMARY] ,
> > > > > CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
> > > > > (
> > > > > [fkChangePart],
> > > > > [Source]
> > > > > ) REFERENCES [tblChangePart] (
> > > > > [pkID],
> > > > > [Source]
> > > > > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
> > > > > CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
> > > > > (
> > > > > [fkInvHdr],
> > > > > [Source]
> > > > > ) REFERENCES [tblInvHdr] (
> > > > > [pkID],
> > > > > [Source]
> > > > > ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
> > > > > ) ON [PRIMARY]
> > > > > GO
> > > > >
> > > > >
> > > > > Thanks in advance,
> > > > > Waiting for your tips and advices,
> > > > > Hamed
> > > > >
> > > > >
> > >
> > >
>|||This is yours:
|--Compute Scalar(DEFINE:([b].[Rate]=[b].[Rate]))
|--Nested Loops(Left Outer Join, OUTER
REFERENCES:([tblPart].[pkCode]))
|--Filter(WHERE:([Expr1003]>0))
| |--Merge Join(Inner Join,
MERGE:([Rate].[fkPart])=([tblPart].[pkCode]),
RESIDUAL:([tblPart].[pkCode]=[Rate].[fkPart]))
| |--Stream Aggregate(GROUP BY:([Rate].[fkPart])
DEFINE:([Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
| | |--Index
Scan(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [Rate]),
WHERE:([Rate].[Date]<'850515') ORDERED FORWARD)
| |--Index
Scan(OBJECT:([PosKish].[dbo].[tblPart].[tblPart16]), ORDERED FORWARD)
|--Top(1)
|--Filter(WHERE:(Convert([b].[IsRated])=1))
|--Bookmark Lookup(BOOKMARK:([Bmk1004]),
OBJECT:([PosKish].[dbo].[tblInvRate] AS [b]) WITH PREFETCH)
|--Index
Seek(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [b]),
SEEK:([b].[fkPart]=[tblPart].[pkCode] AND [b].[Date] <= '850515')
ORDERED FORWARD)
and this is mine:
|--Compute Scalar(DEFINE:([tblInvRate].[Rate]=[tblInvRate].[Rate]))
|--Nested Loops(Left Outer Join, OUTER
REFERENCES:([tblPart].[pkCode]))
|--Filter(WHERE:([Expr1003]>0))
| |--Merge Join(Inner Join,
MERGE:([Rate].[fkPart])=([tblPart].[pkCode]),
RESIDUAL:([tblPart].[pkCode]=[Rate].[fkPart]))
| |--Stream Aggregate(GROUP BY:([Rate].[fkPart])
DEFINE:([Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
| | |--Index
Scan(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [Rate]),
WHERE:([Rate].[Date]<'850515') ORDERED FORWARD)
| |--Index
Scan(OBJECT:([PosKish].[dbo].[tblPart].[tblPart16]), ORDERED FORWARD)
|--Compute
Scalar(DEFINE:([tblInvRate].[Rate]=[tblInvRate].[Rate]))
|--Sort(TOP 1, ORDER BY:([tblInvRate].[Date] DESC,
[tblInvRate].[Time] DESC))
|--Filter(WHERE:(Convert([tblInvRate].[IsRated])=1))
|--Bookmark Lookup(BOOKMARK:([Bmk1004]),
OBJECT:([PosKish].[dbo].[tblInvRate]) WITH PREFETCH)
|--Index
Seek(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11]),
SEEK:([tblInvRate].[fkPart]=[tblPart].[pkCode] AND [tblInvRate].[Date]
<= '850515') ORDERED FORWARD)|||Hamed,
Please, can you post indexes for each table also?
Seems that column [dbo].[tblInvRate].[IsRated] is not integer, correct?.
Ttry casting the value you compare with. Let us assume it is bit data type,
then:
|--Filter(WHERE:(Convert([b].[IsRated])=1))
(
SELECT TOP 1
Rate
FROM
dbo.tblInvRate
WHERE
fkPart = dbo.tblPart.pkCode
AND [Date]<='850515'
And IsRated = cast(1 as bit) <-- change this
)
AMB
"Hamed" wrote:
> This is yours:
> |--Compute Scalar(DEFINE:([b].[Rate]=[b].[Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES:([tblPart].[pkCode]))
> |--Filter(WHERE:([Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE:([Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL:([tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY:([Rate].[fkPart])
> DEFINE:([Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [Rate]),
> WHERE:([Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblPart].[tblPart16]), ORDERED FORWARD)
> |--Top(1)
> |--Filter(WHERE:(Convert([b].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK:([Bmk1004]),
> OBJECT:([PosKish].[dbo].[tblInvRate] AS [b]) WITH PREFETCH)
> |--Index
> Seek(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [b]),
> SEEK:([b].[fkPart]=[tblPart].[pkCode] AND [b].[Date] <= '850515')
> ORDERED FORWARD)
> and this is mine:
> |--Compute Scalar(DEFINE:([tblInvRate].[Rate]=[tblInvRate].[Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES:([tblPart].[pkCode]))
> |--Filter(WHERE:([Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE:([Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL:([tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY:([Rate].[fkPart])
> DEFINE:([Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [Rate]),
> WHERE:([Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblPart].[tblPart16]), ORDERED FORWARD)
> |--Compute
> Scalar(DEFINE:([tblInvRate].[Rate]=[tblInvRate].[Rate]))
> |--Sort(TOP 1, ORDER BY:([tblInvRate].[Date] DESC,
> [tblInvRate].[Time] DESC))
> |--Filter(WHERE:(Convert([tblInvRate].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK:([Bmk1004]),
> OBJECT:([PosKish].[dbo].[tblInvRate]) WITH PREFETCH)
> |--Index
> Seek(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11]),
> SEEK:([tblInvRate].[fkPart]=[tblPart].[pkCode] AND [tblInvRate].[Date]
> <= '850515') ORDERED FORWARD)
>|||Hamed,
In my first post I forgot the "order by" clause in the correlated subquery.
I think that is the reason why we are getting diffrent execution plans.
select
a.*,
(
SELECT TOP 1
b.Rate
FROM
dbo.tblInvRate as b
WHERE
b.fkPart = a.pkCode
AND b.[Date] <= '850515'
And b.IsRated = 1
ORDER BY [Date] DESC, [Time] DESC <-- forgot this
) as Rate
from
(
SELECT
dbo.tblPart.pkCode,
dbo.tblPart.Title,
SUM(Rate.Qty * Rate.Type) AS PriorRemain
FROM
dbo.tblInvRate as Rate
INNER JOIN
dbo.tblPart
ON Rate.fkPart = dbo.tblPart.pkCode
WHERE
Rate.[Date] < '850515'
GROUP BY
dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING
SUM(Rate.Qty * Rate.Type)>0
) as a
go
AMB
"Hamed" wrote:
> This is yours:
> |--Compute Scalar(DEFINE:([b].[Rate]=[b].[Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES:([tblPart].[pkCode]))
> |--Filter(WHERE:([Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE:([Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL:([tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY:([Rate].[fkPart])
> DEFINE:([Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [Rate]),
> WHERE:([Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblPart].[tblPart16]), ORDERED FORWARD)
> |--Top(1)
> |--Filter(WHERE:(Convert([b].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK:([Bmk1004]),
> OBJECT:([PosKish].[dbo].[tblInvRate] AS [b]) WITH PREFETCH)
> |--Index
> Seek(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [b]),
> SEEK:([b].[fkPart]=[tblPart].[pkCode] AND [b].[Date] <= '850515')
> ORDERED FORWARD)
> and this is mine:
> |--Compute Scalar(DEFINE:([tblInvRate].[Rate]=[tblInvRate].[Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES:([tblPart].[pkCode]))
> |--Filter(WHERE:([Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE:([Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL:([tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY:([Rate].[fkPart])
> DEFINE:([Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11] AS [Rate]),
> WHERE:([Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT:([PosKish].[dbo].[tblPart].[tblPart16]), ORDERED FORWARD)
> |--Compute
> Scalar(DEFINE:([tblInvRate].[Rate]=[tblInvRate].[Rate]))
> |--Sort(TOP 1, ORDER BY:([tblInvRate].[Date] DESC,
> [tblInvRate].[Time] DESC))
> |--Filter(WHERE:(Convert([tblInvRate].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK:([Bmk1004]),
> OBJECT:([PosKish].[dbo].[tblInvRate]) WITH PREFETCH)
> |--Index
> Seek(OBJECT:([PosKish].[dbo].[tblInvRate].[tblInvRate11]),
> SEEK:([tblInvRate].[fkPart]=[tblPart].[pkCode] AND [tblInvRate].[Date]
> <= '850515') ORDERED FORWARD)
>

How to improve the performance,

Hi *.*
I've a problem with my query, perhaps you professionals can kindly
Help me to overcome this, let me explain the problem, We have an
Inventory software with about 8500 of Goods (we stored it in table
Called tblPart), every input/output of these Goods saves in a Table
Called tblInvRate, we have a field called Type, if it's input then
Type=1, if that's output the Type=-1, Qty is the number of input or
Output.
Now with the following query we get the remain of Goods in the
Inventory:
SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
Rate.Type) AS PriorRemain
FROM dbo.tblInvRate Rate INNER JOIN
dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
WHERE Rate.[Date] < '850515'
GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING SUM(Rate.Qty * Rate.Type)>0
This query takes about 1 sec to execute,
In tblInvRate we have a field called IsRated and Rate, Rated and
IsRated sets in a sp named prRatingInv which calculate the rate of the
Goods and...
We need to have the last rate of Goods in the remain query so we use
This:
SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
SUM(Rate.Qty * Rate.Type) AS PriorRemain,
(SELECT TOP 1 Rate
FROM dbo.tblInvRate
WHERE (fkPart = dbo.tblPart.pkCode
AND [Date]<='850515' And IsRated = 1)
ORDER BY [Date] DESC, [Time] DESC) AS
Rate
FROM dbo.tblInvRate Rate INNER JOIN
dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
WHERE Rate.[Date] < '850515'
GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING SUM(Rate.Qty * Rate.Type)>0
Above query may take about 40 to 80 secs to execute!!! (Total number of
rows in the tblInvRate is about 1.000.000 records)
H've run the Index Wizard but it can't find any more index on the
Tables, now would you please help me in this situation,
here is the schema of these tables:
CREATE TABLE [tblPart] (
[pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
NULL ,
[Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
NULL ,
[fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit] D
EFAULT
(1),
[Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS NULL
,
[TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS
NULL ,
[OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS NUL
L
,
[fkPartAccGroup] [int] NULL ,
CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
(
[pkCode]
) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [tblInvRate] (
[pkID] [int] NOT NULL ,
[Source] [int] NOT NULL ,
[Dest] [int] NOT NULL ,
[fkInvHdr] [int] NULL ,
[fkInvItm] [int] NULL ,
[fkChangePart] [int] NULL ,
[fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT
NULL ,
[Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
[Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NULL ,
[Qty] [int] NOT NULL ,
[Rate] [decimal](24, 4) NULL ,
[Amount] [decimal](24, 0) NULL ,
[OldRate] [decimal](24, 4) NULL ,
[MainRate] [decimal](24, 4) NULL ,
[Type] [int] NOT NULL ,
[Code] [int] NOT NULL ,
[IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomatic]
DEFAULT (0),
[fkCurrency] [int] NULL ,
[CurrencyRate] [int] NULL ,
[CurrencyAmount] [decimal](24, 0) NULL ,
[fkShop] [int] NULL ,
[fkPerson] [int] NULL ,
[RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind] DE
FAULT
(0),
[IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] DEFA
ULT
(0),
[fkAccVchHdr] [int] NULL ,
[AccTempNum] [int] NULL ,
CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
(
[pkID],
[Source]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
(
[fkChangePart],
[Source]
) REFERENCES [tblChangePart] (
[pkID],
[Source]
) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
(
[fkInvHdr],
[Source]
) REFERENCES [tblInvHdr] (
[pkID],
[Source]
) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
) ON [PRIMARY]
GO
Thanks in advance,
Waiting for your tips and advices,
HamedHamed,
Try using the correlated query after the remain of Goods in the Inventory
has been calculated.
select
a.*,
(
SELECT TOP 1
b.Rate
FROM
dbo.tblInvRate as b
WHERE
b.fkPart = a.pkCode
AND b.[Date] <= '850515'
And b.IsRated = 1
) as Rate
from
(
SELECT
dbo.tblPart.pkCode,
dbo.tblPart.Title,
SUM(Rate.Qty * Rate.Type) AS PriorRemain
FROM
dbo.tblInvRate as Rate
INNER JOIN
dbo.tblPart
ON Rate.fkPart = dbo.tblPart.pkCode
WHERE
Rate.[Date] < '850515'
GROUP BY
dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING
SUM(Rate.Qty * Rate.Type)>0
) as a
go
AMB
"Hamed" wrote:

> Hi *.*
> I've a problem with my query, perhaps you professionals can kindly
> Help me to overcome this, let me explain the problem, We have an
> Inventory software with about 8500 of Goods (we stored it in table
> Called tblPart), every input/output of these Goods saves in a Table
> Called tblInvRate, we have a field called Type, if it's input then
> Type=1, if that's output the Type=-1, Qty is the number of input or
> Output.
> Now with the following query we get the remain of Goods in the
> Inventory:
> SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,SUM(Rate.Qty *
> Rate.Type) AS PriorRemain
> FROM dbo.tblInvRate Rate INNER JOIN
> dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> WHERE Rate.[Date] < '850515'
> GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> HAVING SUM(Rate.Qty * Rate.Type)>0
> This query takes about 1 sec to execute,
> In tblInvRate we have a field called IsRated and Rate, Rated and
> IsRated sets in a sp named prRatingInv which calculate the rate of the
> Goods and...
> We need to have the last rate of Goods in the remain query so we use
> This:
> SELECT dbo.tblPart.pkCode, dbo.tblPart.Title,
> SUM(Rate.Qty * Rate.Type) AS PriorRemain,
> (SELECT TOP 1 Rate
> FROM dbo.tblInvRate
> WHERE (fkPart = dbo.tblPart.pkCode
> AND [Date]<='850515' And IsRated = 1)
> ORDER BY [Date] DESC, [Time] DESC) A
S
> Rate
> FROM dbo.tblInvRate Rate INNER JOIN
> dbo.tblPart ON Rate.fkPart = dbo.tblPart.pkCode
> WHERE Rate.[Date] < '850515'
> GROUP BY dbo.tblPart.pkCode, dbo.tblPart.Title
> HAVING SUM(Rate.Qty * Rate.Type)>0
> Above query may take about 40 to 80 secs to execute!!! (Total number of
> rows in the tblInvRate is about 1.000.000 records)
> H've run the Index Wizard but it can't find any more index on the
> Tables, now would you please help me in this situation,
> here is the schema of these tables:
> CREATE TABLE [tblPart] (
> [pkCode] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS N
OT
> NULL ,
> [Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS N
OT
> NULL ,
> [fkPartUnit] [int] NOT NULL CONSTRAINT [DF_tblPart_fkPartUnit
] DEFAULT
> (1),
> [Descr] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI_AS N
ULL ,
> [TitleLatin] [varchar] (255) COLLATE SQL_Latin1_General_CP1256_CI
_AS
> NULL ,
> [OldCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1256_CI_AS
NULL
> ,
> [fkPartAccGroup] [int] NULL ,
> CONSTRAINT [PK_tblPart] PRIMARY KEY CLUSTERED
> (
> [pkCode]
> ) ON [PRIMARY]
> ) ON [PRIMARY]
> GO
>
> CREATE TABLE [tblInvRate] (
> [pkID] [int] NOT NULL ,
> [Source] [int] NOT NULL ,
> [Dest] [int] NOT NULL ,
> [fkInvHdr] [int] NULL ,
> [fkInvItm] [int] NULL ,
> [fkChangePart] [int] NULL ,
> [fkPart] [varchar] (20) COLLATE SQL_Latin1_General_CP1256_CI_AS N
OT
> NULL ,
> [Date] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NUL
L ,
> [Time] [char] (6) COLLATE SQL_Latin1_General_CP1256_CI_AS NOT NUL
L ,
> [Qty] [int] NOT NULL ,
> [Rate] [decimal](24, 4) NULL ,
> [Amount] [decimal](24, 0) NULL ,
> [OldRate] [decimal](24, 4) NULL ,
> [MainRate] [decimal](24, 4) NULL ,
> [Type] [int] NOT NULL ,
> [Code] [int] NOT NULL ,
> [IsAutomatic] [bit] NULL CONSTRAINT [DF_tblInvRate_IsAutomati
c]
> DEFAULT (0),
> [fkCurrency] [int] NULL ,
> [CurrencyRate] [int] NULL ,
> [CurrencyAmount] [decimal](24, 0) NULL ,
> [fkShop] [int] NULL ,
> [fkPerson] [int] NULL ,
> [RateKind] [int] NOT NULL CONSTRAINT [DF_tblInvRate_RateKind]
DEFAULT
> (0),
> [IsRated] [bit] NOT NULL CONSTRAINT [DF_tblInvRate_IsRated] D
EFAULT
> (0),
> [fkAccVchHdr] [int] NULL ,
> [AccTempNum] [int] NULL ,
> CONSTRAINT [PK_tblInvRate] PRIMARY KEY CLUSTERED
> (
> [pkID],
> [Source]
> ) ON [PRIMARY] ,
> CONSTRAINT [FK_tblInvRate_tblChangePart] FOREIGN KEY
> (
> [fkChangePart],
> [Source]
> ) REFERENCES [tblChangePart] (
> [pkID],
> [Source]
> ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION ,
> CONSTRAINT [FK_tblInvRate_tblInvHdr] FOREIGN KEY
> (
> [fkInvHdr],
> [Source]
> ) REFERENCES [tblInvHdr] (
> [pkID],
> [Source]
> ) ON DELETE CASCADE ON UPDATE CASCADE NOT FOR REPLICATION
> ) ON [PRIMARY]
> GO
>
> Thanks in advance,
> Waiting for your tips and advices,
> Hamed
>|||Thanks for your reply, it take 135 secs to execute,
Alejandro Mesa wrote:[vbcol=seagreen]
> Hamed,
> Try using the correlated query after the remain of Goods in the Inventory
> has been calculated.
> select
> a.*,
> (
> SELECT TOP 1
> b.Rate
> FROM
> dbo.tblInvRate as b
> WHERE
> b.fkPart = a.pkCode
> AND b.[Date] <= '850515'
> And b.IsRated = 1
> ) as Rate
> from
> (
> SELECT
> dbo.tblPart.pkCode,
> dbo.tblPart.Title,
> SUM(Rate.Qty * Rate.Type) AS PriorRemain
> FROM
> dbo.tblInvRate as Rate
> INNER JOIN
> dbo.tblPart
> ON Rate.fkPart = dbo.tblPart.pkCode
> WHERE
> Rate.[Date] < '850515'
> GROUP BY
> dbo.tblPart.pkCode, dbo.tblPart.Title
> HAVING
> SUM(Rate.Qty * Rate.Type)>0
> ) as a
> go
>
> AMB
> "Hamed" wrote:
>|||Hamed,
can you post the execution plan?
AMB
"Hamed" wrote:

> Thanks for your reply, it take 135 secs to execute,
> Alejandro Mesa wrote:
>|||I don't know how to post the execution plan to this group,
Alejandro Mesa wrote:[vbcol=seagreen]
> Hamed,
> can you post the execution plan?
>
> AMB
> "Hamed" wrote:
>|||Hamed,
Use "set showplan_text on" to get the execution plan as text.
set showplan_text on
go
select ...
go
set showplan_text off
go
AMB
"Hamed" wrote:

> I don't know how to post the execution plan to this group,
> Alejandro Mesa wrote:
>|||This is yours:
|--Compute Scalar(DEFINE[b].[Rate]=[b].[Rate]))
|--Nested Loops(Left Outer Join, OUTER
REFERENCES[tblPart].[pkCode]))
|--Filter(WHERE[Expr1003]>0))
| |--Merge Join(Inner Join,
MERGE[Rate].[fkPart])=([tblPart].[pkCode]),
RESIDUAL[tblPart].[pkCode]=[Rate].[fkPart]))
| |--Stream Aggregate(GROUP BY[Rate].[fkPart])
DEFINE[Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
| | |--Index
Scan(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] AS
[Rate]),
WHERE[Rate].[Date]<'850515') ORDERED FORWARD)
| |--Index
Scan(OBJECT[PosKish].[dbo].[tblPart].[tblPart16]), ORDERED
FORWARD)
|--Top(1)
|--Filter(WHEREConvert([b].[IsRated])=1))
|--Bookmark Lookup(BOOKMARK[Bmk1004]),
OBJECT[PosKish].[dbo].[tblInvRate] AS [b]) WITH PREFETCH)
|--Index
Seek(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] AS
[b]),
SEEK[b].[fkPart]=[tblPart].[pkCode] AND [b].[Date]
<= '850515')
ORDERED FORWARD)
and this is mine:
|--Compute Scalar(DEFINE[tblInvRate].[Rate]=[tblInvRate].[
Rate]))
|--Nested Loops(Left Outer Join, OUTER
REFERENCES[tblPart].[pkCode]))
|--Filter(WHERE[Expr1003]>0))
| |--Merge Join(Inner Join,
MERGE[Rate].[fkPart])=([tblPart].[pkCode]),
RESIDUAL[tblPart].[pkCode]=[Rate].[fkPart]))
| |--Stream Aggregate(GROUP BY[Rate].[fkPart])
DEFINE[Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
| | |--Index
Scan(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] AS
[Rate]),
WHERE[Rate].[Date]<'850515') ORDERED FORWARD)
| |--Index
Scan(OBJECT[PosKish].[dbo].[tblPart].[tblPart16]), ORDERED
FORWARD)
|--Compute
Scalar(DEFINE[tblInvRate].[Rate]=[tblInvRate].[Rate]))
|--Sort(TOP 1, ORDER BY[tblInvRate].[Date] DESC,
[tblInvRate].[Time] DESC))
|--Filter(WHEREConvert([tblInvRate].[IsRated])=1))
|--Bookmark Lookup(BOOKMARK[Bmk1004]),
OBJECT[PosKish].[dbo].[tblInvRate]) WITH PREFETCH)
|--Index
Seek(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11]),
SEEK[tblInvRate].[fkPart]=[tblPart].[pkCode] AND [tblI
nvRate].[Date]
<= '850515') ORDERED FORWARD)|||Hamed,
Please, can you post indexes for each table also?
Seems that column [dbo].[tblInvRate].[IsRated] is not integer,
correct?.
Ttry casting the value you compare with. Let us assume it is bit data type,
then:
|--Filter(WHEREConvert([b].[IsRated])=1))
(
SELECT TOP 1
Rate
FROM
dbo.tblInvRate
WHERE
fkPart = dbo.tblPart.pkCode
AND [Date]<='850515'
And IsRated = cast(1 as bit) <-- change this
)
AMB
"Hamed" wrote:

> This is yours:
> |--Compute Scalar(DEFINE[b].[Rate]=[b].[Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES[tblPart].[pkCode]))
> |--Filter(WHERE[Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE[Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL[tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY[Rate].[fkPart
])
> DEFINE[Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] A
S [Rate]),
> WHERE[Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblPart].[tblPart16]), ORDER
ED FORWARD)
> |--Top(1)
> |--Filter(WHEREConvert([b].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK[Bmk1004]),
> OBJECT[PosKish].[dbo].[tblInvRate] AS [b]) WITH PREFETCH
)
> |--Index
> Seek(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] A
S [b]),
> SEEK[b].[fkPart]=[tblPart].[pkCode] AND [b].[Dat
e] <= '850515')
> ORDERED FORWARD)
> and this is mine:
> |--Compute Scalar(DEFINE[tblInvRate].[Rate]=[tblInvRate].&
#91;Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES[tblPart].[pkCode]))
> |--Filter(WHERE[Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE[Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL[tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY[Rate].[fkPart
])
> DEFINE[Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] A
S [Rate]),
> WHERE[Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblPart].[tblPart16]), ORDER
ED FORWARD)
> |--Compute
> Scalar(DEFINE[tblInvRate].[Rate]=[tblInvRate].[Rate]))
> |--Sort(TOP 1, ORDER BY[tblInvRate].[Date] DESC
,
> [tblInvRate].[Time] DESC))
> |--Filter(WHEREConvert([tblInvRate].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK[Bmk1004]),
> OBJECT[PosKish].[dbo].[tblInvRate]) WITH PREFETCH)
> |--Index
> Seek(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11]),
> SEEK[tblInvRate].[fkPart]=[tblPart].[pkCode] AND [tb
lInvRate].[Date]
> <= '850515') ORDERED FORWARD)
>|||Hamed,
In my first post I forgot the "order by" clause in the correlated subquery.
I think that is the reason why we are getting diffrent execution plans.
select
a.*,
(
SELECT TOP 1
b.Rate
FROM
dbo.tblInvRate as b
WHERE
b.fkPart = a.pkCode
AND b.[Date] <= '850515'
And b.IsRated = 1
ORDER BY [Date] DESC, [Time] DESC <-- forgot this
) as Rate
from
(
SELECT
dbo.tblPart.pkCode,
dbo.tblPart.Title,
SUM(Rate.Qty * Rate.Type) AS PriorRemain
FROM
dbo.tblInvRate as Rate
INNER JOIN
dbo.tblPart
ON Rate.fkPart = dbo.tblPart.pkCode
WHERE
Rate.[Date] < '850515'
GROUP BY
dbo.tblPart.pkCode, dbo.tblPart.Title
HAVING
SUM(Rate.Qty * Rate.Type)>0
) as a
go
AMB
"Hamed" wrote:

> This is yours:
> |--Compute Scalar(DEFINE[b].[Rate]=[b].[Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES[tblPart].[pkCode]))
> |--Filter(WHERE[Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE[Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL[tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY[Rate].[fkPart
])
> DEFINE[Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] A
S [Rate]),
> WHERE[Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblPart].[tblPart16]), ORDER
ED FORWARD)
> |--Top(1)
> |--Filter(WHEREConvert([b].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK[Bmk1004]),
> OBJECT[PosKish].[dbo].[tblInvRate] AS [b]) WITH PREFETCH
)
> |--Index
> Seek(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] A
S [b]),
> SEEK[b].[fkPart]=[tblPart].[pkCode] AND [b].[Dat
e] <= '850515')
> ORDERED FORWARD)
> and this is mine:
> |--Compute Scalar(DEFINE[tblInvRate].[Rate]=[tblInvRate].&
#91;Rate]))
> |--Nested Loops(Left Outer Join, OUTER
> REFERENCES[tblPart].[pkCode]))
> |--Filter(WHERE[Expr1003]>0))
> | |--Merge Join(Inner Join,
> MERGE[Rate].[fkPart])=([tblPart].[pkCode]),
> RESIDUAL[tblPart].[pkCode]=[Rate].[fkPart]))
> | |--Stream Aggregate(GROUP BY[Rate].[fkPart
])
> DEFINE[Expr1003]=SUM([Rate].[Qty]*[Rate].[Type])))
> | | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11] A
S [Rate]),
> WHERE[Rate].[Date]<'850515') ORDERED FORWARD)
> | |--Index
> Scan(OBJECT[PosKish].[dbo].[tblPart].[tblPart16]), ORDER
ED FORWARD)
> |--Compute
> Scalar(DEFINE[tblInvRate].[Rate]=[tblInvRate].[Rate]))
> |--Sort(TOP 1, ORDER BY[tblInvRate].[Date] DESC
,
> [tblInvRate].[Time] DESC))
> |--Filter(WHEREConvert([tblInvRate].[IsRated])=1))
> |--Bookmark Lookup(BOOKMARK[Bmk1004]),
> OBJECT[PosKish].[dbo].[tblInvRate]) WITH PREFETCH)
> |--Index
> Seek(OBJECT[PosKish].[dbo].[tblInvRate].[tblInvRate11]),
> SEEK[tblInvRate].[fkPart]=[tblPart].[pkCode] AND [tb
lInvRate].[Date]
> <= '850515') ORDERED FORWARD)
>