在 Rails 中创建表
在rails中创建表时,主键是否默认为:id?
我试图通过 :id 访问表中的一行,但返回结果需要 3 秒多的时间。
该表包含大约 5-20Mb 的 BLOB,每当我使用 ModleName.find(:id) 直接引用一行时,该操作都会花费很长时间。
如果有人对如何加快这个过程有建议,我会洗耳恭听
When you create a table in rails, does the primary key default to :id?
I am trying to access a row in the table by :id, and it's taking over 3 seconds to return the result.
The table contains BLOBs on the order of 5-20Mb and whenever I directly reference a row using ModleName.find(:id), the operation takes a long time.
If anyone has suggestions on how to speed up this process I"m all ears
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,主键是
id
,但这并不意味着已创建任何索引
。要实现这一点,只需在
migration
文件中添加此类行:请参阅此处的文档。
The primary key is the
id
by default but it doesn't mean anyindex
has been created.To achieve that simply add this kind of line in a
migration
file:See doc here.
是的
。不要将实际文件存储在数据库中。您将遇到像现在这样的性能问题。
Yes
Don't store actual files in the database. You'll run into performance issues like you are right now.
是的,Rails 的主键默认为 :id
首先确保 select 查询在 MySQL 命令行本身中运行达到您想要的速度。
因为一旦我遇到了一个问题,MySQL 查询本身很慢,就好像 MySQL 查询很慢一样,Rails 对此无能为力。
无论如何,正如 Chirs 指出的那样,永远不要将文件存储在表中,除非您有充分的理由这样做。
Yes, Rails does the primary key default to :id
First make sure the select query runs up to the speed you wanted in the MySQL command line itself.
Because once I had an issue, where MySQL query itself is slow, as if the MySQL query is slow, there is nothing Rails can do about it.
Anyway as Chirs pointed out, never store files in the table, unless otherwise you have a very good reason to.