使用 SubSonic 获取单条记录的最快方法

发布于 2024-07-18 06:43:56 字数 716 浏览 6 评论 0原文

我是 SubSonic 和 Linq Stuff 的新手,我正在尝试找出检索单个记录的最短且最佳的方法。

还有什么其他方法比这更快并且需要编写更少的代码来获取单个记录?

User user2 = DB.Select().From(User.Schema)
.Where(User.PasswordColumn).IsEqualTo(password)
.And(User.SINumberColumn).IsEqualTo(siNumber)
.ExecuteSingle<User>();

我使用 AntsProfiler 工具进行检查,这平均需要 29.12 毫秒的 CPU 时间 - 经过十次运行测试,

而这需要更长的时间,

UserController uc = new UserController();
Query query = new Query("User");
query.WHERE(User.Columns.Password, password);
query.WHERE(User.Columns.SINumber, siNumber);   
User user = uc.FetchByQuery(query).First<User>();

仅最后一行需要 256.08 毫秒的 CPU 时间加上 UserController 需要 66.86 毫秒。

有什么建议么?

I am new to SubSonic and Linq Stuff and I am trying a figure out the shortest and optimal way of retrieving a single record.

What other way is quicker and requires less code to write than this to get a single record?

User user2 = DB.Select().From(User.Schema)
.Where(User.PasswordColumn).IsEqualTo(password)
.And(User.SINumberColumn).IsEqualTo(siNumber)
.ExecuteSingle<User>();

I have used to AntsProfiler tool to check, and this takes avg of 29.12ms CPU time - tested over ten runs

Where as this takes even longer

UserController uc = new UserController();
Query query = new Query("User");
query.WHERE(User.Columns.Password, password);
query.WHERE(User.Columns.SINumber, siNumber);   
User user = uc.FetchByQuery(query).First<User>();

Just the last line take 256.08ms CPU time plus UserController takes 66.86ms.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与清晨阳光 2024-07-25 06:43:56

“瓶颈”是生成的查询执行,而不是 SubSonic 生成查询或返回结果。 如果特定查询很慢,您应该考虑使用数据库提供商的索引功能来优化它。

在第二种情况下,我假设额外的执行时间是 LINQ 从集合中返回第一项的开销。

所以我的答案是,第一种方法是最好的方法,如果 29ms 不可接受(对于对数据库的调用,我认为这不是不合理的),请在数据库中添加一些索引以加快速度上检索。

The "bottleneck" would be the generated query executing, not SubSonic generating it or returning the result. If a particular query is slow, you should look into using your database provider's indexing functions to optimize it.

In the second case, I'm assuming the additional execution time is overhead for LINQ to return the first item from the collection.

So my answer would be, the first way is the best way to do it, and if 29ms isn't acceptable (which for a call to a DB, I don't think is unreasonable), add some indexes in your DB to speed up retrieval.

固执像三岁 2024-07-25 06:43:56

IIRC,查询对象很流畅。即

query.WHERE(User.Columns.Password, password);
query.WHERE(User.Columns.SINumber, siNumber);

应该读作:

query = query.WHERE(User.Columns.Password, password);
query = query.WHERE(User.Columns.SINumber, siNumber);

测试中增加的时间可能是由于这个原因造成的(您要求所有用户项目,然后抓住第一个)。

IIRC, the Query object is fluent.. i.e.

query.WHERE(User.Columns.Password, password);
query.WHERE(User.Columns.SINumber, siNumber);

Should read as:

query = query.WHERE(User.Columns.Password, password);
query = query.WHERE(User.Columns.SINumber, siNumber);

The increased time in your tests may be accounted for because of this (you're asking for all User items and then grabbing the First).

习惯成性 2024-07-25 06:43:56

我认为约翰在金钱上是正确的。 如果您确实想测试 SubSonic 以了解它需要多长时间,您需要测试 SubSonic 创建 SQL 语句需要多长时间,而不是 SubSonic 创建 SQL 语句需要多长时间以及数据库返回结果需要多长时间同时(您需要隔离您的测试)。

也许您应该测试使用常规旧 SQLCommand 获取结果需要多长时间,并将您的连接和参数传递给它,并查看需要多长时间以便进行比较。

I think that John is right on the money. If you really want to test SubSonic to see how much time it takes you need to test how long it takes subsonic to create the SQL statement not how long it takes SubSonic to create the SQL statement and how long it takes your DB to return the results at the same time(You need to isolate your tests).

Perhaps you should test how long it takes to get the results with a Regular old SQLCommand and pass it your connection and parameters and see how long that takes so that you have a comparison.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文