Tag: 索引

为什么使用’gin’的add_index会创建一个’btree’索引呢?

我在PostgreSQL 9.3.4和Rails 4.0.4 。 我添加了一个“标签”列,以及相应的gin索引(或者,至少我要求一个)。 class AddTagsToPhotos < ActiveRecord::Migration def change add_column :photos, :tags, :text, array: true, null: false, default: [] add_index :photos, :tags, using: 'gin' end end 通过psqlvalidation结果: psql=# \d photos … tags | text[] | not null default ‘{}’::text[] Indexes: “index_photos_on_tags” btree (tags) 请注意,“标签”索引是btree类型 – 而我要求gin 。 现在手动创建一个索引以显示gin可用: psql=# create index index_photos_on_tags2 on […]

Ruby on Rails – 数据未保存。 索引显示空白值

我正在创建一个Ruby on Rails应用程序,但遇到了数据库问题。 我有“新”操作的控制器操作和视图,但除非我使用Rails控制台,否则我的值永远不会保存到数据库中。 我的Rails版本是4.0.4。 这是我的GamesController class GamesController “Game was saved” else render ‘new’ end end def edit @game = Game.find(params[:id]) end def update @game = Game.find(params[:id]) if @game.save redirect_to @game else render ‘edit’ end end def destroy @game = Game.find(params[:id]) @game.destroy redirect_to :action => ‘index’ end def game_params #Probably should use something inlace of […]

如何确定是否可以从展示次数表中删除某些索引

我正在使用基于Ruby on Rails的网站的印象派gem。 我们的数据库是mysql。 印象表大约是2M记录,这对MySql没有任何问题。 表大小为800M,开始对我们的服务器征税。 表的数据大小为200M,索引大小为600M。 大多数索引都是由gem预定义的。 有没有办法弄清楚是否正在使用或可以删除​​这些索引? 1,310,855,040 controlleraction_session_index 1,310,855,040 controlleraction_ip_index 1,310,855,040 controlleraction_request_index –880,757,504 poly_request_index –880,757,504 impressionable_type_message_index –880,757,504 poly_session_index –880,757,504 poly_ip_index —– 6,854,144主要

使用Ruby on Rails索引多列

我在Mysql数据库中有一个表,我想使用多列索引。 如何使用mysql控制台在rails中执行此操作?

路由根以显示登录用户的用户post和未登录的静态页面

故事:未登录的用户应该看到一个欢迎静态页面,当他登录时,他应该看到他的博客post列表。 我想正确的方法是将root路由到列出所有用户post然后检查身份validation的操作。 如果用户未登录,则呈现欢迎页面? 我需要帮助为posts控制器编写一个动作,该控制器显示登录用户的post。

Rails Postgresfunction索引

我应该如何将包含函数的多语言索引输入schema.rb? 例如,这不起作用: add_index “temporary_events”, [“templateinfoid”, “campaign”, “date(gw_out_time)”, “messagetype”], :name => “temporary_events_campaign_tinfoid_date_messagetype” rake db:test:load 耙子流产了! PGError:错误:列“date(gw_out_time)”不存在 :CREATE INDEX“temporary_events_campaign_tinfoid_date_messagetype”ON“temporary_events”(“templateinfoid”,“campaign”,“date(gw_out_time”,“messagetype”)

validation在另一个表上具有条件时,数据库中的唯一性validation

当validation有条件但我的要求已经改变时,我在数据库中的唯一性validation中询问了类似的问题,因此这个问题。 当有多个进程时,在Rails中使用唯一性validation是不安全的,除非在数据库上也强制执行约束(在我的情况下是PostgreSQL数据库,所以请参阅http://robots.thoughtbot.com/the-perils-of-uniqueness-validations )。 在我的例子中,唯一性validation是有条件的:只有在另一个模型上的另一个属性变为真时才应该强制执行。 所以我有 class Parent < ActiveRecord::Base # attribute is_published end class Child < ActiveRecord::Base belongs_to :parent validates_uniqueness_of :text, if: :parent_is_published? def parent_is_published? self.parent.is_published end end 所以模型Child有两个属性: parent_id (与Parent关联)和text (文本属性)。 模型Parent有一个属性: is_published (布尔值)。 如果其parent.is_published为true,则所有类型为Child模型的text应该是唯一的。 使用http://robots.thoughtbot.com/the-perils-of-uniqueness-validations中建议的唯一索引太过限制,因为无论is_published的值如何,它都会强制执行约束。 是否有人知道PostgreSQL数据库上依赖于另一个表的“条件”索引? validation具有条件时,数据库中唯一性validation的解决方案是您的条件取决于同一个表上的属性。 还是另一种解决方法?

何时索引,在Mongoid中索引什么?

我对索引有点新意,但我对索引的用例感到好奇。 (我假设它对索引字段的查询速度要快得多。)是否有标准来确定索引内容和索引时间? 我应该期待什么样的性能优势 – 特别是在Rails应用程序中使用Mongoid和MongoDb?

validation有条件时,数据库中的唯一性validation

当有多个进程时,在Rails中使用唯一性validation是不安全的,除非在数据库上也强制执行约束(在我的情况下是PostgreSQL数据库,所以请参阅http://robots.thoughtbot.com/the-perils-of-uniqueness-validations )。 在我的例子中,唯一性validation是有条件的:只有当模型中的另一个属性变为真时才应强制执行。 所以我有 class Model < ActiveRecord::Base validates_uniqueness_of :text, if: :is_published? def is_published? self.is_published end end 因此模型有两个属性: is_published (布尔值)和text (文本属性)。 对于Model iff is_published为true的所有模型, text应该是唯一的。 使用http://robots.thoughtbot.com/the-perils-of-uniqueness-validations中建议的唯一索引太过限制,因为无论is_published的值如何,它都会强制执行约束。 是否有人知道PostgreSQL数据库中的“条件”索引? 还是另一种解决方法?

Rails和Postgres Hstore:你能在迁移中添加索引吗?

我有一个迁移,我创建了一个像这样的产品表 class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.hstore :data t.timestamps end end end 在activerecord-postgres-hstore页面上,他们为表添加一个索引(在SQL中) CREATE INDEX products_gin_data ON products USING GIN(data); 然而,迁移不会跟踪这种变化(我猜是因为它是Postgres特定的?),有没有办法从迁移中创建索引? 谢谢!