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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:23:57+00:00 2026-06-10T06:23:57+00:00

I am using SELECT * FROM picture INNER JOIN user ON picture.fbid = user.fbid

  • 0

I am using

SELECT * 
  FROM picture 
 INNER JOIN user ON picture.fbid = user.fbid 
 WHERE hide = 0 
   AND userhide = 0 
 ORDER BY gender, RAND( ) LIMIT 0 , 1

This executes and gives me the link to a picture of a random row where the gender is female. It takes around 15 seconds to execute.

How could I change the SQL so that it is calculated fast? Should I add a gender column in the Picture table so that it doesn’t combine both the table and then gets a random row or is there some other way to optimise the SQL?

  • 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-10T06:23:59+00:00Added an answer on June 10, 2026 at 6:23 am

    [EDIT] You can do the same with a MySQL stored procedure, I just coded it here: http://sqlfiddle.com/#!2/d0e6a/2

    (Note: Not having the count done in every query would aliviate the load, otherwise in this particular case it can be even slower and I would recommend to keep your query, but with index optimization. Or check this out: Is cross-table indexing possible?)

    [EDIT 2] Here’s another example with index optimization using an average of 50000 random entries in each table: http://sqlfiddle.com/#!2/bfbe1/1 The query takes less than 100ms (Without indexes I left it running for several minutes and still no result!). You can create indexes on existing tables using CREATE INDEX

    Here’s my solution using 2 queries from a PHP script:

    $sql="SELECT COUNT(*) 
      FROM picture 
     INNER JOIN user ON picture.fbid = user.fbid 
     WHERE hide = 0 
       AND userhide = 0
       AND gender = 'female' 
       GROUP BY gender";
    
    $result=mysql_query($sql);
    $row=mysql_fetch_array($result);
    
    $sql="SELECT * 
          FROM picture 
          INNER JOIN user ON picture.fbid = user.fbid 
          WHERE hide = 0 
          AND userhide = 0
          AND gender = 'female' 
          LIMIT FLOOR(RAND()*$row['COUNT(*)']),1";
    
    $result=mysql_query($sql);
    $picture=mysql_fetch_array($result);
    

    This should make you query a lot faster.

    Also, if your tables are big (which I guess it’s the case, since the query takes 15 secs) it would help if the conditional/join fields are indexed.

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

Sidebar

Related Questions

I want to join two tables using JPQL : SELECT * FROM A LEFT
When using this SQL query: Select * from table_name what happens if the name
I am using mysql_query(INSERT INTO imaegs_backup SELECT * FROM images); in order to copy
I've got this query: SELECT PICTURE FROM LOGO WHERE LOGONO = ? (PICTURE is
When using the following code to select a picture from my gallery, I also
Just a quick question about using select() . I'm using select() to read from
When using $optionquery = SELECT OptionId FROM Option_Table WHERE (OptionType = ?); // Bind
I am using jdbc to select data from a mysql database. I am performing
I want to make custom select from the database table using Linq. We use
Using phpMyAdmin and running the following query I get 19 registers: SELECT * FROM

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.