MySql SELECT索引的好处

Are there significant benefits between the two SQL select ?

SELECT * FROM table USE INDEX (cod_clie) WHERE cod_clie = "example" AND pwd_clie = "example" LIMIT 1;

SELECT * FROM table WHERE cod_clie = "example" AND pwd_clie = "example" LIMIT 1;

cod_clie is an INDEX field, of course!

Usually not, since mysql should use the index. To be 100% sure just append explain to your queries and compare the output.

EXPLAIN SELECT * FROM table USE INDEX (cod_clie) WHERE cod_clie = 'example' AND pwd_clie = 'example' LIMIT 1;
EXPLAIN SELECT * FROM table WHERE cod_clie = 'example' AND pwd_clie = 'example' LIMIT 1;

The index is here to speed up your SQL search. https://dev.mysql.com/doc/refman/5.7/en/mysql-indexes.html

If you don't have any index, SQL will go trough the whole table when you're looking for a specific value (WHERE cod_clie = "example").

If you have big databases with a lot of data you'll realize a quite big difference in performance time.

SQL indexes enhance performance of SQL select query through arranging values of indexed field in specific sort order which thereby minimizes number of rows in a table to be scanned until matching row is found which contains filter value in that column.

Usually indexes are applied on fields used as filters in SQL select query.

By default query analyzer find best index to improve query performance. But due to many manual indexing or heavily modified tables (INSERT or DELETE for example), over time the key distributions (Cardinality) diverge from the true state.

If an Optimizer that picks the wrong plan can cause severe performance issues. This means that your server can hit a performance problem at the most unexpected time.

Lucky for us, we can force the Optimizer to pick a particular plan (index) with the 'USE INDEX' hint.