Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Monday, March 26, 2012

Performance Question

SQL Server 2000, MSDE

I have a table containing simple financial transactions. The 3 primary fields are:

TranSysID INT(PK)
TranDate DATETIME
TranAmount DECIMAL

TranAmount contains both positive (Credit) and negative (Debit) values.

I need to perform a query that returns the beginning balance and ending balance for a particular day.

BegBal = SELECT SUM(TranAmount) WHERE MONTH(TranDate) < 2 AND DAY(TranDate) < 1 AND YEAR(TranDate) < 2004

EndBal = SELECT SUM(TranAmount) WHERE MONTH(TranDate) < 2 AND DAY(TranDate) < 2 AND YEAR(TranDate) < 2004

SHOULD give me the beggining and ending balance for Feb 1, 2004.

My question is what indexes should I create to give me the best possible performance? Right now I have an index on the TranDate field. I do not on the TranAmount field.

TIA

--
Tim Morrison

------------------------

Vehicle Web Studio - The easiest way to create and maintain your vehicle related website.
http://www.vehiclewebstudio.comTim Morrison (sales@.kjmsoftware.com) writes:
> I have a table containing simple financial transactions. The 3 primary
> fields are:
> TranSysID INT(PK)
> TranDate DATETIME
> TranAmount DECIMAL
> TranAmount contains both positive (Credit) and negative (Debit) values.
> I need to perform a query that returns the beginning balance and ending
> balance for a particular day.
> BegBal = SELECT SUM(TranAmount) WHERE MONTH(TranDate) < 2 AND
> DAY(TranDate) < 1 AND YEAR(TranDate) < 2004
> EndBal = SELECT SUM(TranAmount) WHERE MONTH(TranDate) < 2 AND
> DAY(TranDate) < 2 AND YEAR(TranDate) < 2004
> SHOULD give me the beggining and ending balance for Feb 1, 2004.
> My question is what indexes should I create to give me the best possible
> performance? Right now I have an index on the TranDate field. I do not
> on the TranAmount field.

And that index on TranDate is not likely to be used, the way you have
written the query. This is because the column figures in expressions,
SQL Server cannot seek the index. I can possibly scan.

So you should write the query as:

SELECT @.date = '20020204'
SELECT BegBal = SUM(CASE WHEN TranDate < dateadd(DAY, -1, @.date)
THEN TranAmount
ELSE 0
END),
EndBal = SUM(Tranamount)
FROM tbl
WHERE TranDate < @.date

And you should have a non-clustered index on (TranDate, TranAmount)

The reason that for the somewhat complicated expression on BegBal, is
that I would expect most queries to be on recent dates, so in fact,
you will have to traverse most rows. Better then to only do it once.

Since you are traversing allmost all rows, you are in fact scanning the
data. But, by having both the date and the amount in the index, SQL Server
does not have to read the data pages, but only the narrower non-
clustered index. If there are more columns than you are showing, for
instance an account number, you need to add that column to the index
as well.

In the end you may find that you get better performance, by having this
data computed in advance. This requires more work to maintain the data.
The system I work is about financial transactions (securities trading),
and we have tables with all balances pre-computed.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thank you. It may be possible that I am worrying for nothing. I expect about
10-15 rows added per day. It may be 3 years before I start to notice a
slowdown...

Tim

"Erland Sommarskog" <sommar@.algonet.se> wrote in message
news:Xns947A6DF7DA17Yazorman@.127.0.0.1...
> Tim Morrison (sales@.kjmsoftware.com) writes:
> > I have a table containing simple financial transactions. The 3 primary
> > fields are:
> > TranSysID INT(PK)
> > TranDate DATETIME
> > TranAmount DECIMAL
> > TranAmount contains both positive (Credit) and negative (Debit) values.
> > I need to perform a query that returns the beginning balance and ending
> > balance for a particular day.
> > BegBal = SELECT SUM(TranAmount) WHERE MONTH(TranDate) < 2 AND
> > DAY(TranDate) < 1 AND YEAR(TranDate) < 2004
> > EndBal = SELECT SUM(TranAmount) WHERE MONTH(TranDate) < 2 AND
> > DAY(TranDate) < 2 AND YEAR(TranDate) < 2004
> > SHOULD give me the beggining and ending balance for Feb 1, 2004.
> > My question is what indexes should I create to give me the best possible
> > performance? Right now I have an index on the TranDate field. I do not
> > on the TranAmount field.
> And that index on TranDate is not likely to be used, the way you have
> written the query. This is because the column figures in expressions,
> SQL Server cannot seek the index. I can possibly scan.
> So you should write the query as:
> SELECT @.date = '20020204'
> SELECT BegBal = SUM(CASE WHEN TranDate < dateadd(DAY, -1, @.date)
> THEN TranAmount
> ELSE 0
> END),
> EndBal = SUM(Tranamount)
> FROM tbl
> WHERE TranDate < @.date
> And you should have a non-clustered index on (TranDate, TranAmount)
> The reason that for the somewhat complicated expression on BegBal, is
> that I would expect most queries to be on recent dates, so in fact,
> you will have to traverse most rows. Better then to only do it once.
> Since you are traversing allmost all rows, you are in fact scanning the
> data. But, by having both the date and the amount in the index, SQL Server
> does not have to read the data pages, but only the narrower non-
> clustered index. If there are more columns than you are showing, for
> instance an account number, you need to add that column to the index
> as well.
> In the end you may find that you get better performance, by having this
> data computed in advance. This requires more work to maintain the data.
> The system I work is about financial transactions (securities trading),
> and we have tables with all balances pre-computed.
> --
> Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp

Monday, March 12, 2012

performance problem

Hello there
I have table with more then 30,000,000 records.
When i do simple select on 3 fields who are indexes, i see on the execution
plan 90% on bookmark lookup.
What can cause this?"Roy Goldhammer" <roy@.hotmail.com> wrote in message
news:Oea7Jf9UGHA.4792@.TK2MSFTNGP14.phx.gbl...
> Hello there
> I have table with more then 30,000,000 records.
> When i do simple select on 3 fields who are indexes, i see on the
> execution plan 90% on bookmark lookup.
> What can cause this?
Date: Sat, 31 Dec 2005 10:56:24 +0200
Roy, fix your system clock please.
You're still celebrating New Years Eve.

Performance problem

Hello there
I have two tables with combination of two fields that aren't unique in both
tables.
To make them unique i must, create view of the last id of the fields.
example SELECT Fld1, Fld2, max(id)
FROM tbl
GROUP BY Fld1, Fld2
This cause serious performace problem.
Is there a way to handle it?1)How much of a performance problem?
2)can you send details of indexes implemented?
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
"Roy Goldhammer" <roy@.hotmail.com> wrote in message
news:u8JN8LSYGHA.3604@.TK2MSFTNGP02.phx.gbl...
> Hello there
> I have two tables with combination of two fields that aren't unique in
both
> tables.
> To make them unique i must, create view of the last id of the fields.
> example SELECT Fld1, Fld2, max(id)
> FROM tbl
> GROUP BY Fld1, Fld2
> This cause serious performace problem.
> Is there a way to handle it?
>|||Whell Jack:
1. according to execution plan it caust at least 10 subtree cost more. and
on data which is more then 1,000,000 records any query can take more then an
hour.
2. fld1, and fld2 are indexes as non clustered.
What i need to do?
"Jack Vamvas" <delete_this_bit_jack@.ciquery.com_delete> wrote in message
news:Beydna5nIMCvcdzZnZ2dnUVZ8qWdnZ2d@.bt
.com...
> 1)How much of a performance problem?
> 2)can you send details of indexes implemented?
> --
> Jack Vamvas
> ___________________________________
> Receive free SQL tips - www.ciquery.com/sqlserver.htm
> "Roy Goldhammer" <roy@.hotmail.com> wrote in message
> news:u8JN8LSYGHA.3604@.TK2MSFTNGP02.phx.gbl...
> both
>|||Hi Roy
Do you have an index on [ID] column?
I suggest you to create a COVERING index as
CREATE INDEX Covering_idx on TableName(Fld1, Fld2, id) which may very useful
particular along with ORDER BY clause
"Roy Goldhammer" <roy@.hotmail.com> wrote in message
news:ug0QbaSYGHA.3328@.TK2MSFTNGP02.phx.gbl...
> Whell Jack:
> 1. according to execution plan it caust at least 10 subtree cost more. and
> on data which is more then 1,000,000 records any query can take more then
> an hour.
> 2. fld1, and fld2 are indexes as non clustered.
> What i need to do?
> "Jack Vamvas" <delete_this_bit_jack@.ciquery.com_delete> wrote in message
> news:Beydna5nIMCvcdzZnZ2dnUVZ8qWdnZ2d@.bt
.com...
>|||Roy Goldhammer (roy@.hotmail.com) writes:
> I have two tables with combination of two fields that aren't unique in
> both tables.
> To make them unique i must, create view of the last id of the fields.
> example SELECT Fld1, Fld2, max(id)
> FROM tbl
> GROUP BY Fld1, Fld2
> This cause serious performace problem.
> Is there a way to handle it?
Maybe there is, but with the tiny amount of information you have posted,
it is impossible to give any useful advice.
I suggest that you post:
1) CREATE TABLE statements for you tables.
2) CREATE INDEX statements for the tables.
3) CREATE VIEW statement for your view.
4) A sample query from the view.
5) An indication of the number of rows in the table.
Output from SET STATISTICS PROFILE ON is also good.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||That you need to do is redesign your tables to have a primary key. If
you can't alter this schema, then consider implementing a reporting
database that has a correct schema and design a process to move data
from the original source to your reporting warehouse.
Performance may be an issue, but if you're only moving data
periodically, that issue is mitigated. As others have posted, a
clearer definition would be helpful.
HTH,
Stu|||Yes Uri. The ID is the primary key clustered index.
I have also indexes on Fld1 and Fld2
Whay this is not enouth?
"Uri Dimant" <test@.test.com> wrote in message
news:Od9nH6SYGHA.4652@.TK2MSFTNGP04.phx.gbl...
> Hi Roy
> Do you have an index on [ID] column?
> I suggest you to create a COVERING index as
> CREATE INDEX Covering_idx on TableName(Fld1, Fld2, id) which may very
> useful particular along with ORDER BY clause
> "Roy Goldhammer" <roy@.hotmail.com> wrote in message
> news:ug0QbaSYGHA.3328@.TK2MSFTNGP02.phx.gbl...
>

Friday, March 9, 2012

performance on the join

I know that join on an integer field is faster than join on varchar field.
What if I cast the fields to integer within the join clause,
ex. JOIN CAST(tableA.field AS int) = CAST(tableB.field AS int)
would this speed up my query?
Thanks,
CulamNot very likely, since -- even if there are indexes on these columns -- an
explicit conversion pretty much guarantees a scan.
Did you try it? Did you compare execution plans (both real and estimated),
cpu/reads/statistics i/o, etc?
If all the data is integer, why is the column still defined as varchar?
"culam" <culam@.discussions.microsoft.com> wrote in message
news:0D47D119-344A-419E-A5EB-EA44B06F1F88@.microsoft.com...
>I know that join on an integer field is faster than join on varchar field.
> What if I cast the fields to integer within the join clause,
> ex. JOIN CAST(tableA.field AS int) = CAST(tableB.field AS int)
> would this speed up my query?
> Thanks,
> Culam
>