Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.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
Php 如何实现MySQL聚集索引有序连接结果_Php_Mysql_Join_Clustered Index - Fatal编程技术网

Php 如何实现MySQL聚集索引有序连接结果

Php 如何实现MySQL聚集索引有序连接结果,php,mysql,join,clustered-index,Php,Mysql,Join,Clustered Index,假设我有两个表,表1有两个字段:id和cost,有一个聚集的、主的、关于cost的索引id。另一个表table2只有id,这是主键 因此,由于聚集索引,表1中的数据按成本排序 如果我去: Select * from table1 join table2 using (id) 结果并不总是按照表1中聚集索引定义的顺序。在这种情况下,当使用特定/复合聚集索引将多个表连接到一个表时,如何实现这种排序?您正在寻找的是一个STRIAGHT\u连接 如果您这样做: SELECT * from table1

假设我有两个表,表1有两个字段:id和cost,有一个聚集的、主的、关于cost的索引id。另一个表table2只有id,这是主键

因此,由于聚集索引,表1中的数据按成本排序

如果我去:

Select * from table1 join table2 using (id)

结果并不总是按照表1中聚集索引定义的顺序。在这种情况下,当使用特定/复合聚集索引将多个表连接到一个表时,如何实现这种排序?

您正在寻找的是一个STRIAGHT\u连接

如果您这样做:

SELECT * from table1 STRIAGHT_JOIN table2 using (id)

订单将保留。

请小心。SQL结果集排序在形式上是不可预测的,除非在查询中包含ORDERBY子句。形式上的不可预测性比随机性更糟糕,因为在测试中更难捕捉到:结果集的顺序保持不变,直到不可预测为止。当索引顺序与ORDER BY匹配时,ORDER BY不会花费太多。要对结果集进行排序,必须使用ORDER BY。如果您不使用order by,它将导致一个任意的顺序-这可能是您当前希望的顺序,但根本没有保证,并且可能在任何时候发生变化,例如,当mysql选择不同的执行计划时,这就是straight_join在这里的效果。