Order of execution of ORDER BY and LIMIT in a MySQL query - mysql

I have a query like this where I want to display sorted records in a paginated format.
This is my query.
SELECT * FROM answers
WHERE userid = '10'
ORDER BY votes LIMIT 10, 20
The two arguments to LIMIT will be used for generating page-by-page display of records. Now, my problem is ORDER BY. How does MySQL execute this query?
1st way
Select the records according to filters
Sort them
Limit them
2nd way
Select the records according to filters
Limit them
Sort them
If I think like MySQL engine, I would want to implement 2nd, since I would then have to sort lesser records?
Can somebody throw some light on this?
Regards

A SQL LIMIT will in all databases always work on the result of a query, so it will run the query with the ORDER BY and that result will then be limited. This is a functional requirement, the database not perse needs to execute it like that.
Thus the 1st way.

Mysql follows the 1st way.

If you want your 2nd way, try using a subquery with the limit, then order.
Something like:
SELECT * FROM
(
SELECT * FROM answers
WHERE userid = '10'
LIMIT 10
)
ORDER BY votes

The 1st way as mentioned by the last two answers. From a user perspective, the 2nd way won't be very useful anyways.

Related

Limiting the count query in MySQL?

I am trying to do a simple test where I'm pulling from a table the information of a specific part number as such:
SELECT *
FROM table_name
WHERE part_no IN ('abc123')
This returns 25 rows. Now I want to count the number that meet the "accepted" condition in a specific column but the result is limited to only the 10 most recent. My approach is to write it as follows:
Select Count(*)
FROM table_name
WHERE part_no IN ('abc123') AND lot IN ('accepted')
ORDER BY date DESC
LIMIT 10
I'm having a hard time to get the ORDER BY and LIMIT operations to work. I could use help just getting it to limit appropriately, and I can figure out the rest from there.
Edit: I understand that the operations are happening on the COUNT which only returns one row with a value; but I put the second clip to show where I am stuck in my thought process.
Your query SELECT Count(*) FROM ... will always return exactly one row.
It's not 100% clear what exactly you want to do, but if you want to know how many of the last 10 have been accepted, you could use a subquery - something like:
SELECT COUNT(*) FROM (
SELECT lot
FROM table_name
WHERE part_no IN ('abc123')
ORDER BY date DESC
LIMIT 10
)
WHERE lot IN ('accepted')
The inner query will return the 10 most recent rows for part abc123, then the outer query will count the accepted ones.
There are also other solution (for example, you could have the inner query output a field that is 0 when the part is not accepted and 1 when the part is accepted, then take the sum). Depending on which exact dialect/database you are using, you may also have more elegant options.
Select count returns ONE ROW therefore the ORDER BY and the LIMIT will not work on the results

Retrieve all records with LIMIT in mysql

I have the following MySQL query;
SELECT records FROM users
LIMIT 1
One record will be retrieved. Suppose I want to retrieve all records but still use the keyword LIMIT. Can I do something like this?
SELECT records FROM users
LIMIT ALL
Just use
SELECT records FROM users
Do not use LIMIT at all
Or if it is required to use LIMIT , use a very high number
SELECT records FROM users LIMIT 999999999999
You have to just make up a ridiculous number and limit it by that.
E.g. SELECT records FROM users LIMIT 18446744073709551615 if you really want to keep the limit clause. This is similar to using offset to infinite amount.
According to mysql doc you are supposed to use a very large number.
SELECT * FROM tbl LIMIT 95,18446744073709551615;
Terrible solution, if you ask me, but its from the mysql documentation
http://dev.mysql.com/doc/refman/5.0/en/select.html#id4651990
If you want to retrieve all data then just use
select * from tbl;

Saving time in queries on sql

I have a scenario. I have say 300 records in my table. I execute a query to get the total count. Then , since i have to implement pagination,
I select the data from the same table using limits according t the count. I was thinking if i can get the count and data in a single query.? .
I tried below code:
Select * ,count(*) as cnt from table;
But this gave me the total count but only 1 record!
Is there a way to save my time exhausted in query and get results in a single query?
something like:
select t1.*,t2.cnt
from table t1
cross join (select count(*) as cnt from table) t2
limit 'your limit for the first page'
or
select *,(select count(*) from table) as cnt
from table
limit 'your limit for the first page'
You can get information in data structure you mentioned, but there is really no reason to do it. There is no performance problem when you do two queries - one for getting rows count and another for data selection. You don't save anything when you try to select all information in one query. Do two simple queries instead, it will be better solution for your app - you will preserve its simplicity and clarity.
Using two queries might not be as bad as you may think, you can read this for more information.

get total for limit in mysql using same query?

I am making a pagination method, what i did was:
First query will count all results and the second query will do the normal select with LIMIT
Is there technically any way to do this what I've done, but with only one query?
What I have now:
SELECT count(*) from table
SELECT * FROM table LIMIT 0,10
No one really mentions this, but the correct way of using the SQL_CALC_FOUND_ROWS technique is like this:
Perform your query: SELECT SQL_CALC_FOUND_ROWS * FROM `table` LIMIT 0, 10
Then run this query directly afterwards: SELECT FOUND_ROWS(). The result of this query contains the full count of the previous query, i.e. as if you hadn't used the LIMIT clause. This second query is instantly fast, because the result has already been cached.
You can do it with a subquery :
select
*,
(select count(*) from mytable) as total
from mytable LIMIT 0,10
But I don't think this has any kind of advantage.
edit: Like Ilya said, the total count and the rows have a totally different meaning, there's no real point in wanting to retrieve these data in the same query. I'll stick with the two queries. I just gave this answer for showing that this is possible, not that it is a good idea.
While i've seen some bad approaches to this, when i have looked into this previously there were two commonly accepted solutions:
Running your query and then running the same query with a count as you have done in your question.
Run your query and then run it again with the SQL_CALC_FOUND_ROWS keyword.
eg. SELECT SQL_CALC_FOUND_ROWS * FROM table LIMIT 0,10
This second approach is how phpMyAdmin does it.
You can run the first query and then the second query, and so you'll get both the count and the first results.
A query returns a set of records. The count is definitely not one of the records a "SELECT *" query can return, because there is only one count for the entire result set.
Anyway, you didn't say what programming language you run these SQL queries from and what interface you're using. Maybe this option exists in the interface.
SELECT SQL_CALC_FOUND_ROWS
your query here
Limit ...
Without running any other queries or destroying the session then run
SELECT FOUND_ROWS();
And you will get the total count of rows
The problem with 2 queries is consistency. In the time between the 2 queries the data may be changed. If your logic depends on what is counted has to be returned then your only approach would be to use SQL_CALC_FOUND_ROWS.
As of Mysql 8.0.17 SQL_CALC_FOUND_ROWS and FOUND_ROWS() will be deprecated. I don't know of a consistent solution after this functionality has been removed.

mysql limit - a general question

Assuming the query
SELECT * FROM table WHERE a='1' and b='2' and c>'3' and d>'4' and e!='5' and f='6'
returns 10000 results.
My question is, let's say I limit the search to the first 10 results like this:
SELECT * FROM table WHERE a='1' and b='2' and c>'3' and d>'4' and e!='5' and f='6' LIMIT 10
Will mysql search through all the 10000 results or it will stop at the 10th result?
LIMIT will only display the rows specified, based on their position within the resultset. Without an ORDER BY, you're relying on the order the records were inserted.
You'll probably be interested to read about MySQL's ORDER BY/LIMIT performance...
Since there is no ORDER BY, it will stop at the 10th result (after going through as many non-matching rows as necessary). As OMG Ponies says, which 10 rows you get is unspecified.