473,511 Members | 12,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MySQL: Number of Rows Question

Ok, what I want to do is find out the number of rows in a table. The most
obvious solution is to do something like the following:

$sql = "SELECT blah FROM blah WHERE 1";
$result = mysql_query($sql, $db);
$num = mysql_num_rows($result);

If you have a large table, to me that seems like it would be a system hog if
all you want is the number of rows but not the data. I'm not sure if using
mysql_unbuffered_query() would solve this, would it?

Basically my question is this: what is the fastest, most effecient way to
just find out the number of rows in a table?

Thank you.
Jul 16 '05 #1
3 24092
On Mon, 28 Jul 2003 14:22:40 GMT, "Xizor" <no**@nope.com> wrote:
Ok, what I want to do is find out the number of rows in a table. The most
obvious solution is to do something like the following:

$sql = "SELECT blah FROM blah WHERE 1";
$result = mysql_query($sql, $db);
$num = mysql_num_rows($result);
That is the worst possible way, fetching the entire contents of the
table off disk, into memory, transferred over to the client, and then
ignored.
If you have a large table, to me that seems like it would be a system hog if
all you want is the number of rows but not the data. I'm not sure if using
mysql_unbuffered_query() would solve this, would it?
No, because mysql_num_rows becomes unavailable, as the database
doesn't know how many rows are in the result set - it starts
transferring them to the client before they've all been read out of
the database.
Basically my question is this: what is the fastest, most effecient way to
just find out the number of rows in a table?


If you want to count the number of rows, use COUNT, which returns a
single row with the number of rows matched.

MySQL keeps total number of rows stored in a table as part of the
metadata for a table, so 'select count(*) from tab' doesn't even
access the table.

And if you have a WHERE clause, then you avoid fetching all the data
that you don't want if you use COUNT; the database just counts the
number of matching rows without reading the data itself.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2
use

select count(*) from blah;

It will return just the number.
"Xizor" <no**@nope.com> wrote in message
news:QkaVa.147137$GL4.37783@rwcrnsc53...
Ok, what I want to do is find out the number of rows in a table. The most
obvious solution is to do something like the following:

$sql = "SELECT blah FROM blah WHERE 1";
$result = mysql_query($sql, $db);
$num = mysql_num_rows($result);

If you have a large table, to me that seems like it would be a system hog if all you want is the number of rows but not the data. I'm not sure if using
mysql_unbuffered_query() would solve this, would it?

Basically my question is this: what is the fastest, most effecient way to
just find out the number of rows in a table?

Thank you.

Jul 16 '05 #3
In article <%Z********************@newssrv26.news.prodigy.com >,
"s a n j a y" <so*****@somewhere.com> wrote:
use

select count(*) from blah;

It will return just the number.


FYI, if the original poster has learned SQL mostly through PHP books,
etc., he might wanna look into some SQL-specific books. I did, and was
surprised at how many things one can do with the SQL query itself (like
the above!).

--
Floydian Slip(tm) - "Broadcasting from the dark side of the moon"
Random Precision Productions(tm)
67 Union St. #2D, Winooski, Vt. 05404-1948 USA
Sundays, 7-8 pm - Champ 101.3 FM, Colchester; 102.1 FM, Randolph, Vt.
cc*@floydianslip.com - AIM: RandomPrec - www.floydianslip.com
Jul 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3994
by: Phil Powell | last post by:
Relevancy scores are normally defined by a MySQL query on a table that has a fulltext index. The rules for relevancy scoring will exclude certain words due to their being too short (minimum...
0
5755
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
8
4475
by: Tony Clarke | last post by:
Hi, Just a quick question about performance with MySQL & PHP. If I had a table in a MySQL database with about 100,000 records in it and I need to find the last record is there a quick way to do...
6
4384
by: knoak | last post by:
Hi there, I have a small question: I have a table with lots of rows in it. Of course all have a different id, but each can be assigned to a certain category. Categories correspond with 1 - 10....
0
3925
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
3
2050
by: Sven Reifegerste | last post by:
Hi, i have a table with INT columns id,key,b1,b2,c1,c2, having 1.500.000 rows. 'key' and 'id' are indexed (Kardinality 385381) and id (Kardinality 1541525). Performing a SELECT * FROM...
9
1864
by: elyob | last post by:
Hi, I'm looking at storing snippets of details in MySQL about what credit cards a business excepts. Rather than have a whole column for Visa, another for Amex etc ... I am looking at having a...
2
1886
by: moller | last post by:
Im looking in to the possibility of moving from mySQL to an access database. My reasons are: (1) Database is single user. (2) Database local on users PC. (3) Database has only 8 tables where 4...
6
38453
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
7251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7148
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.