FORCE INDEX mySQL ......我把它放在哪里? [英] FORCE INDEX mySQL ...where do I put it?

查看:516
本文介绍了FORCE INDEX mySQL ......我把它放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下mySQL查询,完全正常。除了我需要添加FORCE INDEX,我不确定我必须在哪里做到这一点。我尝试了几乎每个位置,并始终收到mySQL错误。我做错了什么?

I have the following mySQL query that works perfectly fine. Except that I need to add a "FORCE INDEX" and I'm unsure on where I have to do this. I tried just about every location and always receive a mySQL error. What am I doing wrong?

这是原始查询:

$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");

查询以这种方式构造,以便我可以在GROUP BY之前执行ORDER BY ,万一你想知道。

The query is constructed this way so that I can do the "ORDER BY" before the "GROUP BY", in case you're wondering.

我需要补充的是:

FORCE INDEX (products_start_time)

我在几乎所有地方都试过没有成功,这导致我相信我缺少一些更复杂的东西?

I tried it just about everywhere without success, which leads me to believe that there's something more complex that I'm missing?

推荐答案

索引提示的语法记录在这里:

http://dev.mysql.com/doc/refman/5.6 /en/index-hints.html

The syntax for index hints is documented here:
http://dev.mysql.com/doc/refman/5.6/en/index-hints.html

索引索引紧跟在表格引用之后:

Index index go right after the table reference:

  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






警告:

如果您使用 ORDER BY GROUP BY 之前获取每个owner_id的最新条目,你使用MySQL的非标准和未记录的行为来做到这一点。

If you're using ORDER BY before GROUP BY to get the latest entry per owner_id, you're using a nonstandard and undocumented behavior of MySQL to do that.

无法保证它将继续在MySQL的未来版本中运行,并且查询可能是任何其他RDBMS中的错误。

There's no guarantee that it'll continue to work in future versions of MySQL, and the query is likely to be an error in any other RDBMS.

搜索 maximum-n-per-group 标签,用于解释此类查询的更好解决方案。

Search the greatest-n-per-group tag for many explanations of better solutions for this type of query.

这篇关于FORCE INDEX mySQL ......我把它放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆