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

  • SEARCH
  • Home
  • 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 9131213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:06:25+00:00 2026-06-17T08:06:25+00:00

There is a table that contains such columns: id, person_id, pet_id, description There are

  • 0

There is a table that contains such columns:

id, person_id, pet_id, description

There are some statements about this table:

  1. id is a primary key, autoincrement
  2. Each pair (person_id, pet_id) is unique
  3. id, person_id, pet_id is integer and is NOT NULL
  4. “holes” are possible (max pet_id doesn’t tell us there are totally (max) number of columns with such person_id)
  5. It is very possible there are much more different person_id in the table, that average number of pet_id for each person.

The question is: How to select random N pet_id of the current person_id fast?

Example of the table:

1.  1   1     cat
2.  1   2     dog
3.  2   40    horse
4.  2   35    dog
5.  3   46    duck
6.  2   39    duck
7.  1   3     duck
..................
100000  403  12  monkey

Example: I want to select two RANDOM rows for the person number two. One of possible random choices is the row #3. and the row #6.
The choices should be really “random” (should appear with the same probability).

How to do it using mysql query SELECT?

P.S. Of course I’ve read about selecting several random rows from the table, the basic problem with some tricky solutions. But, in my case there are two rows, not one.

I’m thinking of a faster method than

select id from tablename where person_id = 2 order by random()  limit 2;
  • 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-06-17T08:06:26+00:00Added an answer on June 17, 2026 at 8:06 am

    Your strawman query is as good as you can do. If you don’t have a person with lots of pets (and you have an index on person_id), it should run quite fast. If you do have such a person, then you’re out of luck. Forget selecting randomly, even to determine how many pets such a person has requires time O(# pets).

    One possible other idea which probably won’t work for you: If you don’t care about the independence of your selections (i.e., you might get the same random response every time), then you can add a column which you populate with a random number when the row is inserted. Add an index on person_id,random_column and select the first N rows ordered by that pair. Slightly better is to add multiple random columns and select one to order by at random. Unfortunately, this doesn’t scale well and I don’t think you’d be happy with the results.

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

Sidebar

Related Questions

I have a table that contains an Id column as a primary key. There
I have a table that contains id, created, user_id. I'm wondering if there is
There are some section in the table that does not contain any data and
Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have a WARNINGS_INFO table that contains some info on a specific warning message,
My Datatable has some columns that contains data in the format as 2011~|~2012~|~2013. Now
I have a table column that contains values such as abc_1_2_3_4.gif or zzz_12_3_3_45.gif etc.
With this schema: create table object ( obj_id serial primary key, name varchar(80) not
I have a table that I created within xcode so there is no nib
Scaffolding works like magic. However, there are fields in my table that does necessarily

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.