There is a field in the table:
pp enum('T','S','E','U','AC','AT','M','MA','BA','MC','BC','D') COLLATE utf8_unicode_ci DEFAULT NULL KEY pp (pp) If you do this:
select * from tbl_invoices where (pp is null or pp in ('S')); This shows that the key is used in the request:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: tbl_invoices type: ref_or_null possible_keys: pp key: pp key_len: 2 ref: const rows: 83064 Extra: Using index condition; Using where And if you do this:
select * from tbl_invoices where (pp is null or pp in ('S','D')); That key is no longer used:
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: tbl_invoices type: ALL possible_keys: pp key: NULL key_len: NULL ref: NULL rows: 321844 Extra: Using where Tell me how to remake the request, so that in the second case, the key is also used?
where. Those. I am interested in how many records withpp IS NULL,pp = 'S'andpp = 'D'. In general, this is normal behavior - sampling by index leads to random reading from the disk, and if almost all the records meet the condition, then the records will be scanned sequentially much faster. - BOPOHforce index (pp)or can we assume that the mysql optimizer always knows better than me? - Fyodor Ustinovforce index). If MyISAM is used, then it may make sense to makeANALYZE TABLEto recalculate the index cardinality (for the like, InnoDB does not seem to help) - BOPOH