[MDEV-15445] Server selecting the wrong index Created: 2018-03-01  Updated: 2019-10-17  Resolved: 2019-01-28

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.2.12
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Oren Bissick (Inactive) Assignee: Alice Sherepa
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Galera Cluster running on CentOS 7 virtual machines.


Attachments: File analyze.csv     File explain.csv     File server.cnf    

 Description   

I created an compound index with use_id and asd_date_start. Theres also a index for use_id, which i removed later on since its redundant. The issue is that after i created the compound index i did a explain and the result was that it would use the compound index. When the query ran it did not use the compound index, it used the single index on use_id.

Indices:
KEY `pro_id` (`use_id`),
KEY `agent_state_detail_idx1` (`asd_date_start`) USING BTREE,
KEY `idx_agent_state_detail_asd_date_end` (`asd_date_end`),
KEY `idx_agent_state_detail_use_id_asd_date_start` (`use_id`,`asd_date_start`)

Query:
select agentstate0_.asd_id as asd_id1_1_, agentstate0_.asd_created_date as asd_crea2_1_, agentstate0_.asd_duration as asd_dura3_1_, agentstate0_.asd_duration_pending as asd_dura4_1_, agentstate0_.asd_date_end as asd_date5_1_, agentstate0_.asd_event_type as asd_even6_1_, agentstate0_.iwo_id as iwo_id12_1_, agentstate0_.iwo_id_pending as iwo_id_13_1_, agentstate0_.asd_need_updated as asd_need7_1_, agentstate0_.asd_place as asd_plac8_1_, agentstate0_.asd_reason_code as asd_reas9_1_, agentstate0_.asd_reason_code_description as asd_rea10_1_, agentstate0_.asd_date_start as asd_dat11_1_, agentstate0_.use_id as use_id14_1_ from agent_state_detail agentstate0_ where agentstate0_.use_id='01960da6-3c27-4c3f-aab1-6f25e1ce1056' and (agentstate0_.asd_date_start between TIMESTAMP'2018-02-27 08:55:00' and TIMESTAMP'2018-02-27 13:30:59.999000' or agentstate0_.asd_date_end between TIMESTAMP'2018-02-27 08:55:00' and TIMESTAMP'2018-02-27 13:30:59.999000' or (TIMESTAMP'2018-02-27 08:55:00' between agentstate0_.asd_date_start and agentstate0_.asd_date_end) and (TIMESTAMP'2018-02-27 13:30:59.999000' between agentstate0_.asd_date_start and agentstate0_.asd_date_end)) and (agentstate0_.iwo_id is null or agentstate0_.asd_duration_pending>0) order by agentstate0_.asd_date_start ASC



 Comments   
Comment by Alice Sherepa [ 2018-03-06 ]

Please add output of:

SHOW CREATE TABLE agent_state_detail;
EXPLAIN EXTENDED .. #query
ANALYZE FORMAT=JSON ..#query

and attach your .cnf file(s)

Comment by Alice Sherepa [ 2018-03-08 ]

Hi Oren,
in the attached outputs of analyze and explain, optimizer using the compound index in both cases.
It depends on table statistics, such as cardinality of keys, what indexes optimizer will choose,
if they are equally good, then they are used in order, they were defined in the table.
So my guess is that pro_id was defined before index_5. But the important here is that index_5 is used in ANALYZE,
when the query is really executed and then produced explain output.
And yes, pro_id is redundant.

Generated at Thu Feb 08 08:21:21 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.