Jump to content

Fastest Way


jaymc

Recommended Posts

I want to count how many rows in a database match a members username

 

Whats the best way

 

At current I basically have

 

$q = "SELECT `id` FROM `table` WHERE `username` = 'fred'";

$num = mysql_num_rows($q);

 

Is that the fastest and less intensive way to count?

Link to comment
Share on other sites

$connection is the variable I use as a handle for the database connection

e.g.

       $connection = mysql_connect("localhost","username","password") or die ("Could not connect: " . mysql_error());
       mysql_select_db("databasename",$connection) or die ("Could not select db: " . mysql_error());

 

I don't know if COUNT is quicker, but the easy way to find out is create a large table and do some timings.

 

It's certainly more efficient in Oracle.

Link to comment
Share on other sites

Instead of

 

$querystring = "SELECT COUNT(*) FROM table WHERE username = 'fred'";

 

You may prefer to use

 

$querystring = "SELECT COUNT(id) FROM table WHERE username = 'fred'";

 

Assuming id is a primary key. If you want to go farther, add an index on username. This would be optimal, I believe.

 

The difference isn't super profound right now (~0.0007ms on 300 records, ~0.0021ms on 3,000 records) but will be more appearant with a lot of records.

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.