Given a 联合索引in mySql, will a successful query using it have type const, const when we…
- User
Given a 联合索引in mySql, will a successful query using it have type const, const when we use explain?
- Assistant
Given a 联合索引in mySql, will a successful query using it have type const, const when we use explain?
Not necessarily. In MySQL, the type column in the EXPLAIN output indicates how MySQL accesses the table. The value const means that MySQL has found a constant row (at most one matching row) and can treat it as a constant during query execution. This typically occurs when you are querying a table using a unique index or primary key with constant values.
Composite (联合) Indexes:
A composite index is an index on multiple columns. When you have a composite index, whether the type in EXPLAIN shows as const depends on several factors:
Unique Index: If the composite index is declared as a unique index (or is the primary key), and your query specifies constant values for all columns in the index, then MySQL knows that there can be at most one matching row. In this case, the type will typically be const.
-- Assuming a table 'users' with a unique composite index on (first_name, last_name)
EXPLAIN SELECT * FROM users WHERE first_name = 'John' AND last_name = 'Doe';
The type would be const because the composite index ensures uniqueness.
Non-Unique Index: If the composite index is not unique, even if you specify constants for all indexed columns, there might be multiple rows that match. In such cases, the type is likely to be ref or range.
-- Assuming a table 'orders' with a non-unique composite index on (customer_id, order_date)
EXPLAIN SELECT * FROM orders WHERE customer_id = 123 AND order_date = '2023-10-01';
The type would be ref because multiple orders could match the given customer and date.
Partial Column Match: If your query does not specify all columns in the composite index with constants, or uses ranges (like BETWEEN, <, >, etc.), the type may be range or index, depending on how MySQL optimizes the query.
EXPLAIN SELECT * FROM users WHERE first_name = 'John';
Here, only one part of the composite index is used, so the type might be range or ref.
Summary:
type = const: When there is a unique composite index and the query specifies constants for all indexed columns, resulting in at most one matching row.type values: When the composite index is not unique, not all indexed columns are specified, or multiple rows can match, the type will be ref, range, etc.Conclusion:
A successful query using a composite index will not always have type = const in the EXPLAIN output. It depends on the uniqueness of the index and how the query matches against the indexed columns.
References:
Note: Always analyze your specific query and table schema with EXPLAIN to understand how MySQL executes it.
Answer: Not necessarily; only if a unique composite index uniquely matches the query will EXPLAIN show type ‘const’—otherwise, using a composite index may result in other types like ‘ref’ or ‘range’.