2017-03-18 48 views
2

比方说,我有这样定义的MySQL表:给出MySQL的选择及限位计算复杂

create table test_table(
id int(10) unsigned auto_increment primary key 
/*, other attributes...*/ 
); 

这表我想取从像这样的最后一个记录:

select * from test_table order by id desc limit 1; 

它作品,但它有点粗略,它的复杂性是什么? 因为在选择之后执行“限制”和“排序依据”,是否为O(log(n))?

有没有更好的方法从自动增量表中选择最后一条记录?

回答

3

您也可以使用此方法获得所需的输出。

SELECT * FROM test_table where id=(select max(id) from test_table); 

希望,这会帮助你。

+0

这似乎工作,但什么是max()函数的复杂性? 它是不是遍历所有索引来找到最高的?感谢您的答复! –