MySQL: how to index an “OR” clause

后端 未结 2 1419
闹比i
闹比i 2020-12-02 23:32

I\'m executing the following query

SELECT COUNT(*)
FROM table
WHERE field1=\'value\' AND (field2 >= 1000 OR field3 >= 2000)

There is

2条回答
  •  有刺的猬
    2020-12-02 23:51

    I'm new here, so I can't comment on other people's posts, but this is related to the posts by David M. and soulmerge.

    The temporary table is not necessary. The UNION David M. suggested does not double count, as UNION implies a distinct (i.e. if a row exists in one half of the union, ignore it in the other). If you used UNION ALL, you would get two records.

    The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.

    http://dev.mysql.com/doc/refman/5.0/en/union.html

提交回复
热议问题