Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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
我应该将排序键字段与外键一起索引,还是单独索引(mysql)?_Mysql_Ruby On Rails_Sorting_Indexing_Foreign Keys - Fatal编程技术网

我应该将排序键字段与外键一起索引,还是单独索引(mysql)?

我应该将排序键字段与外键一起索引,还是单独索引(mysql)?,mysql,ruby-on-rails,sorting,indexing,foreign-keys,Mysql,Ruby On Rails,Sorting,Indexing,Foreign Keys,(这在Rails应用程序中) 给出两个表格: items id, int(11), primary key name, varchar(30) choices id, int(11), primary key item_id, int(11), foreign key identifier, varchar(5) name, varchar(30) 针对选项的大多数查询将大致如下: SELECT identifier, name FROM choices WHERE item_id = n

(这在Rails应用程序中)

给出两个表格:

items
id, int(11), primary key
name, varchar(30)

choices
id, int(11), primary key
item_id, int(11), foreign key
identifier, varchar(5)
name, varchar(30)
针对选项的大多数查询将大致如下:

SELECT identifier, name FROM choices WHERE item_id = n ORDER BY identifier;
让我们假设我们都同意索引可以帮助排序性能(我知道我们都没有,这没关系,但对于这个问题,让我们假设)

索引选项的最佳方法是什么,以获得搜索和排序的好处:

  • 两个索引,分别位于项目id标识符上

  • 项目id、标识符上的一个索引

1在
项目id、标识符上的复合索引将更好地用于您的查询,因为这样的索引将覆盖。

您需要复合索引。有了两个单独的索引,mysql必须在使用索引进行过滤或使用索引进行排序之间进行选择(因为mysql在查询中每个表只使用一个索引)。(您的案例就是他们给出的例子之一)。

我从来没有使用过rails,但是不可能使用B+树索引项\u id和标识符吗?
在那里,您可以进行快速查询,也可以对结果进行排序。

这是我的直觉。我没有看到链接页面。非常感谢。我感谢您的快速响应。这更多的是在数据库中,而不是在应用程序(Rails)中。我相信MySQL默认为BTree索引。