php - MySQL COUNT vs SELECT rows performance -


i have small table 3 columns , using php.

table (id unsigned int(10), gameid unsigned int(10), userid unsigned int(10)) - id, gameid , userid indexed.

at point (when player join game) system check if game have 100 players in it, if retrive rows , actions on them , in end rows deleted.

select query:

select * table gameid = 1 order id asc limit 100 

count query:

select count(*) rows table gameid = 1 limit 100 

delete query:

delete table gameid = 1 order id asc limit 100 

my question performance , speed, better:

approach 1 (3 queries)

  • execute count query , check how many rows in table
  • if rows >= 100 do:
    • execute select query , process data
    • execute delete query

approach 2 (2 query)

  • execute select query
  • if count(rows) >= 100 do:
    • process data
    • execute delete query

what approach better use?

thanks

microtime(true) php friend.

if there index starting gameid, "approach 1" might faster, since can count(*) in index.

on other hand, there 2 things count against it:

  • 3 queries slower 2.
  • if there lot more 100 rows gameid = 1 (which not seem case), spend more time doing full count. "approach 2" stops @ 100.

i think real answer "there not enough difference between 2 matter.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -