I'm trying to find out what's the fastest way to execute such a query.
Presumely I have 1 denormalized table (TableTest) with 2 columns A and B
A - Nvarchar(50)
B - smalltimestamp
I have 10 records in this table out of which column A has 2 distinct values
(Test1 and Test2) and B has 10 distinct values, so it might look something
like this
A B
Test1 2006-03-31 15:21:00
Test1 2006-03-23 15:21:00
Test1 2006-02-01 15:21:00
..
Test2 2006-04-01 15:21:00
Test2 2006-03-31 15:12:00
Test2 2006-01-28 13:21:00
Now I want to execute a query on this table to return the latest occurrence
of A and B, so the results should look like this
A B
Test1 2006-03-31 15:21:00
Test2 2006-04-01 15:21:00
What is the fastest way to do this? I've tried a couple of methods but
they're all slow.I'd say that the construct which is designed for this type of operation has
the best chance for best
performance:
SELECT A, MAX(B)
FROM tblname
GROUP BY A
And then you make sure you have good indexes to support that query. Perhaps
a non-clustered index on
(A,B) is the best choice here.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Nestor" <n3570r@.yahoo.com> wrote in message news:%23N1WqPJVGHA.4436@.TK2MSFTNGP10.phx.gbl..
.
> I'm trying to find out what's the fastest way to execute such a query.
> Presumely I have 1 denormalized table (TableTest) with 2 columns A and B
> A - Nvarchar(50)
> B - smalltimestamp
> I have 10 records in this table out of which column A has 2 distinct value
s (Test1 and Test2) and
> B has 10 distinct values, so it might look something like this
> A B
> Test1 2006-03-31 15:21:00
> Test1 2006-03-23 15:21:00
> Test1 2006-02-01 15:21:00
> ..
> Test2 2006-04-01 15:21:00
> Test2 2006-03-31 15:12:00
> Test2 2006-01-28 13:21:00
> Now I want to execute a query on this table to return the latest occurrenc
e of A and B, so the
> results should look like this
> A B
> Test1 2006-03-31 15:21:00
> Test2 2006-04-01 15:21:00
> What is the fastest way to do this? I've tried a couple of methods but the
y're all slow.
>
>
No comments:
Post a Comment