Jump to content

mysql_num_rows


LanceT

Recommended Posts

Heres some of the code Im using

 


$get = mysql_query("SELECT * FROM getem WHERE get1='$user' OR get2='$user'", $dbh); 

$getnum = mysql_num_rows($get);

 

Assuming that the table getem is HUGE, would this query take up a lot of resources?

Link to comment
Share on other sites

how intensive is it to use

mysql_num_rows to get a number of rows?

 

I assumed you meant "get THE number of rows", in which case it is VERY inefficient. It has to select every row from the table and return it. So in that situation COUNT(*) would be a huge performance gain.

 

Now the second time around, you show me a query that probably returns one or two rows. In that case it really wont make much of a difference, especially since you are probably going to want to know who they are.

Link to comment
Share on other sites

how intensive is it to use

mysql_num_rows to get a number of rows?

 

I assumed you meant "get THE number of rows", in which case it is VERY inefficient. It has to select every row from the table and return it. So in that situation COUNT(*) would be a huge performance gain.

 

Now the second time around, you show me a query that probably returns one or two rows. In that case it really wont make much of a difference, especially since you are probably going to want to know who they are.

 

so my query, even if run on a 1,000,000 row table, won't take up too many resources?

 

Thanks for the input.

Link to comment
Share on other sites

Well the tricky part is finding which records match. Its no big deal IF you've got get1 and get2 indexed. Whether you are returning the results or just counting them doesn't really matter.

 

alright well I indexed them, but also is there any loss if I index rows? If not, then why don't people just index everything?

Link to comment
Share on other sites

You don't index everything because it slows write performance a bit (it not only has to write the data but update the index as well) and it takes up more disk space.

 

We just had a discussion earlier this week on what to index.

Link to comment
Share on other sites

Basically, it comes down to having to retrieve the contents of each of the matching records, versus just counting if any records are found -- the latter is almost always faster, unless you're going to examine each one in turn anyway.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.