ActiveRecord 查询(城堡、性能)
我有 3 个表:
- 零件:
名称
:内部名称,活动
:布尔 - 语言:语言列表(英语、法语、德语……)
- PartsTranslations:
RealName
和其他 2 个表的 Id。
我想要获取部件列表,告诉我内部名称
、活动
状态以及有多少翻译缺少(总语言减法翻译)
我做了这个SQL查询,它给了我我需要的东西(我不知道这是否是更好的方法,但它有效):
SELECT
parts1.name,
parts1.active,
(
(SELECT count(lang.id)
FROM languages AS lang)
-
(SELECT count(trans.id)
FROM parts AS parts2
INNER JOIN partstranslations as trans
ON parts2.id = trans.partid
WHERE parts2.id = parts1.id)
)
from parts as parts1;
1°问题-< /strong> 如何使用 Castle ActiveRecord 进行此查询?
2°问题-最终查询(昂贵的查询)的性能如何?
谢谢
I've 3 tables:
- Parts:
Name
: internal name,Active
: bool - Languages: list of languages (English, French, German, ....)
- PartsTranslations:
RealName
and Id's of the other 2 tables.
I would like to get a list of Parts telling me the internal name
, active
status and how many translations are missing (total lang subtract translations made)
I made this SQL query that is giving me what I need (I don't know if this is the better way to make it or not, but it's working):
SELECT
parts1.name,
parts1.active,
(
(SELECT count(lang.id)
FROM languages AS lang)
-
(SELECT count(trans.id)
FROM parts AS parts2
INNER JOIN partstranslations as trans
ON parts2.id = trans.partid
WHERE parts2.id = parts1.id)
)
from parts as parts1;
1º question- How can I make this query using Castle ActiveRecord?
2º question- How's the performance of the final query (expensive one)?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能够使用 HqlBasedQuery 在 ActiveRecord 中进行此查询,因此我在此处发布答案以帮助与我情况相同的其他人。
我还对此查询进行了分页,它还为我提供了强类型的
PartProjection
对象。此类不需要任何 ActiveRecord 参数。I was able to make this query in ActiveRecord using
HqlBasedQuery
, so I post here the answer to help others in the same situation as me.I also have pagination made on this query and it also give me a stronged typed
PartProjection
objects. This class does NOT need to have any ActiveRecord parameter.我无法帮助 ActiveRecord,但此查询应该比具有相关子查询的查询执行得更好。
或者,如果您确实想使用相关子查询,则无需再次从其中的
Parts
中进行选择:I can't help with ActiveRecord, but this query should perform better than the one with a correlated subquery.
Or, if you do want to use the correlated subquery, you don't need to select from
Parts
in it again: