• <small id='JwRe1'></small><noframes id='JwRe1'>

    <legend id='JwRe1'><style id='JwRe1'><dir id='JwRe1'><q id='JwRe1'></q></dir></style></legend>
    <tfoot id='JwRe1'></tfoot>

  • <i id='JwRe1'><tr id='JwRe1'><dt id='JwRe1'><q id='JwRe1'><span id='JwRe1'><b id='JwRe1'><form id='JwRe1'><ins id='JwRe1'></ins><ul id='JwRe1'></ul><sub id='JwRe1'></sub></form><legend id='JwRe1'></legend><bdo id='JwRe1'><pre id='JwRe1'><center id='JwRe1'></center></pre></bdo></b><th id='JwRe1'></th></span></q></dt></tr></i><div id='JwRe1'><tfoot id='JwRe1'></tfoot><dl id='JwRe1'><fieldset id='JwRe1'></fieldset></dl></div>
        <bdo id='JwRe1'></bdo><ul id='JwRe1'></ul>
      1. 为什么 MySQL 在 SELECT 语句中使用 RAND() 时返回相同

        时间:2023-07-29

        1. <small id='Bwy1X'></small><noframes id='Bwy1X'>

          <legend id='Bwy1X'><style id='Bwy1X'><dir id='Bwy1X'><q id='Bwy1X'></q></dir></style></legend>
              <tbody id='Bwy1X'></tbody>

          1. <i id='Bwy1X'><tr id='Bwy1X'><dt id='Bwy1X'><q id='Bwy1X'><span id='Bwy1X'><b id='Bwy1X'><form id='Bwy1X'><ins id='Bwy1X'></ins><ul id='Bwy1X'></ul><sub id='Bwy1X'></sub></form><legend id='Bwy1X'></legend><bdo id='Bwy1X'><pre id='Bwy1X'><center id='Bwy1X'></center></pre></bdo></b><th id='Bwy1X'></th></span></q></dt></tr></i><div id='Bwy1X'><tfoot id='Bwy1X'></tfoot><dl id='Bwy1X'><fieldset id='Bwy1X'></fieldset></dl></div>

              <bdo id='Bwy1X'></bdo><ul id='Bwy1X'></ul>
              • <tfoot id='Bwy1X'></tfoot>
                  本文介绍了为什么 MySQL 在 SELECT 语句中使用 RAND() 时返回相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我打开了许多浏览器窗口,指向同一个自动刷新的 PHP 页面.它访问 MySQL 数据库以识别过时的客户信息.专门获取最后一天未更新的记录并强制更新.其余代码似乎处理得很好.

                  I have a number of browser windows open pointing to the same auto-refreshing PHP page. It accesses a MySQL database to identify customer information that is out of date. Specifically getting records that haven't been updated in the last day and forces an update. The rest of the code seems to be processing fine.

                  这是我的 MySQLi 查询:

                  Here is my MySQLi query:

                  $query = "SELECT *
                            FROM customers
                            WHERE customer_group='consumables' AND customer_updated < DATE_SUB(NOW(), INTERVAL 1 DAY)
                            ORDER BY RAND()
                            LIMIT 10";
                  

                  我被告知 RAND() 不是很合适,因为它处理大表的速度很慢,但是在这个项目结束之前我的表不会增加到超过 20000.我还有一个随机变量被传递到 URL,如clientdataupdates.php?nocachepls=1541231".

                  I have been informed that RAND() is not very suitable due to it's slow processing of large tables, but my tables will not increase to over 20000 before the end of this project. I also have a random variable being passed to the URL like "clientdataupdates.php?nocachepls=1541231".

                  所以这是我的问题:在当前的 5000 条奇数记录中,如果该脚本同时在多个浏览器窗口中运行,它们会从 MySQL 返回相同的记录.诚然,所选择的记录似乎是随机选取的,但如果查询在完全相同的时间运行,则会在所有窗口中返回相同的记录.

                  So here is my problem: Out of the current 5000 odd records, if this script is run in multiple browser windows at the same time, they are getting the same records returned from MySQL. Admittedly the chosen record seems to be picked at random, but the same record is returned in all of the windows if the query is run at the exact same time.

                  我的研究受到了很大的限制,因为我一直在搜索的关键字(现在已经几天了)似乎与其他问题有关,例如php mysql 在使用 rand() 时返回相同的结果"有太多谷歌响应指向一般使用 rand().

                  My research has been quite limited by the fact that they keywords I have been searching for (over a few days now) seem to relate to other problems e.g. "php mysql returning same result while using rand()" has too many google responses that point to using rand() in general.

                  虽然我仍然希望得到任何帮助,但实际上我更想知道为什么会这样.我对 MySQL 内部工作原理的了解有限,但就我连接 PHP 和 MySQL 的所有经验而言,我也没有看到任何类似的情况发生.

                  Whilst I would still appreciate any assistance, I would actually more like to know why this is happening. My knowledge of the inner workings of MySQL is limited, but for all my experience interfacing PHP and MySQL I have not seen anything similar occur either.

                  更新:

                  我还使用包含回调函数的 ajax 函数进行了测试,以再次启动它.每次div内容都是相同的记录——但看起来还是随机选择了哪条记录.

                  I have also tested using an ajax function that includes a callback function to kick it off again. Every time the div contents are the same record - but it still looks like which record is selected at random.

                  <div id='worker1' class='workerDiv'>worker: waiting..</div>
                  <div id='worker2' class='workerDiv'>worker: waiting..</div>
                  <div id='worker3' class='workerDiv'>worker: waiting..</div>
                  <div id='worker4' class='workerDiv'>worker: waiting..</div>
                  <div id='worker5' class='workerDiv'>worker: waiting..</div>
                  <script>
                   function nextWorker(thisWorker){
                    setTimeout(function(){ ajaxpage('customerdata_worker.php',thisWorker,nextWorker(thisWorker)); }, 10000);
                   }
                   setTimeout(nextWorker('worker1'), 100);
                   setTimeout(nextWorker('worker2'), 100);
                   setTimeout(nextWorker('worker3'), 100);
                   setTimeout(nextWorker('worker4'), 100);
                   setTimeout(nextWorker('worker5'), 100);
                  </script>
                  

                  推荐答案

                  您可能在某些结果集中从 MySQL 查询缓存接收信息.

                  You are probably receiving information from the MySQL query cache in some result sets.

                  试试这个:

                  SELECT SQL_NO_CACHE *
                         /* etc */
                  

                  注意:将 SQL_NO_CACHE 字与 SELECT 和 *(或您选择的第一列的名称)放在同一行.

                  BEWARE: Put the SQL_NO_CACHE word on the same line as the SELECT and the * (or the name of the first column you are selecting).

                  参见:http://dev.mysql.com/doc/refman/5.1/en/query-cache.html 它说,

                  查询缓存将 SELECT 语句的文本与发送给客户端的相应结果.如果一个相同的稍后收到语句,服务器从查询缓存,而不是再次解析和执行语句.这查询缓存在会话之间共享,因此由一个生成的结果集可以发送客户端以响应另一个发出的相同查询客户.

                  The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.

                  专业提示:避免在软件中使用 SELECT *.给出结果集中所需列的名称.

                  Pro tip: Avoid SELECT * in software. Give the names of the columns you need in the result set.

                  这篇关于为什么 MySQL 在 SELECT 语句中使用 RAND() 时返回相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:mysqli 代码和 call_user_func_array() 中出现的错误 下一篇:Mysqli Prepare 语句 + 绑定顺序 BY

                  相关文章

                  最新文章

                  <i id='NPe6a'><tr id='NPe6a'><dt id='NPe6a'><q id='NPe6a'><span id='NPe6a'><b id='NPe6a'><form id='NPe6a'><ins id='NPe6a'></ins><ul id='NPe6a'></ul><sub id='NPe6a'></sub></form><legend id='NPe6a'></legend><bdo id='NPe6a'><pre id='NPe6a'><center id='NPe6a'></center></pre></bdo></b><th id='NPe6a'></th></span></q></dt></tr></i><div id='NPe6a'><tfoot id='NPe6a'></tfoot><dl id='NPe6a'><fieldset id='NPe6a'></fieldset></dl></div>
                • <small id='NPe6a'></small><noframes id='NPe6a'>

                  <legend id='NPe6a'><style id='NPe6a'><dir id='NPe6a'><q id='NPe6a'></q></dir></style></legend>

                    <tfoot id='NPe6a'></tfoot>
                    • <bdo id='NPe6a'></bdo><ul id='NPe6a'></ul>