title: PHP 10000 concurrent calls and MYSQL query wait time and slow
body: <p>I have a very simple query that inserts every api call to messageRequest table. it can go maximum of 1000 requests or more per second.</p>
  
  <p>for example, <a href="http://myapplication.com/sendMessage.php?phone=123&amp;gsm=mobitel&amp;message=helloworld" rel="nofollow">http://myapplication.com/sendMessage.php?phone=123&amp;gsm=mobitel&amp;message=helloworld</a></p>
  
  <p>assume, anyone with our app in mobile will call this URL. and when i make like 1000 calls via </p>
  
  <p>ab -n 1000 -c 10 "<a href="http://myapplication.com/sendMessage.php?phone=123&amp;gsm=mobitel&amp;message=helloworld" rel="nofollow">http://myapplication.com/sendMessage.php?phone=123&amp;gsm=mobitel&amp;message=helloworld</a>"</p>
  
  <p>The query is <code>insert into messageRequest values(null, phone, gsm, message);</code></p>
  
  <p>it works fine, but the issue is while this many concurrent calls are there, when i try to use my activity log page in this URL <a href="http://myapplication.com/apiLogs.php" rel="nofollow">http://myapplication.com/apiLogs.php</a> to see how many SMS api calls are received in last 30 minutes. </p>
  
  <p>It just queries <code>SELECT count(*) from messageRequest where created_at &gt; '2016-10-01 12:01:00'</code> </p>
  
  <p>it takes like 100 seconds to return result. and when i stopped making concurrent calls via <code>ab -n command</code> its working fine. so i assumed when 1000 concurrent calls happens and a record is inserted for every call, MYSQL table messageRequest is getting locked.</p>
  
  <p>to avoid this problem, </p>
  
  <p>i changed this to multiple sessions, for example to <code>sendMessage.php</code> i made it run with user1, and for the apiLogs.php i made it with user2. so 2 diff sessions can access the same table to read and write simultaneously but that didnt help too. </p>
  
  <p><strong>sendMessage.php</strong></p>
  
  <pre><code>define('DB_HOST', 'localhost');
  define('DB_USER', 'user1');
  define('DB_PASS', ''); 
  </code></pre>
  
  <p><strong>apiLogs.php</strong></p>
  
  <pre><code>define('DB_HOST', 'localhost');
  define('DB_USER', 'user2');
  define('DB_PASS', '');
  </code></pre>
  
  <p>i am confused how to make it work without any waiting time while so many concurrent calls? </p>
  
  <p><strong>Update</strong></p>
  
  <p>sendMessage.php code</p>
  
  <pre><code>define('DB_HOST', 'localhost');
  define('DB_USER', 'user1');
  define('DB_PASS', 'user1');
  
   mysql_connect(DB_HOST, DB_USER, DB_PASS);
   mysql_select_db('smsApp');
  
  
       mysql_query(
          "insert into messageRequest values (
              NULL,
              '{$_REQUEST['phone']}',
              '{$_REQUEST['gsm']}',
              '{$_REQUEST['message']}',
              '{$_REQUEST['text']}',
              now()
              );
          "
      );
  </code></pre>
  
  <p>And i am making concurrent request with this command </p>
  
  <pre><code>ab -n 1000 -c 10 "http://myapplication.com/sendMessage.php?phone=123&amp;gsm=mobitel&amp;message=helloworld&amp;text=123"
  </code></pre>
  
  <p>And while is running, i am trying to view this page apiLogs.php (source code is below)</p>
  
  <pre><code>  define('DB_HOST', 'localhost');
      define('DB_USER', 'user2');
      define('DB_PASS', 'user2');
  
       mysql_connect(DB_HOST, DB_USER, DB_PASS);
       mysql_select_db('smsApp');
  
  $30m_ago = new DateTime("30 minutes ago");
  $s = $30m_ago-&gt;format("Y-m-d H:i:s");
  $result = mysql_query("SELECT count(*) from messageRequest where created_at &gt; '$s'");
  
  $response['last_30_min_sms_count'] = current(mysql_fetch_row($result));
  
  echo json_encode($response)."\n";
  </code></pre>