How do I add indices to MySQL tables?

后端 未结 7 1837
北荒
北荒 2020-11-22 13:54

I\'ve got a very large MySQL table with about 150,000 rows of data. Currently, when I try and run

SELECT * FROM table WHERE id = \'1\';

the

7条回答
  •  无人及你
    2020-11-22 14:11

    It's worth noting that multiple field indexes can drastically improve your query performance. So in the above example we assume ProductID is the only field to lookup but were the query to say ProductID = 1 AND Category = 7 then a multiple column index helps. This is achieved with the following:

    ALTER TABLE `table` ADD INDEX `index_name` (`col1`,`col2`)
    

    Additionally the index should match the order of the query fields. In my extended example the index should be (ProductID,Category) not the other way around.

提交回复
热议问题