Home All Groups Group Topic Archive Search About
Author
17 Aug 2006 2:46 PM
Roshan Jayalath
Hi all,

I have a large table from which I need to fetch data according to a given
filter conditions. But I will only need to fetch a set of records (Eg : To
display the first 200 records in the table.). Still I would need the total
no. of records which will satisfy the filter to display a text like
"Displaying first 200 records out of 50000 records matching the filter
criteria"

Currently what I do is doing two queries,

1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>

Running the same query twice would be slow as the number of records would be
huge. Can anyone advice me of a way to run an optimize query without running
the same query twice.

Best Regards,
Roshan Jayalath

Author
17 Aug 2006 2:56 PM
amish
Roshan Jayalath wrote:

Show quote
> Hi all,
>
> I have a large table from which I need to fetch data according to a given
> filter conditions. But I will only need to fetch a set of records (Eg : To
> display the first 200 records in the table.). Still I would need the total
> no. of records which will satisfy the filter to display a text like
> "Displaying first 200 records out of 50000 records matching the filter
> criteria"
>
> Currently what I do is doing two queries,
>
> 1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
> 2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
>
> Running the same query twice would be slow as the number of records would be
> huge. Can anyone advice me of a way to run an optimize query without running
> the same query twice.
>
> Best Regards,
> Roshan Jayalath

If you are using SQL Server 2005 you can use rowcount function
partition on filed which you are filtering. Then you can first 200 row
numbers and max row number for each partition in single query.

Regards
Amish Shah
http://shahamishm.tripod.com
Author
17 Aug 2006 3:20 PM
Roshan Jayalath
Hi Amish,

Thanks for the quick response. But unfortunately I have to use SQL Server
2000 for the time beign.

Best Regards,
Roshan Jayalath

Show quote
"amish" wrote:

>
> Roshan Jayalath wrote:
>
> > Hi all,
> >
> > I have a large table from which I need to fetch data according to a given
> > filter conditions. But I will only need to fetch a set of records (Eg : To
> > display the first 200 records in the table.). Still I would need the total
> > no. of records which will satisfy the filter to display a text like
> > "Displaying first 200 records out of 50000 records matching the filter
> > criteria"
> >
> > Currently what I do is doing two queries,
> >
> > 1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
> > 2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
> >
> > Running the same query twice would be slow as the number of records would be
> > huge. Can anyone advice me of a way to run an optimize query without running
> > the same query twice.
> >
> > Best Regards,
> > Roshan Jayalath
>
> If you are using SQL Server 2005 you can use rowcount function
> partition on filed which you are filtering. Then you can first 200 row
> numbers and max row number for each partition in single query.
>
> Regards
> Amish Shah
> http://shahamishm.tripod.com
>
>
Author
17 Aug 2006 3:48 PM
Tav
Roshan Jayalath wrote:
Show quote
> Hi Amish,
>
> Thanks for the quick response. But unfortunately I have to use SQL Server
> 2000 for the time beign.
>
> "amish" wrote:
>
> >
> > Roshan Jayalath wrote:
> >
> > > Hi all,
> > >
> > > I have a large table from which I need to fetch data according to a given
> > > filter conditions. But I will only need to fetch a set of records (Eg : To
> > > display the first 200 records in the table.). Still I would need the total
> > > no. of records which will satisfy the filter to display a text like
> > > "Displaying first 200 records out of 50000 records matching the filter
> > > criteria"
> > >
> > > Currently what I do is doing two queries,
> > >
> > > 1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
> > > 2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
> > >
> > > Running the same query twice would be slow as the number of records would be
> > > huge. Can anyone advice me of a way to run an optimize query without running
> > > the same query twice.
> > >
> > > Best Regards,
> > > Roshan Jayalath
> >
> > If you are using SQL Server 2005 you can use rowcount function
> > partition on filed which you are filtering. Then you can first 200 row
> > numbers and max row number for each partition in single query.
> >
> > Regards
> > Amish Shah
> > http://shahamishm.tripod.com
> >
> >

Roshan Jayalath,

I have tried to mimick this on SQL Server 2000 and the best I can come
up with is as follows:

SELECT TOP 200    A.FullCount,
        FirstName,
        LastName
FROM        fAdrBook, (SELECT COUNT(Int_Key) AS FullCount From fAdrBook WHERE
<Filter condition>) A
WHERE        <Filter condition>

However, I doubt whether this helps as it's still two SELECT queries.

Regards,

-Tav.-
Tavis Pitt
Author
17 Aug 2006 5:08 PM
Alexander Kuznetsov
Roshan,

try saving intermediate results in a temporary table (or table
variable):

SELECT Int_Key, FirstName, LastName
into #t
>From fAdrBook WHERE <Filter condition>


SELECT TOP 200 FirstName, LastName From #t
2. SELECT COUNT(Int_Key) From #t
Author
18 Aug 2006 5:19 AM
Roshan Jayalath
Hi all,

Thanks for all of your kind replies. I tried this one.

SELECT TOP 20 A.*,  @@ROWCOUNT AS X FROM fAdrBook A,
(SELECT B.Int_Key FROM fAdrBook B WHERE <Filter Condition)
AS C WHERE A.Int_Key = C.Int_Key

But it doesnt work as @@RowCount doesnt give the rowcount returned by the
inner select as I expected it to work. Any thoughts ? I also tried something
like the below

SELECT TOP 20 A.*, B.Cnt FROM fAdrBook A,
(SELECT COUNT(B.Int_Key) AS Cnt,  B.Int_Key FROM fAdrBook B WHERE <Filter
Condition Group By ....)
AS C WHERE A.Int_Key = C.Int_Key

Int_Key is a unique auto incrementing key. The problem is the group by
clause, because if I put B.Int_Key in the select list I ll have to group by
it as well, which will cause COUNT(b.Int_Key) to be always 1.

Best Regards,
Roshan Jayalath
Author
18 Aug 2006 6:22 AM
amish
Roshan Jayalath wrote:

Show quote
> Hi all,
>
> Thanks for all of your kind replies. I tried this one.
>
> SELECT TOP 20 A.*,  @@ROWCOUNT AS X FROM fAdrBook A,
> (SELECT B.Int_Key FROM fAdrBook B WHERE <Filter Condition)
> AS C WHERE A.Int_Key = C.Int_Key
>
> But it doesnt work as @@RowCount doesnt give the rowcount returned by the
> inner select as I expected it to work. Any thoughts ? I also tried something
> like the below
>
> SELECT TOP 20 A.*, B.Cnt FROM fAdrBook A,
> (SELECT COUNT(B.Int_Key) AS Cnt,  B.Int_Key FROM fAdrBook B WHERE <Filter
> Condition Group By ....)
> AS C WHERE A.Int_Key = C.Int_Key
>
> Int_Key is a unique auto incrementing key. The problem is the group by
> clause, because if I put B.Int_Key in the select list I ll have to group by
> it as well, which will cause COUNT(b.Int_Key) to be always 1.
>
> Best Regards,
> Roshan Jayalath

Try this

SELECT TOP 200 FirstName, LastName, (SELECT COUNT(Int_Key) From
fAdrBook WHERE <Filter condition> ) as cnt
From fAdrBook WHERE <Filter condition>

or

SELECT TOP 200 FirstName, LastName, a.cnt
From fAdrBook ,(SELECT COUNT(Int_Key) cnt From fAdrBook WHERE <Filter
condition> )  a
WHERE <Filter condition>


Regards
Amish Shah
http://shahamishm.tripod.com

SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
Author
18 Aug 2006 11:03 AM
Roshan Jayalath
Thanks amish,

But again that will ru the same query more than once aint it ?

Roshan

Show quote
"amish" wrote:

>
> Roshan Jayalath wrote:
>
> > Hi all,
> >
> > Thanks for all of your kind replies. I tried this one.
> >
> > SELECT TOP 20 A.*,  @@ROWCOUNT AS X FROM fAdrBook A,
> > (SELECT B.Int_Key FROM fAdrBook B WHERE <Filter Condition)
> > AS C WHERE A.Int_Key = C.Int_Key
> >
> > But it doesnt work as @@RowCount doesnt give the rowcount returned by the
> > inner select as I expected it to work. Any thoughts ? I also tried something
> > like the below
> >
> > SELECT TOP 20 A.*, B.Cnt FROM fAdrBook A,
> > (SELECT COUNT(B.Int_Key) AS Cnt,  B.Int_Key FROM fAdrBook B WHERE <Filter
> > Condition Group By ....)
> > AS C WHERE A.Int_Key = C.Int_Key
> >
> > Int_Key is a unique auto incrementing key. The problem is the group by
> > clause, because if I put B.Int_Key in the select list I ll have to group by
> > it as well, which will cause COUNT(b.Int_Key) to be always 1.
> >
> > Best Regards,
> > Roshan Jayalath
>
> Try this
>
> SELECT TOP 200 FirstName, LastName, (SELECT COUNT(Int_Key) From
> fAdrBook WHERE <Filter condition> ) as cnt
>  From fAdrBook WHERE <Filter condition>
>
> or
>
> SELECT TOP 200 FirstName, LastName, a.cnt
>  From fAdrBook ,(SELECT COUNT(Int_Key) cnt From fAdrBook WHERE <Filter
> condition> )  a
> WHERE <Filter condition>
>
>
> Regards
> Amish Shah
> http://shahamishm.tripod.com
>
> SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
>
>
Author
18 Aug 2006 9:23 PM
Tracy McKibben
Roshan Jayalath wrote:
Show quote
> Hi all,
>
> I have a large table from which I need to fetch data according to a given
> filter conditions. But I will only need to fetch a set of records (Eg : To
> display the first 200 records in the table.). Still I would need the total
> no. of records which will satisfy the filter to display a text like
> "Displaying first 200 records out of 50000 records matching the filter
> criteria"
>
> Currently what I do is doing two queries,
>
> 1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
> 2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
>
> Running the same query twice would be slow as the number of records would be
> huge. Can anyone advice me of a way to run an optimize query without running
> the same query twice.
>
> Best Regards,
> Roshan Jayalath

How do I page through a recordset?
http://databases.aspfaq.com/database/how-do-i-page-through-a-recordset.html


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
18 Aug 2006 9:50 PM
rpresser
Roshan Jayalath wrote:
Show quote
> Hi all,
>
> I have a large table from which I need to fetch data according to a given
> filter conditions. But I will only need to fetch a set of records (Eg : To
> display the first 200 records in the table.). Still I would need the total
> no. of records which will satisfy the filter to display a text like
> "Displaying first 200 records out of 50000 records matching the filter
> criteria"
>
> Currently what I do is doing two queries,
>
> 1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
> 2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
>
> Running the same query twice would be slow as the number of records would be
> huge. Can anyone advice me of a way to run an optimize query without running
> the same query twice.

Do you have enough indexes on your tables?  Unless "large table" means
more than ten million records, there's no reason that running the query
through twice should be time consuming.
Author
20 Aug 2006 6:32 AM
Roshan Jayalath
Hi again,

Tracy, Thanks for the resourceful link that you provided.

RPresser, Thanks. We do not have more than 10 million records. Maximum it
will be five hundred thousand and the required columns are indexed. But still
we need to increase the overall performance, to display the data with minimal
time. (Eg: For a user to see 10 records in a grid (with the total no. of
records matching his filter) it wont be good to take minutes, I guess you
would agree.

Best Regards,
Roshan
Show quote
"rpresser" wrote:

>
> Roshan Jayalath wrote:
> > Hi all,
> >
> > I have a large table from which I need to fetch data according to a given
> > filter conditions. But I will only need to fetch a set of records (Eg : To
> > display the first 200 records in the table.). Still I would need the total
> > no. of records which will satisfy the filter to display a text like
> > "Displaying first 200 records out of 50000 records matching the filter
> > criteria"
> >
> > Currently what I do is doing two queries,
> >
> > 1. SELECT TOP 200 FirstName, LastName From fAdrBook WHERE <Filter condition>
> > 2. SELECT COUNT(Int_Key) From fAdrBook WHERE <Filter condition>
> >
> > Running the same query twice would be slow as the number of records would be
> > huge. Can anyone advice me of a way to run an optimize query without running
> > the same query twice.
>
> Do you have enough indexes on your tables?  Unless "large table" means
> more than ten million records, there's no reason that running the query
> through twice should be time consuming.
>
>
Author
21 Aug 2006 6:05 PM
rpresser
Roshan Jayalath wrote:
> Hi again,
>
> Tracy, Thanks for the resourceful link that you provided.
>
> RPresser, Thanks. We do not have more than 10 million records. Maximum it
> will be five hundred thousand and the required columns are indexed. But still
> we need to increase the overall performance, to display the data with minimal
> time. (Eg: For a user to see 10 records in a grid (with the total no. of
> records matching his filter) it wont be good to take minutes, I guess you
> would agree.

Querying a properly indexed table of a half million records should
never take more than a second total for both the count run and
returning the first 10 records.

AddThis Social Bookmark Button