Fetching 1000 + records quikly - mysql

Am using java as language,Spring Mvc and Hibernate as Frameworks,mysql as database.
I have 1000 records to show.its taking long time to fetch 1000+ records in single request.so ,i want to fetch 25 records first ,then next 25 records,then next 25 records like that.how to do that?

Use Query with limit option, with pagination concept
for example
select * from table limit 0,25
second 25 result
select * from table limit 25,25
every request calculate (page number * 25);
if page is 20 then 20*25
select * from table limit 500,25

Its working Fine with my little change
For First 25 Result
select * from table limit 0,25
For this query i will Get 1 to 25 Records
second 25 Result
select * from table limit 25,25
For this query i will Get 26 to 50 Records
next 25 Result
for Example if page is 3
as per your calculation (every request calculate (page number * 25);)
(3*25)=75
select * from table limit 75,25
For this query i will Get 75 to 100 Records
But Actucally i need 3rd 25 Records which is 51 to 75
so I Changed calculation like this
every request calculate (page number -1 * 25)
if page is 3 then ((3-1) *25) =(2*25)=50
select * from table limit 50,25
For this query i will Get 51 to 75 Records .which is i Wanted......

Related

MySQL Using LIMIT vs OFFSET [duplicate]

In the query below:
SELECT column
FROM table
LIMIT 18 OFFSET 8
how many results will we get as output and from where to where?
It will return 18 results starting on record #9 and finishing on record #26.
Start by reading the query from offset. First you offset by 8, which means you skip the first 8 results of the query. Then you limit by 18. Which means you consider records 9, 10, 11, 12, 13, 14, 15, 16....24, 25, 26 which are a total of 18 records.
Check this out.
And also the official documentation.
OFFSET is nothing but a keyword to indicate starting cursor in table
SELECT column FROM table LIMIT 18 OFFSET 8 -- fetch 18 records, begin with record 9 (OFFSET 8)
you would get the same result form
SELECT column FROM table LIMIT 8, 18
visual representation (R is one record in the table in some order)
OFFSET LIMIT rest of the table
__||__ _______||_______ __||__
/ \ / \ /
RRRRRRRR RRRRRRRRRRRRRRRRRR RRRR...
\________________/
||
your result
You will get output from column value 9 to 26 as you have mentioned OFFSET as 8
It will skip first 8 records and desplays records from 9 to 26
limit 18 : Display/select 18 records
offset 8 : will skip 8 records
if Your table has ids like 1,2,3,4,5,6,7,8,9,10,11.... and so on , so 1 to 8 records will be skipped and records after 9 to 26 ie 18 records will be displayed/selected.
Offset is majorly used to support pagination in MySql SELECT statements.
First the query will execute and then the records after the offset will be returned.
For example: let's say you want to show 10 reviews per page for a product as per the order of ratings (highest first), then below query can be used to get the reviews which will be shown on third page:
Select * from Reviews where productid= order by ratings desc LIMIT 10 OFFSET 20.

How to query specific rows order by multiply result

I have a product table with commission column. I want to compare the product and sort by commission result. Is it possible to do so? Here I have :
id price com% (com)
3 3500 23 (805)
4 3800 23 (874)
30 3800 25 (950)
31 4000 25 (1000)
32 3500 22 (770)
According to the com column. I expect the result to sort from 31,30,4,3 and 1 accordingly. All I can thinking of is this query.
select * from excursion_db order by exc_com desc
Only sort by the commission (com column). Which may cause an error when there's a high com% but low price. Eventually the com might lower than the higher price but less com%.
So, how to get the specify row order by the sum of price*com% desc?
Please note that : The (com) column is not exists in the table. I write up here to compare the total commission.
Try this:
select * from excursion_db order by price * `com%` desc

Which rows are returned when using LIMIT with OFFSET in MySQL?

In the query below:
SELECT column
FROM table
LIMIT 18 OFFSET 8
how many results will we get as output and from where to where?
It will return 18 results starting on record #9 and finishing on record #26.
Start by reading the query from offset. First you offset by 8, which means you skip the first 8 results of the query. Then you limit by 18. Which means you consider records 9, 10, 11, 12, 13, 14, 15, 16....24, 25, 26 which are a total of 18 records.
Check this out.
And also the official documentation.
OFFSET is nothing but a keyword to indicate starting cursor in table
SELECT column FROM table LIMIT 18 OFFSET 8 -- fetch 18 records, begin with record 9 (OFFSET 8)
you would get the same result form
SELECT column FROM table LIMIT 8, 18
visual representation (R is one record in the table in some order)
OFFSET LIMIT rest of the table
__||__ _______||_______ __||__
/ \ / \ /
RRRRRRRR RRRRRRRRRRRRRRRRRR RRRR...
\________________/
||
your result
You will get output from column value 9 to 26 as you have mentioned OFFSET as 8
It will skip first 8 records and desplays records from 9 to 26
limit 18 : Display/select 18 records
offset 8 : will skip 8 records
if Your table has ids like 1,2,3,4,5,6,7,8,9,10,11.... and so on , so 1 to 8 records will be skipped and records after 9 to 26 ie 18 records will be displayed/selected.
Offset is majorly used to support pagination in MySql SELECT statements.
First the query will execute and then the records after the offset will be returned.
For example: let's say you want to show 10 reviews per page for a product as per the order of ratings (highest first), then below query can be used to get the reviews which will be shown on third page:
Select * from Reviews where productid= order by ratings desc LIMIT 10 OFFSET 20.

Limiting using WHERE instead of LIMIT

I want to select some rows with a specific filter, but don't limit if I don't get, at least, 40 rows.
It's a pseudo-example:
SELECT * FROM `table`
WHERE
SUM(1) < 40 OR
`age` > 18
It's similar to LIMIT, but LIMIT will consider the WHERE filter ever. I want to ignore the filter if I don't have at least 40 rows (but accept the firsts rows).
How I do that?
Edit: a lot of people had doubts what I really wanted.
This is an example:
ID AGE
1 10
2 20
3 30
4 10
5 20
6 30
7 10
I want to get the first 2 rows EVER. And only after at least two rows, get new rows that match the given conditions (WHERE).
For example: I want the first 2 rows more rows whose age is 30. The result would be equivalent to:
ID AGE
1 10 <first>
2 20 <second>
3 30 <conditional>
6 30 <conditional>
You can use an increasing variable #rownum to simulate the same functionality. However, this is much less efficient than limit because the server brings the filtered-out rows into memory and continuously performs the #rownum:=#rownum+1 calculation.
SELECT #rownum:=#rownum+1, t.*
FROM `table` t, (SELECT #rownum:=0) r
WHERE
#rownum <= 40 OR
`age` > 18

sql to select top 10 records

I have the following table (points):
recno uid uname points
============================
1 a abc 10
2 b bac 8
3 c cvb 12
4 d aty 13
5 f cyu 9
-------------------------
--------------------------
What I need is to show only the top ten records with by points (desc) and five records on each page. I have following the SQL statement:
select * from points where uid in(a,c) order by uid LIMIT 1, 5
Thanks
for the first page:
SELECT * FROM points p ORDER BY points DESC LIMIT 0, 5
for the second page:
SELECT * FROM points p ORDER BY points DESC LIMIT 5, 5
You can't execute an SQL query to return a set number of pages, you'll have to implement some kind of pagination module or whatever equivalent there is for the scenario you're in and fetch LIMIT 0, 5 for one then LIMIT 5, 5 for the other.
With such few records it wouldn't be an issue but in a production scale environment selected all records then breaking those results down into pages would be a lot of unnecessary overhead, it's good practice to only select the data you need.