Unix Technical Forum

Fast way to do "SELECT count(*) FROM table"

This is a discussion on Fast way to do "SELECT count(*) FROM table" within the MySQL forums, part of the Database Server Software category; --> Hello. I start to write a simple Database management tool and i want to display the number of rows ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:31 AM
llothar
 
Posts: n/a
Default Fast way to do "SELECT count(*) FROM table"

Hello. I start to write a simple Database management tool and i want
to display the number of rows in a table. Unfortunately MySQL is
extremely slow on this "SELECT count(*) FROM table" statements. Looks
like a full table scan for me. Is there any other way to retrieve this
information?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:31 AM
SigPower@gmail.com
 
Posts: n/a
Default Re: Fast way to do "SELECT count(*) FROM table"

On Dec 22, 9:55 am, llothar <llot...@web.de> wrote:
> Hello. I start to write a simple Database management tool and i want
> to display the number of rows in a table. Unfortunately MySQL is
> extremely slow on this "SELECT count(*) FROM table" statements. Looks
> like a full table scan for me. Is there any other way to retrieve this
> information?


What engine are you using? I use MyISAM almost exclusively and I can
assure you that this engine does not need a table scan to return the
number of records in a table. I did a select count(*) on a MyISAM
table with just over 20 million rows in .72 seconds on my slow
laptop. The number of records is part of the table's metadata.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:31 AM
Kees Nuyt
 
Posts: n/a
Default Re: Fast way to do "SELECT count(*) FROM table"

On Sat, 22 Dec 2007 06:55:07 -0800 (PST), llothar
<llothar@web.de> wrote:

>Hello. I start to write a simple Database management tool and i want
>to display the number of rows in a table. Unfortunately MySQL is
>extremely slow on this "SELECT count(*) FROM table" statements. Looks
>like a full table scan for me. Is there any other way to retrieve this
>information?


Does your table have a PRIMARY KEY ?
--
( Kees
)
c[_] Nostalgia. Sure ain't what it used to be.... (#26)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:31 AM
John Nagle
 
Posts: n/a
Default Re: Fast way to do "SELECT count(*) FROM table"

SigPower@gmail.com wrote:
> On Dec 22, 9:55 am, llothar <llot...@web.de> wrote:
>> Hello. I start to write a simple Database management tool and i want
>> to display the number of rows in a table. Unfortunately MySQL is
>> extremely slow on this "SELECT count(*) FROM table" statements. Looks
>> like a full table scan for me. Is there any other way to retrieve this
>> information?

>
> What engine are you using? I use MyISAM almost exclusively and I can
> assure you that this engine does not need a table scan to return the
> number of records in a table. I did a select count(*) on a MyISAM
> table with just over 20 million rows in .72 seconds on my slow
> laptop. The number of records is part of the table's metadata.


MyISAM has a record count, so COUNT(*) is cheap. InnoDB tables
do not, so COUNT(*) there can be expensive. There's the issue that
"number of records" is non-trivial to measure when transactions are
in progress.

John Nagle
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:28 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com