In MySQL, efficiently retrieving data from expansive tables is paramount. This article addresses a query performance impediment encountered while accessing a non-indexed field in a table with 150,000 rows.
The 'id' field, being the primary index, yields rapid queries. However, searching by the unindexed 'product_id' field resulted in sluggish performance. An EXPLAIN query confirmed that MySQL failed to utilize the recently created 'product_id_index'.
To resolve this issue, the following command can be executed:
ALTER TABLE `table` ADD INDEX `product_id_index` (`product_id`)
Furthermore, MySQL can encounter suboptimal performance when comparing integers to strings. Ensuring that both fields, 'id' and 'product_id', conform to appropriate data types, will enhance query efficiency.
The above is the detailed content of How Can I Speed Up MySQL Queries on Large Tables by Using Indexes?. For more information, please follow other related articles on the PHP Chinese website!