A causa di questa impostazione:Perché MySQL non utilizza gli indici nella clausola WHERE IF?
mysql> show global variables like '%indexes';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| log_queries_not_using_indexes | ON |
+-------------------------------+-------+
Le query lente log continuare a ricevere:
# Time: 120607 16:58:30
# [email protected]: xbtit[xbtit] @ [123.30.53.244]
# Query_time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 16006
SELECT * FROM xbtit_files WHERE IF(soha_id is null OR soha_id = '', info_hash, soha_id)='6d63dd4ab199190b531752067414d4d6e6568f90';
Cercando di spiegare questa query:
mysql> EXPLAIN SELECT * FROM xbtit_files WHERE IF(soha_id is null OR soha_id = '', info_hash, soha_id)='6d63dd4ab199190b531752067414d4d6e6568f90';
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | xbtit_files | ALL | NULL | NULL | NULL | NULL | 16006 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
Ciò che mi ha sorpreso è il motivo per MySQL non utilizza gli indici :
mysql> show index from xbtit_files;
+-------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| xbtit_files | 0 | PRIMARY | 1 | info_hash | A | 16006 | NULL | NULL | | BTREE | |
| xbtit_files | 1 | filename | 1 | filename | A | 16006 | NULL | NULL | YES | BTREE | |
| xbtit_files | 1 | category | 1 | category | A | 1 | NULL | NULL | | BTREE | |
| xbtit_files | 1 | uploader | 1 | uploader | A | 16 | NULL | NULL | | BTREE | |
| xbtit_files | 1 | bin_hash | 1 | bin_hash | A | 16006 | 20 | NULL | | BTREE | |
| xbtit_files | 1 | ix_sohaid | 1 | soha_id | A | 16006 | NULL | NULL | YES | BTREE | |
+-------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
FORCE INDEX
anche non funziona:
mysql> EXPLAIN SELECT * FROM xbtit_files force index (PRIMARY) WHERE IF(soha_id is null OR soha_id = '', info_hash, soha_id)='6d63dd4ab199190b531752067414d4d6e6568f90';
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | xbtit_files | ALL | NULL | NULL | NULL | NULL | 16006 | Using where |
+----+-------------+-------------+------+---------------+------+---------+------+-------+-------------+
devo dividere questo query in 2 operazioni?
Quante righe restituisce questa query? –
Vedere il valore 'Rows_sent' nel log delle query lente. – quanta
È possibile eliminare la funzione if, ad esempio SELECT * FROM xbtit_files WHERE soha_id = '6d63dd4ab199190b531752067414d4d6e6568f90' O info_hash = '6d63dd4ab199190b531752067414d4d6e6568f90'? –