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

前端 未结 8 632
长发绾君心
长发绾君心 2020-11-29 06:10

Say that I have a long, expensive query, packed with conditions, searching a large number of rows. I also have one particular condition, like a company id, that will limit t

8条回答
  •  日久生厌
    2020-11-29 06:55

    The order of columns in your where clause shouldn't really matter, since MySQL will optimize the query before executing it. But I suggest you read the chapter on Optimization in the MySQL reference manual, to get a basic idea on how to analyze queries and tables, and optimize them if necessary. Personally though, I would always try to put indexed fields before non-indexed fields, and order them according to the number of rows that they should return (most restrictive conditions first, least restrictive last).

提交回复
热议问题