Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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 - Fatal编程技术网

什么';mysql中空列的顺序是什么?

什么';mysql中空列的顺序是什么?,mysql,ruby-on-rails,Mysql,Ruby On Rails,在我的rails 3应用程序中,有一个名为Book的模型 Book(id: integer, link_index: integer, publish_status: integer, link_page: integer, created_at: datetime, updated_at: datetime) 当我这样查询时,link\u index允许为NULL,其他不允许为NULL: Book.where(link_page: 1).published.order('link_index

在我的rails 3应用程序中,有一个名为
Book
的模型

Book(id: integer, link_index: integer, publish_status: integer, link_page: integer, created_at: datetime, updated_at: datetime)
当我这样查询时,
link\u index
允许为
NULL
,其他不允许为NULL:

Book.where(link_page: 1).published.order('link_index DESC').limit(5).pluck(:id)
它返回
[518331486488493]

但是当我使用
map
而不是
pull

Book.where(link_page: 1).published.order('link_index DESC').limit(5).map(&:id)
它返回
[5181516534566]

我们所知道的是:只有id=518的列具有link_index=4,其他所有列的link_index都为NULL。因此结果是正确的:518作为第一个元素返回

但在上述两种情况下,为什么空元素之间的顺序不同?


更新:

也许这不是关于
map
pulk
,因为我直接在mysql shell中使用SQL,这总是一个问题:

SELECT id FROM `books` WHERE `books`.`link_page` = 1 AND `books`.`publish_status` = 4 ORDER BY link_index DESC LIMIT 5;
返回:

+-----+
| id  |
+-----+
| 518 |
| 331 |
| 486 |
| 488 |
| 493 |
+-----+
+-----+------------+----------------+-----------+
| id  | link_index | publish_status | link_page |  
+-----+------------+----------------+-----------+
| 518 |          4 |              4 |         1 |
| 512 |       NULL |              4 |         1 |
| 516 |       NULL |              4 |         1 |
| 534 |       NULL |              4 |         1 |
| 566 |       NULL |              4 |         1 |
+-----+------------+----------------+-----------+
但是

返回:

+-----+
| id  |
+-----+
| 518 |
| 331 |
| 486 |
| 488 |
| 493 |
+-----+
+-----+------------+----------------+-----------+
| id  | link_index | publish_status | link_page |  
+-----+------------+----------------+-----------+
| 518 |          4 |              4 |         1 |
| 512 |       NULL |              4 |         1 |
| 516 |       NULL |              4 |         1 |
| 534 |       NULL |              4 |         1 |
| 566 |       NULL |              4 |         1 |
+-----+------------+----------------+-----------+

为什么?

地图和弹拨是完全不同的功能map在采集级别上运行,而asPULL在db级别上运行


我建议你检查一下这两个。不同之处在于用于检索数据的索引或某些临时表的使用。第一个查询只返回ID,所以可以使用适当索引上的索引扫描或索引合并来获取这些ID,然后顺序取决于这些ID的BTREE顺序。在第二种情况下,计划会有所不同,可能会使用不同的索引集或不同的索引顺序,因此它会以其他顺序选择行-如果很多值为空,则不存在“正确”的顺序(您没有定义第二列以用于重复链接索引的情况),mysql可以自由选择它认为最好的(成本最低的计划和其他东西隐藏在那里)。

您的
地图版总是产生相同的结果?是的!总是相同的。谢谢~我知道
地图和
弹拨
之间的区别,但问题可能不是由此引起的,请参阅我更新了我的问题。