Does the order of fields in a WHERE clause affect performance in MySQL?

后端 未结 3 872
礼貌的吻别
礼貌的吻别 2020-12-04 23:17

I have two indexed fields in a table - type and userid (individual indexes, not a composite).

types field values are very lim

3条回答
  •  青春惊慌失措
    2020-12-05 00:15

    Most query optimizers use the order in which conditions appear as a hint. If everything else is equal, they will follow that order.

    However, many things can override that:

    • the second field has an index and the first has not
    • there are statistics to suggest that field 2 is more selective
    • the second field is easier to search (varchar(max) vs int)

    So (and this is true for all SQL optimization questions) unless you observe a performance issue, it's better to optimize for clarity, not for (imagined) performance.

提交回复
热议问题