use*_*870 38 mysql indexing greatest-n-per-group
我有以下mySQL查询,完全正常.除了我需要添加"FORCE INDEX",我不确定我必须在哪里做到这一点.我尝试了几乎每个位置,并始终收到mySQL错误.我究竟做错了什么?
这是原始查询:
$sql_select_recent_items = $db->query("SELECT * FROM (SELECT owner_id, product_id, start_time, price, currency, name, closed, active, approved, deleted, creation_in_progress FROM db_products ORDER BY start_time DESC) as resultstable
WHERE resultstable.closed=0 AND resultstable.active=1 AND resultstable.approved=1 AND resultstable.deleted=0 AND resultstable.creation_in_progress=0
GROUP BY resultstable.owner_id
ORDER BY start_time DESC");
Run Code Online (Sandbox Code Playgroud)
查询以这种方式构造,以便我可以在"GROUP BY"之前执行"ORDER BY",以防您想知道.
我需要补充的是:
FORCE INDEX (products_start_time)
Run Code Online (Sandbox Code Playgroud)
我在几乎没有成功的地方试过它,这让我相信有一些我更缺失的东西更复杂?
Bil*_*win 60
索引提示的语法在此处记录:http:
//dev.mysql.com/doc/refman/5.6/en/index-hints.html
索引索引在表引用后右转:
SELECT * FROM (
SELECT owner_id, product_id, start_time, price, currency, name, closed,
active, approved, deleted, creation_in_progress FROM db_products
FORCE INDEX (products_start_time)
ORDER BY start_time DESC) as resultstable
WHERE resultstable.closed=0
AND resultstable.active=1
AND resultstable.approved=1
AND resultstable.deleted=0
AND resultstable.creation_in_progress=0
GROUP BY resultstable.owner_id
ORDER BY start_time DESC
Run Code Online (Sandbox Code Playgroud)
警告:
如果您ORDER BY之前使用GROUP BY每个owner_id获取最新条目,那么您将使用MySQL的非标准和未记录的行为来执行此操作.
无法保证它将继续在MySQL的未来版本中运行,并且查询可能是任何其他RDBMS中的错误.
搜索最大-n-per-group标记,以获得针对此类查询的更好解决方案的多种解释.
FORCE_INDEX 在 MySQL 8 之后将被弃用:
Thus, you should expect USE INDEX, FORCE INDEX, and IGNORE INDEX to be deprecated in
a future release of MySQL, and at some time thereafter to be removed altogether.
Run Code Online (Sandbox Code Playgroud)
https://dev.mysql.com/doc/refman/8.0/en/index-hints.html
对于 v8 ,您应该使用JOIN_INDEX, GROUP_INDEX, ORDER_INDEX, 和INDEX代替。
| 归档时间: |
|
| 查看次数: |
92059 次 |
| 最近记录: |