i have the following table with its fields, this table has more than 30K user's,
EACH USE'r has more than 1000 records,
userid is named as ANONID , i want to select randomly 100 user with all their records,using MYSQL
thanks in advance
"rand()" function as mentioned in earlier answers do not work in SQL 2k12 . for SQL use following query to get random 100 rows using "newid()" function
("newid()" is built in function for SQL)
select * from table
order by newid()
offset 0 rows
fetch next 100 rows only
For a table of 30,000 and a single sample, you can use:
select t.*
from t
order by rand()
limit 100;
This does exactly what you want. It will take a few seconds to return.
If performance is an issue, there are other more complicated methods for sampling the data. A simple method reduces the number of rows before the order by. So a 5% sample will speed the query and here is one method for doing that:
select t.*
from t
where rand() < 0.05
order by rand()
limit 100;
EDIT:
You want what is called a clustered sample or hierarchical sample. Use a subquery:
select t.*
from t join
(select userid
from (select distinct userid from t) t
order by rand()
limit 100
) tt
on t.userid = tt.userid;
SELECT * FROM table
ORDER BY RAND()
LIMIT 100
It is slow, but it works.
Related
I have next mysql query
SELECT DISTINCT * FROM dom_small WHERE count>=0 AND dom NOT IN
(SELECT dom FROM dom_small WHERE was_blocked=1)
ORDER BY count DESC LIMIT 30 OFFSET 4702020
When i increase OFFSET more and more, subquery run long and long.
When OFFSET 0 mysql query load 0 sec but when 4702020 mysql query load 1 min 19,49 sec
How to solve this problem?
You can yield same result with out using subquery
SELECT DISTINCT * FROM dom_small WHERE count>=0 AND was_blocked=1
ORDER BY count DESC LIMIT 30 OFFSET 4702020;
Use following query:
SELECT DISTINCT * FROM dom_small WHERE count>=0 AND dom NOT IN
(select * from(SELECT dom FROM dom_small WHERE was_blocked=1) t1 )
ORDER BY count DESC LIMIT 30 OFFSET 4702020
It can speed up the performance by caching the sub-query result. I previously used this method and it helped me much.
But as others mentioned using offset with big numbers slow down the performance.
SELECT DISTINCT * FROM dom_small WHERE count>=0 AND dom NOT IN
(SELECT dom FROM dom_small WHERE was_blocked=1)
ORDER BY count DESC LIMIT 30 OFFSET 4702020
Although the other comments are accurate, the only suggestion I can offer is that your distinct and subquery are from the same table "dom_small". Also, you are not doing any aggregate count(*) vs what appears to be an actual column in your table called count.
That said, I would have an index to help optimize the query on
( dom, was_blocked, count )
I have MySQL query, that returns set of rows. What I need is to get them in random order, each time that the query is executed.
For example, I have query
SELECT id,id_banner,name FROM module_banner
And it returns me 3 rows with ids - 1,2,3
I want to get them in random order - 3,2,1 2,3,1 1,3,2 and so on.
Let me know if the question is not clear
P.S.
Is there solution without RANDOM() function ?
You want to use ORDER BY RAND():
SELECT id,id_banner,name FROM module_banner ORDER BY RAND()
Just add ORDER BY RAND() at the end of your query.
Query in-general will be
SELECT field1, field2, ... , field(n) FROM TableName ORDER BY RAND()
In your case it would be
SELECT id, id_banner, name FROM module_banner ORDER BY RAND()
Update 1
While searching I found one article & You should read this article : "Do not use ORDER BY RAND() or How to get random rows from table?"
SELECT id,id_banner,name
FROM module_banner JOIN
(SELECT CEIL(RAND() *
(SELECT MAX(id)
FROM module_banner)) AS id
) AS r2
USING (id);
Reference
i was wondering if there was an easy way with just an sql statement to return the last three results in the table but in that order i.e. if there are a hundered results it would return in the order of 98, 99, 100 not simply ordering by id DESC and limit 3 which would return in order 100, 99, 98
Any help much appreciated.
p.s. in this instance, lets say I don't know the amount of results and don't really want to send 2 sql requests just to find the amount ( for any OFFSET answers ).
One way would be to use DESC and then just sort them again:
SELECT * FROM (SELECT * FROM some_table ORDER BY id DESC LIMIT 3) a ORDER BY id
Two options, I guess.
You could use DESC to return them in reverse order as you stated, and just reverse the order again in your application code. This is potentially the most efficient, as you can do it in a single query and it can potentially only need to read three rows of an index.
You can first find out the number of results in the table, then do a LIMIT <results-3>, 3
Is it okay if you just flip it back to the original order?
SELECT * FROM (SELECT * FROM SOMETABLE ORDER BY ID DESC LIMIT 3) AS T ORDER BY ID;
Select *
FROM (SELECT * FROM yourTABLE ORDER BY ID DESC LIMIT 0,3) as TempTable ORDER BY ID
If SELECT SUM(amount) FROM transactions ORDER BY order LIMIT 0, 50 sums the amount field for the first 50 records in a table, how do a sum all records after the first 50? In other words, I'd like to do something like SELECT SUM(amount) from transactions ORDER BY order LIMIT 50, *, but that doesn't work.
SELECT SUM(amount)
FROM (
SELECT amount
FROM transactions
ORDER BY
order
LIMIT 50, 1000000000000
) q
Note that your original query:
SELECT SUM(amount)
FROM transactions
ORDER BY
order
LIMIT 0, 50
does not do what you probably think it does. It is synonymous to this:
SELECT a_sum, order
FROM (
SELECT SUM(amount) AS a_sum, order
FROM transactions
) q
ORDER BY
order
LIMIT 0, 50
The inner query (which would normally fail in any other engine but works in MySQL due to its GROUP BY extension syntax) returns only 1 records.
ORDER BY and LIMIT are then applied to that one aggregated record, not to the records of transactions.
The documentation advices to use an incredible large number as second parameter to LIMIT:
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
There is probably a more efficient way, but you could run a count query first, to retrieve total # of rows in your table:
SELECT count(*) FROM transactions
Stuff that into a variable and use that variable as your second argument for LIMIT. You could probably do this as a nested mysql query.
how to form a query to select 'm' rows randomly from a query result which has 'n' rows.
for ex; 5 rows from a query result which has 50 rows
i try like as follows but it errors
select * from (select * from emp where alphabet='A' order by sal desc) order by rand() limit 5;
u can wonder that why he needs sub query, i need 5 different names from a set of top 50 resulted by inner query.
SELECT * FROM t
ORDER BY RAND() LIMIT 5
or from your query result:
SELECT * FROM ( SELECT * FROM t WHERE x=y ) tt
ORDER BY RAND() LIMIT 5
This will give you the number to use as 'm' (limit)
TRUNCATE((RAND()*50),0);
...substitute 50 with n.
To check it try the following:
SELECT TRUNCATE((RAND()*50),0);
I should warn that this could return 0 as a result, is this ok for you?
For example you could do something like this:
SELECT COUNT(*) FROM YOUR_TABLE
...and store the result in a variable named totalRows for example. Then you could do:
SELECT * FROM YOUR_TABLE LIMIT TRUNCATE((RAND()*?),0);
where you substitute the '?' with the totalRows variable, according to the tech stack you are using.
Is it clearer now? If not please add more information to your question.