Jump to content

php MySQL Simultaneous Requests


KnowSkill

Recommended Posts

I am building an iOS app that makes a post to a php file I have on a web server.  The php file then
1. uses the rand function to generate a random number

2. checks to see if this exists in the DB

3. either adds it or makes  a new one (its a little more complicated than that, but for the sake of the question...)

4. echos the result

 

I have build an html/js file that generates 1000 XMLHttpRequests to that page with the proper info and for all of the requests to come back it takes like 45 seconds.  Is there no way to make this kind of thing happen more instantaneous or is it going to have to be slow due to the 1000 simultaneous requests?

Link to comment
Share on other sites

your stated method actually has a race condition where multiple concurrent instances could produce and try to insert the same number.
 
you can do this using one INSERT query, which avoids the race condition. see the following example - http://forums.phpfreaks.com/topic/297377-going-from-sessions-only-to-remember-me/?do=findComment&comment=1517023
 
by only running one query, your code should be at least twice as efficient.
 
i would think that 45 seconds for 1000 requests probably means that your current code has some additional inefficients and/or your database table doesn't have an index (the method in the code i linked to above requires an index.)
 
does what you are using these values for require a random number or could it be an incrementing number? also, how many total numbers will there be, because another way of doing this would be to generate all the possible numbers, randomize them, then store them in a database table. you can just query for the next unused value. to avoid the race condition, you would actually run an UPDATE query to toggle a 'used' bit in an available row. then, if the UPDATE query was successful, you know that the row you just toggled contains your random number. you can get the number from that row by using the msyql LAST_INSERT_ID(expr) function in the query, then retrieve the number by fetching the last inserted id value.

Edited by mac_gyver
Link to comment
Share on other sites

here's a thread/post showing how you would run an UPDATE query to toggle a value and retrieve a value from that row at the same time - http://forums.phpfreaks.com/topic/296803-get-id-after-update/?hl=%2Blast_insert_id&do=findComment&comment=1513845

 

the user_id in that thread would instead be your column holding the previously randomized numbers.

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.