<bdo id='OYdg3'></bdo><ul id='OYdg3'></ul>

  • <legend id='OYdg3'><style id='OYdg3'><dir id='OYdg3'><q id='OYdg3'></q></dir></style></legend>

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

      <tfoot id='OYdg3'></tfoot>

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

        如何优化 MySQL 的 ORDER BY RAND() 函数?

        时间:2023-08-20

        <tfoot id='ca9qL'></tfoot>

        <small id='ca9qL'></small><noframes id='ca9qL'>

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

            <tbody id='ca9qL'></tbody>

        1. <i id='ca9qL'><tr id='ca9qL'><dt id='ca9qL'><q id='ca9qL'><span id='ca9qL'><b id='ca9qL'><form id='ca9qL'><ins id='ca9qL'></ins><ul id='ca9qL'></ul><sub id='ca9qL'></sub></form><legend id='ca9qL'></legend><bdo id='ca9qL'><pre id='ca9qL'><center id='ca9qL'></center></pre></bdo></b><th id='ca9qL'></th></span></q></dt></tr></i><div id='ca9qL'><tfoot id='ca9qL'></tfoot><dl id='ca9qL'><fieldset id='ca9qL'></fieldset></dl></div>
            <bdo id='ca9qL'></bdo><ul id='ca9qL'></ul>
                1. 本文介绍了如何优化 MySQL 的 ORDER BY RAND() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想优化我的查询,所以我查看了 mysql-slow.log.

                  I'd like to optimize my queries so I look into mysql-slow.log.

                  我的大多数慢查询都包含 ORDER BY RAND().我找不到解决此问题的真正解决方案.MySQLPerformanceBlog<有一个可能的解决方案/a> 但我认为这还不够.在优化不佳(或经常更新、用户管理)的表上,它不起作用,或者我需要运行两个或多个查询才能选择我的 PHP 生成的随机行.

                  Most of my slow queries contains ORDER BY RAND(). I cannot find a real solution to resolve this problem. Theres is a possible solution at MySQLPerformanceBlog but I don't think this is enough. On poorly optimized (or frequently updated, user managed) tables it doesn't work or I need to run two or more queries before I can select my PHP-generated random row.

                  这个问题有什么解决办法吗?

                  Is there any solution for this issue?

                  一个虚拟的例子:

                  SELECT  accomodation.ac_id,
                          accomodation.ac_status,
                          accomodation.ac_name,
                          accomodation.ac_status,
                          accomodation.ac_images
                  FROM    accomodation, accomodation_category
                  WHERE   accomodation.ac_status != 'draft'
                          AND accomodation.ac_category = accomodation_category.acat_id
                          AND accomodation_category.acat_slug != 'vendeglatohely'
                          AND ac_images != 'b:0;'
                  ORDER BY
                          RAND()
                  LIMIT 1
                  

                  推荐答案

                  试试这个:

                  SELECT  *
                  FROM    (
                          SELECT  @cnt := COUNT(*) + 1,
                                  @lim := 10
                          FROM    t_random
                          ) vars
                  STRAIGHT_JOIN
                          (
                          SELECT  r.*,
                                  @lim := @lim - 1
                          FROM    t_random r
                          WHERE   (@cnt := @cnt - 1)
                                  AND RAND(20090301) < @lim / @cnt
                          ) i
                  

                  这在 MyISAM 上特别有效(因为 COUNT(*) 是即时的),但即使在 InnoDB 中它也是 10ORDER BY RAND() 效率高几倍.

                  This is especially efficient on MyISAM (since the COUNT(*) is instant), but even in InnoDB it's 10 times more efficient than ORDER BY RAND().

                  这里的主要思想是我们不排序,而是保留两个变量并计算当前步骤要选择的行的运行概率.

                  The main idea here is that we don't sort, but instead keep two variables and calculate the running probability of a row to be selected on the current step.

                  有关详细信息,请参阅我博客中的这篇文章:

                  See this article in my blog for more detail:

                  • 选择随机行

                  更新:

                  如果你只需要选择一个随机记录,试试这个:

                  If you need to select but a single random record, try this:

                  SELECT  aco.*
                  FROM    (
                          SELECT  minid + FLOOR((maxid - minid) * RAND()) AS randid
                          FROM    (
                                  SELECT  MAX(ac_id) AS maxid, MIN(ac_id) AS minid
                                  FROM    accomodation
                                  ) q
                          ) q2
                  JOIN    accomodation aco
                  ON      aco.ac_id =
                          COALESCE
                          (
                          (
                          SELECT  accomodation.ac_id
                          FROM    accomodation
                          WHERE   ac_id > randid
                                  AND ac_status != 'draft'
                                  AND ac_images != 'b:0;'
                                  AND NOT EXISTS
                                  (
                                  SELECT  NULL
                                  FROM    accomodation_category
                                  WHERE   acat_id = ac_category
                                          AND acat_slug = 'vendeglatohely'
                                  )
                          ORDER BY
                                  ac_id
                          LIMIT   1
                          ),
                          (
                          SELECT  accomodation.ac_id
                          FROM    accomodation
                          WHERE   ac_status != 'draft'
                                  AND ac_images != 'b:0;'
                                  AND NOT EXISTS
                                  (
                                  SELECT  NULL
                                  FROM    accomodation_category
                                  WHERE   acat_id = ac_category
                                          AND acat_slug = 'vendeglatohely'
                                  )
                          ORDER BY
                                  ac_id
                          LIMIT   1
                          )
                          )
                  

                  这假设您的 ac_id 的分布或多或少是均匀的.

                  This assumes your ac_id's are distributed more or less evenly.

                  这篇关于如何优化 MySQL 的 ORDER BY RAND() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 MySQL 中选择除一列之外的所有列? 下一篇:MySQL 中的分隔符

                  相关文章

                  最新文章

                  <small id='hFU5f'></small><noframes id='hFU5f'>

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