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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:52:15+00:00 2026-05-29T10:52:15+00:00

I have a dictionary in my database which has over million records and this

  • 0

I have a dictionary in my database which has over million records and this simple select

select * from Word where languageId = 'en' order by rand() limit 1

randomly selects one word.

The problem is that this request lasts 3-4 seconds which is very long because I have to repeat it many times.

Is there a way to achieve the same thing but much faster?

EDIT – table schema

wordId - integer, auto increment
languageId - varchar (FK), values like cs, en, de, ... 
word - varchar, word itself

Data structure example

wordId   languageId   word
--------------------------
1        cs           abatyše
...
100000   cs           zip
100001   en           aardvark
...
etc

SQL

CREATE TABLE Language (
  languageId VARCHAR(20)  NOT NULL  ,
  name VARCHAR(255)  NULL    ,
PRIMARY KEY(languageId));

CREATE TABLE Word (
  wordId INTEGER UNSIGNED  NOT NULL   AUTO_INCREMENT,
  languageId VARCHAR(20)  NOT NULL  ,
  word VARCHAR(255)  NULL    ,
PRIMARY KEY(wordId)  ,
INDEX Word_FK_Language(languageId),
  FOREIGN KEY(languageId)
    REFERENCES Language(languageId)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION);
  • 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-29T10:52:16+00:00Added an answer on May 29, 2026 at 10:52 am

    If you have an IDs column and the gaps between the elements are not huge (not too many elements were removed, otherwise some elements will be selected more often) then try this query

    SELECT * FROM `table` 
       WHERE id >= 
          (SELECT FLOOR( MAX(id) * RAND()) FROM `table` WHERE languageId = 'en' ) 
       AND languageId = 'en'
       ORDER BY id LIMIT 1;
    

    And look at different examples here
    http://akinas.com/pages/en/blog/mysql_random_row/

    ps: I just realized that it works good only without requirement for the languageId, otherwise the gaps in IDs for the same languageId could be huge.

    Updated Try this one, it could be in a couple of times faster. I checked it against execution time of your query.. twice faster..

    SELECT d.* FROM
      (SELECT @rn:=0 ) r, 
      (SELECT FLOOR(count(*)*RAND()) as rnd FROM `Word` WHERE languageId = 'en') t,  
      (SELECT @rn:=@rn+1 as rn, `Word`.* FROM `Word` WHERE languageId = 'en' ) d 
    WHERE d.rn >= t.rnd LIMIT 1
    

    basically it still creates some kind of continuous ids, but without sorting by them.

    Last Update This one could be even faster (depends on the random number generated)

    SELECT d.* FROM
      ( SELECT @rn:=@rn+1 as rn, w.*, t.rnd rnd FROM 
         (SELECT @rn:=0 ) r, 
         (SELECT FLOOR(count(*)*RAND()) rnd FROM `Word` WHERE languageId = 'en') t, 
         `Word` w 
       WHERE w.languageId = 'en' AND @rn<t.rnd
      ) d 
    WHERE d.rn=d.rnd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have dictionary which I'm mapping using Fluent NHibernate. The dictionary has a complex
This one has me rather confused. I've written a query which runs fine from
Up until now, I have maintained a 'dictionary' table in my database, for example:
I have a database table that is a dictionary of defined terms -- key,
In which I mean rebasing in the dictionary, rather than git definition... I have
I have a Dictionary which is binded to a combobox. I have used dictionary
I have a dictionary of 10000 Product/Colour/Size combinations which I have created with something
I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have a staging database which stores the GEO location as the following structure
Here's the scenario I have an ASP.NET 4.0 application which has a LOT of

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.