首页 \ 问答 \ 在主动记录中深度嵌套连接(deeply nested joins in activerecord)

在主动记录中深度嵌套连接(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
更新时间:2022-05-04 11:05

最满意答案

我注意到的第一件事是你的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.

相关问答

更多
  • 我注意到的第一件事是你的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 ([ ...
  • 你可能想要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 ...
  • 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) ...
  • 只有大约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 -> ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)