Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008, 03:31 PM
Junior Member
 
Join Date: Mar 2008
Posts: 15
Default php count() or sql count(*) is faster??

I'm building a pagination, but I have a question in my mind

php count() and count(*) in mysql would be better if the data size is getting bigger?

Or do you have other efficient way to get the "number of elements" in a collection and the data all together??

Because if I use sql way, I need to send 2 queries to database....

Last edited by iroy2000 : 03-21-2008 at 03:38 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-24-2008, 03:37 PM
xentek's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 111
Default

MySQL count is probably faster, but not on star. Do it on your primary key field, and it should be faster.

Also, don't call either again and again - set the result to a variable and reference that. Especially in a loop.
__________________
- xentek
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-25-2008, 12:09 PM
Junior Member
 
Join Date: Jan 2008
Posts: 12
Default

If you make php count, this does mean that you have to retrieve all the records ... and query one more time for results depending on current page, therefore it php count is slower.

If you have 100 k records you'll feel the difference.

Slavi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 10:48 PM
Junior Member
 
Join Date: May 2007
Posts: 15
Default

Quote:
Originally Posted by xentek View Post
MySQL count is probably faster, but not on star. Do it on your primary key field, and it should be faster.
Keep in mind this is only safe if the field you're counting on is 'not null'. Counting on fields skips null values.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-17-2008, 02:03 PM
xentek's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 111
Default

Quote:
Originally Posted by Mark^Bastard View Post
Keep in mind this is only safe if the field you're counting on is 'not null'. Counting on fields skips null values.
Indeed. I should have noted that. I usually count on my primary key, which by design does not hold any null values.
__________________
- xentek
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-17-2008, 07:27 PM
Junior Member
 
Join Date: Apr 2008
Location: Germany, Hamburg
Posts: 11
Default

Good design prohibits NULL values in a database .
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



All times are GMT. The time now is 02:00 PM.