这些中哪个是最好的查询? [英] Which is the best query in these?

查看:96
本文介绍了这些中哪个是最好的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表Ledger拥有大量记录。

帐户ID为FK(未编入索引),

ID为PK



哪一个是获得更快结果的最好方法?

两个查询的时间为00:00:01秒



Table " Ledger" is having huge number of records.
Account ID is FK (not indexed),
ID is PK

which one is the best way to get faster result?
Its taking 00:00:01 seconds for both query

SELECT TOP 1 OpeningBal FROM Ledger
WHERE AccountID=123456 
ORDER BY ID DESC 






OR

SELECT OpeningBal FROM Ledger
 WHERE ID = (SELECT MAX(ID) FROM Ledger WHERE AccountID=123456 );





我尝试了什么:





What I have tried:

SELECT TOP 1 OpeningBal FROM Ledger
WHERE AccountID=123456 
ORDER BY ID DESC 



AND


AND

SELECT OpeningBal FROM Ledger
 WHERE ID = (SELECT MAX(ID) FROM Ledger WHERE AccountID=123456 );

推荐答案

第一个是最好的



actually first one is the best

SELECT TOP 1 OpeningBal FROM Ledger
WHERE AccountID=123456 
ORDER BY ID DESC 





和是我还建议





and yes I would also suggest to

"SET STATISTICS IO ON ;SET STATISTICS TIME ON


这篇关于这些中哪个是最好的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆