关于读取表中最后一项数据(后续)第一种select *
from tb
where id=(select max(ID)from tb)
和第二种select top 1 * from tb order by id desc
如果tb表中有10万个数据,每个数据里有10项第一种直接求最大ID是不是比第二种ID排序执行速度快啊?
from tb
where id=(select max(ID)from tb)
和第二种select top 1 * from tb order by id desc
如果tb表中有10万个数据,每个数据里有10项第一种直接求最大ID是不是比第二种ID排序执行速度快啊?
如果id是key的话,当然2快了
如果id有重复的话以一个可能会出现多条数据
如果id重复且数据没有任何索引的情况下,应该是一个人快
id唯一且有聚集索引应该一样快。
这个你可以自己试试,
呵呵,沟沟事实是不是这样的?
select top 1000000 id=identity(int,1,1) into Qas from syscolumns a,syscolumns b,syscolumns c,syscolumns d
GO
select top 1 * from Qas order by id desc
/*
执行时间 0秒
*/
select * from Qas where id=(select max(id) from Qas)
/*
执行时间 1秒
*/
GO
drop table Qas
你可以试下当Qas有1000笔和100000笔时的情况。又要推荐一下,请看看leimin 大侠的 “一句SQL引发的思考”
执行计划有时候也会骗人的。
还有请教下 索引是自己手动建,还是sql系统自动建的?手动建 要怎么建索引?
看执行计划,还是少许不用
Estimated row size, top 1 的是11,=max的是19
其他都一样
ON 字段
要自己手动建,sql自动帮你维护和管理的
猜想
0)SELECT COUNT(ID) FROM ta-----------
100015
Warning: Null value is eliminated by an aggregate or other SET operation.1)加索引后两者执行计划近乎相同 一个是Index Scan 一个Index Seek,理论上Seek高于ScanCREATE CLUSTERED INDEX tID ON ta (ID)
GO
--DROP INDEX ON ta.ID
--GO--第一种SQL codeselect *
from ta
where id=(select max(ID)from ta)1 1 select * from ta where id=(select max(ID)from ta)
1 1 |--Top(TOP EXPRESSION:((1)))
2 1 |--Clustered Index Seek(OBJECT:([Testshen].[dbo].[ta].[tID]), SEEK:([Testshen].[dbo].[ta].[ID] IsNotNull) ORDERED BACKWARD)--DBCC DROPCLEANBUFFERS--第二种SQL codeselect top 1 * from ta order by id desc1 1 select top 1 * from ta order by id desc
1 1 |--Top(TOP EXPRESSION:((1)))
1 1 |--Clustered Index Scan(OBJECT:([Testshen].[dbo].[ta].[tID]), ORDERED BACKWARD)Ⅱ
1)第一种SQL codeselect *
from ta
where id=(select max(ID)from ta)--这里2次Table Scan并且用到Nested Loops Join
select * from ta where id=(select max(ID)from ta)
1 1 select * from ta where id=(select max(ID)from ta)
1 1 |--Nested Loops(Inner Join, WHERE:([Expr1008]=[Testshen].[dbo].[ta].[ID]))
1 1 |--Stream Aggregate(DEFINE:([Expr1008]=MAX([Testshen].[dbo].[ta].[ID])))
100016 1 | |--Table Scan(OBJECT:([Testshen].[dbo].[ta]))
100016 1 |--Table Scan(OBJECT:([Testshen].[dbo].[ta]))--DBCC DROPCLEANBUFFERS2)第一种SQL code--这里1次表Table Scan和Sortselect top 1 * from ta order by id desc1 1 select top 1 * from ta order by id desc
1 1 |--Sort(TOP 1, ORDER BY:([Testshen].[dbo].[ta].[ID] DESC))
100016 1 |--Table Scan(OBJECT:([Testshen].[dbo].[ta]))
我肯定用第二种嘛。
清楚明了。
top 还是很不错的
求最大值的时间复杂度是O(n)
最好的排序算法的时间复杂度也要O(n*log(n))SQL跟这个是没有关系啊
select count(*) from dbo.TB_FB
805616条数据
select *
from TB_FB
where id=(select max(ID)from TB_FB)
CPU 15 Reads 258
select top 1 * from TB_FB order by id desc
CPU 15 Reads 5
set @time=getdate()
--第一种
select top 1 * from tb order by id desc
select datediff(ms,@time,getdate())as '查询时间(毫秒)' --第二种
set @time=getdate()
select * from tb where id=(select max(ID)from tb)
select datediff(ms,@time,getdate())as '查询时间(毫秒)'
7 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
7
7 SQL Server 执行时间:
7 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
8
8 SQL Server 执行时间:
8 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
10 SQL Server 分析和编译时间:
10 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
10 表 'PersonalInfo'。扫描计数 1,逻辑读取 3 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
10
10 SQL Server 执行时间:
10 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
方法2
12 SQL Server 分析和编译时间:
12 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
12
12 SQL Server 执行时间:
12 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
13
13 SQL Server 执行时间:
13 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
15 SQL Server 分析和编译时间:
15 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
15 表 'PersonalChild'。扫描计数 1,逻辑读取 7 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
15
15 SQL Server 执行时间:
15 CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。
表PersonalInfo有No Column Name
650280条数据