在主动记录中深度嵌套连接(deeply nested joins in activerecord)
我试图在我的网站中优化我的查询。
我有3个模型,照片,人物,外观
照片has_many外观,许多人通过外观外观表只有一个person_id,photo_id
现在,如果我要对“人物”进行搜索,并且想要加载他们的外观和照片,我会这样做:
Person.joins(:appearances => :photo).limit(5)
现在,我不确定这是否理想,但假设我的人有属于照片的外观,而照片又有外表和其他人。 我甚至不知道在香草SQL中是否或如何做到这一点,但我只是好奇,如果这是可能的。
Person.joins(:appearances => :photo => :appearaces => :person).limit(5)
这个语法导致了错误,我只是好奇,我正在处理在视图中获取照片的外观和人物,我只是想试验加载时间,看看这是否可能。
I'm trying to optimize my queries throughout my site.
I have 3 models, Photo, Person, Appearances
Photo has_many appearances, and many people through appearances Appearances table just has a person_id, photo_id
Now, if I were to do a search on a 'Person' and I wanted to eager load their appearances and photos, I would do something like this:
Person.joins(:appearances => :photo).limit(5)
Now, I'm not sure if this is ideal, but hypothetically my person has appearances which belongs to a photo, which in turn has appearances and other people. I don't even know if or how you'd do this in vanilla SQL, but I am just curious if this is possible.
Person.joins(:appearances => :photo => :appearaces => :person).limit(5)
This syntax results in errors, again, I'm just curious, I'm handling getting the appearances and people of a photo inside my views, I just wanted to experiment with load times and see if this was even possible.
原文:https://stackoverflow.com/questions/13404843
最满意答案
我注意到的第一件事是你的
instance
语法不完全正确:instance (Ord k) => MyFunctor (DataMap.Map k) where ...
否则,它似乎很好。
First thing I noticed is that your
instance
syntax isn't quite right:instance (Ord k) => MyFunctor (DataMap.Map k) where ...
Otherwise it seems fine.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
我注意到的第一件事是你的instance语法不完全正确: instance (Ord k) => MyFunctor (DataMap.Map k) where ... 否则,它似乎很好。 First thing I noticed is that your instance syntax isn't quite right: instance (Ord k) => MyFunctor (DataMap.Map k) where ... Otherwise it seems fine.
-
那么,第一 - ...如果它使用真正的Functor而不仅仅是Endofunctors ... 哈斯克尔函子是真正的函子。 只有Functor类不允许使用任何类型的通用函数,但只允许Hask中的特定函数 。 事实上,非内分子函数是有趣的; 数学家一直都在使用它们。 正如你所说,不可能有非内模单元monad,与Applicative类似是可能的:该类实际上只是一个很好的Haskell接口,用于monoidal函数 ,它可以在不同类别之间定义。 看起来像这样: class (Functor r t f, Cat ...
-
fromAscList函数应该应用于升序列表。 但是“h”>“_c”。 你的代码违反了这个先决条件,所以它不能正常工作。 The fromAscList function should be applied to an ascending list. But "h" > "_c". Your code violates this precondition, so it is not a surpruse that it does not work properly.
-
commands :: Map String ([Handle] -> IO ()) commands = fromAscList [ ("o",_), ("i",_), ("q",_) ] 但 ghci> Data.List.sort ["o","i","q"] ["i","o","q"] 你对Data.Map说谎,所以它构造了一个不满足要求的不变量的Map 。 因此,查找Map中的内容不起作用,因为请求被发送到错误的分支(有时)。 commands :: Map String ([ ...
-
Haskell O(n)(或最接近)的方式来“更改”Data.Map中的值(Haskell O(n) (or closest) way to “alter” values in a Data.Map)[2022-04-04]
你可能想要mapMaybeWithKey函数: mapMaybeWithKey :: (k -> a -> Maybe b) -> Map ka -> Map kb 上)。 映射键/值并收集Just结果。 或者只是简单的mapMaybe如果你不需要访问密钥。 You probably want the mapMaybeWithKey function: mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b O(n). Map keys/va ... -
使用或不使用Data.Map(To Use or Not to Use Data.Map)[2023-05-26]
map-map的初始化仍然只需要O(n) 。 首先考虑列表清单。 假设外部列表是[a 1 ,a 2 ,...,a p ],并且每个内部项目是j =(l j ,[b 0 ,b 1 ,...,b q j ])。 然后,列表清单的构建需要O(n =Σj = 1 p q j )。 初始化内部地图需要m j 。 = O(q j )。 初始化map-of-map需要O(Σj = 1 p m j )= O(n)。 Initialization of the map-of-maps still only takes O(n ... -
关键的想法 我的理解是Missing3和Unknown3工作有点像Nothing ,除了他们提供了一些关于为什么没有答案的反馈,所以可能表现得彼此略有不同。 当然,我认为Missing3应该表现得像Nothing 。 让我们看一下这些是如何为Maybe定义的: 函子 这是Maybe的Functor实例: instance Functor Maybe where fmap _ Nothing = Nothing fmap f (Just a) = Just (f a) ...
-
何时应该使用Haskell的Data.Map来支持元组列表?(When should Haskell's Data.Map be used in favor of a list of tuples?)[2023-06-25]
只有大约2000个日期,因此我并不关心性能(你可以看到我到处使用Strings); 当时正在使用Data.Map过度杀伤? 什么时候Data.Map应该优先于元组列表? 您应该使用适合您的问题和性能/编程时间约束的数据结构,因此使用Map可能是一个好主意。 也许在您的情况下,如果您的数据已经订购,您可以做到 union [] _ = [] union _ [] = [] union xss@((dx,vx):xs) yss@((dy,vy):ys) = case compare dx dy of ... -
Data.Map.update与insert略有不同(如果你交换参数,它应该只适用于你的例子)。 insert在地图中设置一个值,如果它已经替换它。 update执行查找并使用函数根据以前的值定义值的内容。 它可以返回Nothing来声明应该删除该键的值,或者Just新值。 也许类型签名会更好地澄清: update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a 和 insert :: Ord k => k -> a -> Map k a -> ...