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);
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
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..
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)