FORCE INDEX mySQL …where do I put it?

半世苍凉 提交于 2019-12-03 00:59:11
Bill Karwin

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

WARNING:

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.

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.

Search the tag for many explanations of better solutions for this type of query.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!