I have a small table with three columns and using PHP.
TABLE (id unsigned int(10), gameid unsigned int(10), userid unsigned int(10)) - id, gameid and userid are indexed.
At some point (When a player join a game) system will check if the game already have 100 players in it, if so it retrive all rows and do some actions over them and in the end that rows will get deleted.
SELECT Query:
SELECT * FROM table WHERE gameid = 1 ORDER BY id ASC LIMIT 100
COUNT Query:
SELECT COUNT(*) as rows FROM table WHERE gameid = 1 LIMIT 100
DELETE Query:
DELETE FROM table WHERE gameid = 1 ORDER BY id ASC LIMIT 100
My question is about performance and speed, what will be better:
Approach 1 (3 Queries)
- Execute COUNT query and check how many rows are in table
- If rows >= 100 do:
- Execute SELECT Query and process the data
- Execute DELETE Query
Approach 2 (2 Query)
- Execute SELECT Query
- If count(rows) >= 100 do:
- process the data
- Execute DELETE Query
What approach will be better to use?
Thanks