A table can contain at most one matching record, which is read from the beginning of the query. Since there is only one record, the field values recorded by this row in the remaining optimization programs can be treated as a constant value. The const table query is very fast, because only one read is required! Const is used to compare a fixed value with a primary key or UNIQUE index. In the following queries, tbl_name is the const table:
SELECT * FROM tbl_name WHERE primary_key = 1;
SELECT * FROM tbl_name
WHERE primary_key_part1 = 1 AND primary_key_part2 = 2;
Eq_ref
A row of records is read from the table and associated with the records read from the previous table. Different from the const type, this is the best connection type. It is used for connecting all parts of the index and the index is of the primary key or UNIQUE type. Eq_ref can be used to retrieve fields when "=" is compared. The comparative values can be fixed values or expressions, and the fields in the table can be used in the expression. They are ready before reading the table. In the following examples, MySQL uses the eq_ref connection to process ref_table:
SELECT * FROM ref_table, other_table
WHERE ref_table.key_column = other_table.column;
SELECT * FROM ref_table, other_table
WHERE ref_table.key_column_part1 = other_table.column
AND ref_table.key_column_part2 = 1;
Ref
All records that match the search value in this table will be taken out and used together with the records obtained from the previous table. Ref is the leftmost prefix used by the Connection program to use the KEY, or the KEY is not a primary key or a UNIQUE index (in other words, the connection program cannot retrieve only one record based on the KEY value. When only a few matching records are queried based on the key value, this is a good connection type. Ref can also be used to compare fields by using the = operator. In the following examples, MySQL uses ref to process ref_table: