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 8603865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:25:31+00:00 2026-06-12T02:25:31+00:00

My objective I am trying to retrieve multiple random rows that contain only unique

  • 0

My objective

I am trying to retrieve multiple random rows that contain only unique userid but for the type column to be random – type can only be 0 or 1. The table in question will contain less than 1,000 rows at any given time.

My table

CREATE TABLE tbl_message_queue (
  userid bigint(20) NOT NULL,
  messageid varchar(20) NOT NULL,
  `type` int(1) NOT NULL,
  PRIMARY KEY (userid,messageid,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Sample data

userid  |   messageid   |   type
---------------------------------------------------
4353453 |   518423942   |   0
4353453 |   518423942   |   1
2342934 |   748475435   |   0
2342934 |   748475435   |   1
7657529 |   821516543   |   0
7657529 |   821516543   |   1
0823546 |   932843285   |   0
0823546 |   932843285   |   1

What to rule out

Using ORDER BY RAND() isn’t feasible as over at least 18,000 of these types of queries are executed by applications at any given moment and are causing high load. Using SELECT DISTINCT or GROUP BY is (obviously) more efficient and will always pick unique userid but type will always equal to 0 with an acceptable load.

The common method is to create an id column but I’m looking for an alternative way only. The group primary key cannot change as it is required and deeply integrated into our application, however the structure of each column can be altered.

Thanks.

  • 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-12T02:25:32+00:00Added an answer on June 12, 2026 at 2:25 am

    My understanding of your question is that for each userid you have two entries, but want to extract only one, at random.

    To achieve this, you ought to generate a random value between 0 and 1 for each unique userid, and then JOIN this list with the starting list:

    SELECT a.* FROM tbl_message_queue AS a
        JOIN ( SELECT userid, FLOOR(2*RAND()) AS type 
               FROM tbl_message_queue GROUP BY userid ) AS b
        ON ( a.userid = b.userid AND a.type = b.type );
    

    But if an ORDER BY RAND() does not work for you, maybe we should compromise.

    In the above sequence, any two userids will be uncorrelated — i.e., the fact that user A gets type 0 tells you nothing about what user B will turn up with.

    Depending on the use case, a less random (but “apparently random”) sequence could be obtained with two queries:

    SELECT @X := FLOOR(2*RAND()), @Y := POW(2,FLOOR(2+14*RAND()))-1;
    
    SELECT * FROM tbl_message_queue WHERE (((userid % @Y) & 1) XOR type XOR @X);
    

    This way, you can get what seems a random extraction. What really happens is that the userids are correlated, and you only have some couple dozens different extractions possible. But using only simple operators, and no JOINs, this query is very fast.

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

Sidebar

Related Questions

I'm trying to use Objective-C 2.0 feature Property in GNUstep(using Windows). But i can't
I am trying to use Objective-C blocks in an iPhone 3.1.3 app but am
I am trying to create an objective c interface that encapsulates the functionality of
I'm trying to use an Objective-C class made in Python to do this but
I'm learning Objective-C and trying to develop a simple zipper application, but I stopped
I'm trying to compile the following Objective-C program on Ubuntu Hardy, but for some
I'm trying to learn Objective-C. I almost finished one excercise but it is not
I am using Objective-C and trying to create int values that are between 1-99
I'm trying to learn Objective-C and iPhone programming, but I'm stuck with a problem.
I am trying to save and retrieve data within multiple views across my application,

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.