This question already has answers here:
MySQL select 10 random rows from 600K rows fast
(28 answers)
Closed 9 years ago.
Is it true that ORDER BY rand() performance is very slow compared to other solutions? If yes, what are better ways to select random row(s) from the database?
My query:
SELECT sName FROM bpoint WHERE placeID=? ORDER BY rand() LIMIT 1;
Yes, ORDER BY RAND() can be very slow in larger result-sets.
An option is to fetch resultset with this statement (into an array):
SELECT sName FROM bpoint WHERE placeID=?;
After that - use array_rand($resultset) to get a randomized item from the $resultset query.
Related
This question already has answers here:
Select last N rows from MySQL
(6 answers)
Closed 6 years ago.
SELECT FROM users.id,username ORDER BY ID ASC LIMIT 3
Not working in output,
this line not error showing
how to correct line ?
it should be
SELECT id, username FROM users ORDER BY ID ASC LIMIT 3
You would be better served writing the following...
SELECT id FROM users ORDER BY id ASC LIMIT 3
Your original query is wrong, SQL, like any programming language, needs to be written EXACTLY as intended.
Please do an introductory course on SQL.
This question already has answers here:
MySQL: Fastest way to count number of rows
(14 answers)
Closed 8 years ago.
I want to count all of rows in table that match to my condition as fast as mysql can do.
So, i have four SQL and want you to explain all of them, how is it different for each SQL?
and which is fastest or best for query times and server performance? or it has another way that can better than these. Thank you.
select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition
or
select * from table where my_condition
//and then use mysql_num_rows()
First of all don't even think about doing the last one! It literally means select all the data I have in the database, return it to the client and the client will count them!
Second, you need to understand that if you're counting by column name, count will not return null values, for example: select count(column1) from table_name; if there is a row where column1 is null, it will not count it, so be careful.
select count(*), select count(1), select count(primary_key) are all equal and you'll see no difference whatsoever.
This question already has answers here:
How to request a random row in SQL?
(30 answers)
Closed 8 years ago.
I have names in my database and I want to draw a name for a contest.
Anyone have an idea for that ?
Thanks !!
SELECT * FROM table WHERE num_value >= RAND() * (SELECT MAX(num_value) FROM table) LIMIT 1
This works in constant time, regardless of the table size, if num_value is indexed. One caveat: this assumes that num_value is equally distributed in the range 0..MAX(num_value). If your dataset strongly deviates from this assumption, you will get skewed results (some rows will appear more often than others).
A query like this could work
SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Selecting Random Rows in MySQL
I'm creating a simple web application using PHP and MySQL. In it, I need to randomly select a small set of rows from a table in a random order. How can I achieve such thing using MySQL?
SELECT * FROM table ORDER BY RAND() LIMIT 10;
Edit:
Useful information about the MySQL RAND() function can be found here.
select * from table order by rand() limit 10
Note that order by rand() with large dataset is very slow but in your case it's not a problem.
you could do that using RAND() function .
SELECT questine FROM tablename ORDER BY RAND() LIMIT 10
will select 10 questines at random under assumption the questine is stored under field questine
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
MySQL select 10 random rows from 600K rows fast
I need to pull from a table a random id. At this moment only a few records exists but with time they will grow. What methods of getting this id are in Php or MySql, and what are the trade offs, consequences between them. One last thing i need speed and performance.
select * from YOUR_TABLE order by rand() limit 1
You can achieve this direct in your SQL:
SELECT `idfield` FROM `table` ORDER BY RAND() LIMIT 0,1;
Also see here for some alternatives.
This will be a simple means of execution than building the randomisation in your PHP and then passing to mySQL, though the link above details the merits of the various approaches.
SELECT id FROM table ORDER BY RAND() LIMIT 1
What this does is basically: take the table, order the records by a random number, and get the first record's ID.
There is already a lot of questions and answer about that:
Random Records Mysql PHP
mysql query with random and desc
If you have less that 500,000 records, the RAND() is perfectly fine.
SELECT * FROM tbl_name ORDER BY RAND() LIMIT 1
Well you can obtain a random number with RAND(), but you would still have to call that method/function until you actually get something (you can have 1,2,5,6,100 as id`s due to deletion).
there is a post on this http://jan.kneschke.de/projects/mysql/order-by-rand/
Procedures can be usefull (but said to be slow...):
1 var to get the maximum value
then produce a random within 1 and that interval (or get the minimum)
then query