I have the following query:
SELECT t.id FROM account_transaction t JOIN transaction_code tc ON t.transaction_code_id = tc.id JOIN account a ON t.account_number = a.account_number GROUP BY tc.id
When I do an EXPLAIN
the first row shows, among other things, this:
table: t type: ALL possible_keys: account_id,transaction_code_id,account_transaction_transaction_code_id,account_transaction_account_number key: NULL rows: 465663
Why is key NULL?
possible_keys : shows the keys that can be used by MySQL to find rows from the table. These keys may or may not be used in practice. keys : indicates the actual index used by MySQL. MySQL always looks for an optimal key that can be used for the query.
MySQL with examples in great detail. The 5 keys supported by MySQL are Primary Key, Unique Key, Candidate Key, Foreign Key, and Super Key. You learned how to create a table to define and also how to alter the table for primary and unique keys. You also learned how to add and delete a foreign key in MySQL.
Pre-requisites: You have to create a database table with data in a MySQL database to check the Force Index feature of MySQL. Open the terminal and connect with the MySQL server by executing the following command. Run the following command to create a database named test_db.
The filtered column indicates an estimated percentage of table rows that will be filtered by the table condition. That is, rows shows the estimated number of rows examined and rows × filtered / 100 shows the number of rows that will be joined with previous tables.
Another issue you may be encountering is a data type mis-match. For example, if your column is a string data type (CHAR, for ex), and your query is not quoting a number, then MySQL won't use the index.
SELECT * FROM tbl WHERE col = 12345; # No index SELECT * FROM tbl WHERE col = '12345'; # Index
Source: Just fought this same issue today, and learned the hard way on MySQL 5.1. :)
Edit: Additional information to verify this:
mysql> desc das_table \G *************************** 1. row *************************** Field: das_column Type: varchar(32) Null: NO Key: PRI Default: Extra: *************************** 2. row *************************** [SNIP!] mysql> explain select * from das_table where das_column = 189017 \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: das_column type: ALL possible_keys: PRIMARY key: NULL key_len: NULL ref: NULL rows: 874282 Extra: Using where 1 row in set (0.00 sec) mysql> explain select * from das_table where das_column = '189017' \G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: das_column type: const possible_keys: PRIMARY key: PRIMARY key_len: 34 ref: const rows: 1 Extra: 1 row in set (0.00 sec)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With