Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3336212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:06:31+00:00 2026-05-18T00:06:31+00:00

I have a huge table in my database (MySQL)with millions of data. I need

  • 0

I have a huge table in my database (MySQL)with millions of data. I need to populate 10 random data and show it on the UI. What would be a good approach considering performance?

I was thinking about creating MySQL View to populate 10 random rows and read it from UI. Or is there any other efficient way to handle this situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:06:31+00:00Added an answer on May 18, 2026 at 12:06 am

    First of all (it’s my proper opinion) i’m against using raw SQL when we are already working with a hight level framework like Django unless we don’t find what we are looking for in the framework (Django) , so i will rather use Django for this matter:

    remark this approach it’s only working if you have set auto increment to you PK and of course if you data is consistent (you don’t remove record from the table so that you can be sure that all ids are auto incremented)

     import random
    
     # Getting the number of rows in the table it's equivalent to do SELECT COUNT(*).
     count_record = Table.objects.count() 
    
     # Choose 10 (at most) number from the list of all ids. 
     random_pks = random.sample(range(1, count_record) , min(count_record, 10))
    
     random_list = Table.objects.filter(pk__in=random_pks)
    

    if the condition sited before are not satisfied i think you can do it with an raw SQL query like this:

    query = """SELECT * FROM table
    ORDER BY RAND()
    LIMIT 10""")
    
    table.objects.raw(query)
    

    about performance i think you have to timeit, Hope this will help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database with a few (five to be precise) huge tables.
I have a database containing a single huge table. At the moment a query
I have a huge table from one mysql db, I want to create a
I have a huge MySQL table which has its rows encoded in UTF-8 twice.
I have a huge data.table in R which contains the result of a experiment:
I have a scripts that retrieves huge data on a table. I want to
I have one table spread across two servers running MySql 4. I need to
I have an MEMORY MYSQL database that contains a table with 200k+ rows. I
I have this big database on mysql (104 tables), where almost every single table
I have a table in a MySQL database with an ID column. This is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.