Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如果rails中有复合索引,是否需要单列索引?_Ruby On Rails_Postgresql_Rails Migrations_Compound Index - Fatal编程技术网

Ruby on rails 如果rails中有复合索引,是否需要单列索引?

Ruby on rails 如果rails中有复合索引,是否需要单列索引?,ruby-on-rails,postgresql,rails-migrations,compound-index,Ruby On Rails,Postgresql,Rails Migrations,Compound Index,我用PostgreSQL在rails中创建了一个表,它是帐户和程序之间的连接表,添加索引的最佳方法是什么?当我有两个复合索引顺序时,是否需要为每个引用使用单个索引index:true create_table :custom_library_programs do |t| t.references :account, index: true, foreign_key: true t.references :program, index: true, foreign_key: true

我用PostgreSQL在rails中创建了一个表,它是帐户和程序之间的连接表,添加索引的最佳方法是什么?当我有两个复合索引顺序时,是否需要为每个引用使用单个索引
index:true

create_table :custom_library_programs do |t|
  t.references :account, index: true, foreign_key: true
  t.references :program, index: true, foreign_key: true
  t.boolean :submitted, default: false

  t.timestamps null: false

  t.index [:account_id, :program_id]
  t.index [:program_id, :account_id]
end 

不,你的索引将涵盖你。复合索引的工作方式是,它只能按索引定义的列顺序顺序使用。例如,您的第一个索引:

t.index [:account_id, :program_id] ##Speeds up queries for accounts or 'accounts & programs'
工作原理与

t.index:account_id
##加快对帐户的查询速度

如果你只是在查账户。但是,这不会加快单独查找程序的查询速度(只会加快对“帐户和程序”的查询),但由于您以相反的方式创建了复合索引(
t.index[:program\u id,:account\u id]
),因此您也可以双向快速查找