title: Optimize result page to query number of result first
body: <p>Here is my code. But I have some problem about loading time for result more than 1000 rows.</p>
  
  <p>How can I change code to check number of query result for create paginate first. Then, get result only that paginate page?</p>
  
  <pre><code>function showResult(req, res){
  
      var n = req.query.query;
      mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE query_text LIKE "%' + n + '%" ORDER BY popularity DESC LIMIT 0 , 10', function (error, rows) {
          mysql_crawl.query('SELECT prod_name, full_price, discount_price, quantity, fulltext_id,prod_link, images, prod_desc, status, web_name,web_logo FROM `catalogsearch_fulltext` WHERE MATCH(data_index) AGAINST("'+n+'") ', function(error, product_data) {
  
              var totalItems = product_data.length, itemts=product_data;
  
      //set default variables
      var totalResult = totalItems,
          pageSize = 10,
          pageCount = Math.floor(totalResult / pageSize)
          currentPage = 1
  
  
      //set current page if specifed as get variable (eg: /?page=2)
      if (typeof req.query.page !== 'undefined') {
          currentPage = +req.query.page;
      }
  
      //render index.ejs view file
              res.render('result.html', {result: n, 
              related: rows.map(row =&gt; row.query_text), 
              page_num: p,
              product_data: product_data,
              totalItems: totalItems,
              pageSize: pageSize,
              pageCount: pageCount,
              currentPage: currentPage})
          });
      });
  }
  </code></pre>