Why it takes more time for MySQL to get records starting with higher number? [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I speed up a MySQL query with a large offset in the LIMIT clause?
In our application we are showing records from MySQL on a web page. Like in most such applications we use paging. So the query looks like this:
select * from sms_message
where account_group_name = 'scott'
and received_on > '2012-10-11' and
received_on < '2012-11-30'
order by received_on desc
limit 200 offset 3000000;
This query takes 104 seconds. If I only change offset to low one or remove it completely, it's only half a second. Why is that?
There is only one compound index, and it's account_group_name, received_on and two other columns. Table is InnoDB.
UPDATE:
Explain returns:
1 SIMPLE sms_message ref all_filters_index all_filters_index 258 const 2190030 Using where
all_filters_index is 4-columns filter mentioned above.

Yes this is true, time increases as offset value increases and the reason is because offset works on the physical position of rows in the table which is not indexed. So to find a row at offset x, the database engine must iterate through all the rows from 0 to x.

Related

Select a top 10 list by number of times that person was entered [duplicate]

This question already has an answer here:
SQL counting all rows instead of counting individual rows
(1 answer)
Closed 2 years ago.
I have a table that keeps track of how many times a user accomplishes something.
I want a query that can help me create a top 10 list to display the leaders.
Anon - 10
Anon2 - 7
Anon 3 -3
My query which currently selects the entire count of something and only 1 user returns the following:
Anon - 20
Below is the query:
Select count(referral_person) AS C, referral_person
from item_referral
ORDER by count(referral_person) DESC;
What am i doing wrong?
Your query is missing a group by clause - but it has an aggregate function in the select clause. So MySQL gives you just one row, with the overall count of rows in the table and an arbitrary value of referral_person.
In most other databases, this would generate a syntax error - and in MySQL too, if sql mode ONLY_FULL_GROUP_BY was enabled - which would make it much easier for you to spot the problem.
Also, to select the top 10, you can use order by and limit.
It seems like you want:
select referral_person, count(*) cnt
from item_referral
group by referral_person
order by cnt desc
limit 10

How can I get limited records from database using spring JPA?

I want to fetch records in batches from MySQL database. Initially I fetch records of 1st 10 rows, then next 10 rows, until I found every matched row.
I found one solution of limiting query, but It didn’t help me.
Pageable p = PageRequest.of(offset, limit)
Here, if the offset is 0, and the limit is 10 then I got 10 records correctly. But, then if I applied value for offset other than 0, no rows were returned.
How can I use offset here? Or is there any other solution for this query? Please suggest if any.
public static PageRequest of(int page, int size)
Parameters:
page - zero-based page index, must not be negative.
size - the size of the page to be returned, must be greater than 0.
Here the first parameter is index of page not offset and the second parameter is size of the page not limit. Ref
Example: Suppose you have 30 documents. If you use page size as 10 then there will be 3 page total.
PageRequest.of(0, 10) -> offset 0 limit 10 -> will fetch first 10 document.
PageRequest.of(1, 10) -> offset 10 limit 10 -> will fetch 10 document after first 10 document.
PageRequest.of(2, 10) -> offset 20 limit 10 -> will fetch 10 document after first 20 document.

Is there a way to be less random than ORDER BY RAND to increase speed?

I run this query 5 times, 5 seconds apart, on a table of 500,000 rows:
SELECT * FROM `apps` WHERE dev_name = '' ORDER BY RAND() LIMIT 10;
I'd like to get 50 rows that have a 90-95% chance of being unique. The query takes 10 seconds right now. I'd rather get it lower and have a smaller chance of being random.
Try
AND RAND() >= 0.90
(or 0.95 if you like) in your WHERE clause.
You may want to study Fetching Random Rows from a Table, as highlighted in here in a related question. Another great article can be found here.

How to write search(list) queries in Mysql

There is a search page in webapplication(Pagination is used : 10 records per page). Database used : Mysql. Table has around 1000 00records.Query is tuned as in query is using index (checked Explain plan).Result set that fetches around 17000 rows and takes around 5 sec .Can any please suggest how to optimize search Query.(Note : Tried to use limit but query time did not improve).
Query Eg:
Select * from abc
Join def on abc.id=def.id
where date >= '2013-09-03'
and date <='2014-10-01'
and def.state=1
-- id on both table is indexed
-- date and state column cannot be indexed as they have low SI.

mysql pdo get range of rows

i have a table with about 100 rows , and i want every time to get rows between number and number , like this
if `i=1` i want to get the rows `0 1 2 3 4`
if `i=2` i want to get the rows `5 6 7 8 9`
if `i=3` i want to get the rows `10 11 12 13 14`
and maybe the last value of i will just take 3 or 4 rows not 5
i think the solution will be something like this
select * from question limit (i-1)*5 , i*5-1
but doesn't work , cos i don't know how to use variable in a query , and when i tried it for i=1 it doesn't work and i got a syntax error
The first parameter to LIMIT is the zero-based starting row index. The second one is the number of rows. So, if you're always looking for five rows:
SELECT * FROM question LIMIT (i-1)*5 , 5
(You'll have to calculate (i-1)*5 with whatever language you're using to build the query, and pass an actual number to MySQL).