views:

147

answers:

2

I have a database with 1,000,000 records and a query like this:

select count(code) from table1

It works well on the local system but becomes very slow on the network. Other queries like select * from table execute quickly, but select count(code) from table1 is very slow. I can't change the database's structure. My database is Foxpro and I use VB.NET.

Is there a solution?

Edit: Should I write code like this?

dim ds as new dataset
dim da as new datadapter("select count(*) from table ", connection)
da.fill(ds,"tbl1")

Then how can I get select count(code) from table1 from the dataset?

Or do I have to use LINQ?

Edit 2: I mean the comparison between select count(*) and select count(code).
What is the solution?

+6  A: 

It will be faster to do

select count(*) from table1

then to use count(code).

JacobM
Why? That seems counter-intuitive.
David Lively
@David: Basically, `count(*)` is treated as a special case: It just counts the row without retrieving any data. `count(code)` retrieves the column just to count it.
James Curran
A: 

The select count(code) selects the column 'code' from the table, and then counts them, but select * just selects them, but does not do the count. So if you are comparing these two then logically select * is fast.

For my table which has more than 1,000,000 records the execution time is:

select * = 0.9818625

select count(column_name) = 1.571275

SP249