How to quickly SELECT 3 random records from a 30k MySQL table with a where filter by a single query?

后端 未结 6 1890
生来不讨喜
生来不讨喜 2020-12-14 02:38

Well, this is a very old question never gotten real solution. We want 3 random rows from a table with about 30k records. The table is not so big in point of view MySQL, but

6条回答
  •  半阙折子戏
    2020-12-14 03:13

    What about creating another table containing only items with image ? This table will be much lighter as it will contain only one-third of the items the original table has !

    ------------------------------------------
    |ID     | Item ID (on the original table)|
    ------------------------------------------
    |0      | 0                              |
    ------------------------------------------
    |1      | 123                            |
    ------------------------------------------
                .
                .
                .
    ------------------------------------------
    |10 000 | 30 000                         |
    ------------------------------------------
    

    You can then generate three random IDs in the PHP part of the code and just fetch'em the from the database.

提交回复
热议问题