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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:59:07+00:00 2026-05-27T01:59:07+00:00

What would be the most efficient way to select a non previously selected pair

  • 0

What would be the most efficient way to select a non previously selected pair of different random values from a column of unique (non repeated) values?

My current approach is (keeping every pair of values already associated in a separate “mytable_associations” table):

SELECT * FROM
(
 SELECT id,count(*) AS associations_count FROM mytable 
 INNER JOIN mytable_associations 
 WHERE (myvalue=myvalue1 OR myvalue=myvalue2) 
 GROUP BY myvalue 
 HAVING associations_count<(SELECT count(*) FROM mytable)-1
 ORDER BY rand() limit 1
) mytable1 
LEFT JOIN 
(SELECT myvalue AS myvalue2 FROM mytable) mytable2
ON mytable1.myvalue1<>mytable2.myvalue2
WHERE
(
 SELECT myvalue1 FROM mytable_associations 
 WHERE
 myvalue1=mytable1.myvalue1 AND myvalue2=mytable2.myvalue2
 OR
 myvalue1=mytable2.myvalue2 AND myvalue2=mytable1.myvalue1
) IS NULL;

(And then of course update mytable_associations with this new association)

Which, as you can see, could hugely benefit from some optimization.

(Sorry about the poor indentation in the code, I really don’t know how to indent mysql commands).

Can you help me?

(P.S. This is my first question ever posted here: Sure I’m doing lots of things wrong and I’d understand the consequent flaming, but please don’t be too hard on me 😉 )

  • 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-27T01:59:08+00:00Added an answer on May 27, 2026 at 1:59 am

    Any solution involving order by rand() is going to be inefficient. For alternatives, see:

    • My answer to Selecting random rows with MySQL
    • Jan Kneschke on Order by RAND()

    To exclude numbers you’ve already picked, here’s how I’d do it (this is pseudocode):

    $c1 = SELECT COUNT(DISTINCT myvalue) FROM mytable
    $c2 = SELECT COUNT(*) FROM mytable_associations
    
    $offset = ROUND( RAND() * ($c1 * ($c1-1) - $c2) )
    
    SELECT v.* FROM (
      SELECT LEAST(m1.myvalue,my2.myvalue) AS myvalue1,
        GREATEST(m1.myvalue,my2.myvalue) AS myvalue2
      FROM (SELECT DISTINCT myvalue FROM mytable) AS m1 
      INNER JOIN (SELECT DISTINCT myvalue FROM mytable) AS m2
        ON m1.myvalue <> m2.myvalue
    ) AS v
    LEFT OUTER JOIN mytable_associations AS a USING (myvalue1,myvalue2)
    WHERE a.myvalue1 IS NULL
    LIMIT 1 OFFSET $offset
    

    By ensuring that myvalue1 < myvalue2, and storing them in that order in mytable_associations, you can simplify the join.

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

Sidebar

Related Questions

What would be the most efficient way of recording to a log (.txt) from
What would be the most efficient way of counting the number of times a
If I have two associative arrays, what would be the most efficient way of
In SQL Server would a varbinary(16) be the most efficient way of storing an
As the title says, what's the most efficient way to get a random selection
What would be the most efficient data type to store a UUID/GUID in databases
What would be the most efficient method of reading a text file into a
I'm trying to figure out what would be the most efficient (with respect to
Given a URL, what would be the most efficient code to download the contents
What are the most efficient SQL queries that would take an existing MySQL table

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.