KnowSkill Posted August 11, 2015 Share Posted August 11, 2015 I am building an iOS app that makes a post to a php file I have on a web server. The php file then1. 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? Quote Link to comment https://forums.phpfreaks.com/topic/297722-php-mysql-simultaneous-requests/ Share on other sites More sharing options...
mac_gyver Posted August 11, 2015 Share Posted August 11, 2015 (edited) 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 August 11, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/297722-php-mysql-simultaneous-requests/#findComment-1518445 Share on other sites More sharing options...
mac_gyver Posted August 11, 2015 Share Posted August 11, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297722-php-mysql-simultaneous-requests/#findComment-1518447 Share on other sites More sharing options...
KnowSkill Posted August 11, 2015 Author Share Posted August 11, 2015 The rows cannot be generated beforehand, but if they already exist they can be updated. Quote Link to comment https://forums.phpfreaks.com/topic/297722-php-mysql-simultaneous-requests/#findComment-1518490 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.