[High Score] queries the last row of data based on the condition.

Source: Internet
Author: User
[High score for help] which of the max (id) and orderbydesc queries for the last row of data based on the condition is more efficient? Is there any other method ?, Orderby has always been used ., If id is a primary key, it is the same. Otherwise, if id already exists, the former result is not necessarily correct. I think it should be orderby because the index can be used, max [high score for help] queries the last row of data based on conditions
Max (id) and order by desc which are more efficient?
Is there any other method?

------ Solution --------------------
Order by is always used.
------ Solution --------------------
If id is a primary key, it is the same
Otherwise, if the id already exists, the former result is not necessarily correct.
------ Solution --------------------
I think it should be order by, because indexes can be used, while max needs to traverse and compare
------ Solution --------------------
Discussion

If id is a primary key, it is the same
Otherwise, if the id already exists, the former result is not necessarily correct.

------ Solution --------------------
Discussion

If id is a primary key, it is the same
Otherwise, if the id already exists, the former result is not necessarily correct.

------ Solution --------------------
The explian of the first SQL statement is
1, "SIMPLE", "collect", "index", NULL, "PRIMARY", "4", NULL, 1, "Using index"


Article 2 SQL
1, "SIMPLE", NULL, "Select tables optimized away"

------ Solution --------------------
Discussion

Reference:

If id is a primary key, it is the same
Otherwise, if the id already exists, the former result is not necessarily correct.


After testing, there is no difference in speed.

PHP code

Mysql_connect ('2017. 0.0.1 ', 'root', 'root ');
Mysql_select_db ('collect ');

$ Sql1 = 'SELECT id from collect order ......

------ Solution --------------------
Is the id of the primary key?
If the innodb engine is used, order by limit 1 is more efficient.
In myisam, simple select max (id) is fast, and the maximum id is equivalent to the one in the cache, because I show that the Extra column displays Select tables optimized away, is the same as select count (id.
However, if you select other fields in addition to max (id), the efficiency of a statement may be inferior to that of order by limit 1,
Of course, your statement can be changed
Select * from table where id = (select max (id) as id from table)
It should be very fast. it's my explicit-based analysis. you can test it.
View the explain analysis by yourself.

------ Solution --------------------
For explains on simple count queries (I. e. explain select count (*) from people) the extra section will read "Select tables optimized away. "This is due to the fact that MySQL can read the result directly from the table internals and therefore does not need to perform the select.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.