[MDEV-32255] select does not use index when using different column Created: 2023-09-26  Updated: 2023-09-26  Resolved: 2023-09-26

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

Type: Bug Priority: Major
Reporter: Artur Tomasi Assignee: Sergei Golubchik
Resolution: Not a Bug Votes: 0
Labels: performance
Environment:

11.1.2-MariaDB-1:11.1.2+maria~ubu2204



 Description   

Version
11.1.2-MariaDB-1:11.1.2+maria~ubu2204
Table

CREATE TABLE "qms_occurrences" (
    "id" int(11) NOT NULL,
    "ref_created_by" int(11) NOT NULL,
    "dt_created" date NOT NULL,
    "title" varchar(160) NOT NULL,
    "sector" varchar(80) NOT NULL,
    "description" text NOT NULL,
    "reason" varchar(160) NOT NULL,
    "disposition" varchar(160) NOT NULL,
    "state" smallint(6) NOT NULL,
    "ref_accepted_by" int(11) NOT NULL,
    "ref_analised_by" int(11) NOT NULL,
    "dt_analyse_due" date DEFAULT NULL,
    "dt_analysed" date DEFAULT NULL,
    "ref_approved_by" int(11) NOT NULL,
    "ref_executed_by" int(11) NOT NULL,
    "execute_state" smallint(6) DEFAULT NULL,
    "dt_execute_estim_start" date DEFAULT NULL,
    "dt_execute_estim_end" date DEFAULT NULL,
    "dt_execute_real_start" date DEFAULT NULL,
    "dt_execute_real_end" date DEFAULT NULL,
    "ref_verified_by" int(11) NOT NULL,
    "archived" tinyint(4) NOT NULL,
    "ref_category" int(11) NOT NULL,
    "ref_type" int(11) NOT NULL,
    "serial" varchar(40) NOT NULL,
    "ref_actionplan_analysis" int(11) NOT NULL,
    "priority" smallint(6) NOT NULL,
    "ref_previous" int(11) NOT NULL,
    "restriction" int(11) NOT NULL,
    "workflow" smallint(6) NOT NULL,
    "ref_owner" int(11) NOT NULL,
    "dt_approve_due" date DEFAULT NULL,
    "dt_execute_due" date DEFAULT NULL,
    "ref_actionplan_disposition" int(11) NOT NULL,
    "dt_verify_due" date DEFAULT NULL,
    "dt_last_verification" date DEFAULT NULL,
    "context_count" int(11) NOT NULL,
    "initiative_count" int(11) NOT NULL,
    "ref_event_accepted" int(11) NOT NULL,
    "ref_event_approved" int(11) NOT NULL,
    "ref_event_executed" int(11) NOT NULL,
    "ref_event_verified" int(11) NOT NULL,
    "next_update" date DEFAULT NULL,
    "options" varchar(32) NOT NULL,
    "stage_current" smallint(6) NOT NULL,
    "stage_origin" smallint(6) NOT NULL,
    "dt_accept_due" date DEFAULT NULL,
    "analysis_family" int(11) NOT NULL,
    "analysis_source" int(11) DEFAULT NULL,
    PRIMARY KEY ("id"),
    KEY "qms_occurrences_category" ("ref_category"),
    KEY "qms_occurrences_restriction" ("restriction"),
    KEY "qms_occurrences_state" ("state"),
    KEY "qms_occurrences_type" ("ref_type"),
    KEY "qms_occurrences_disposition" ("ref_actionplan_disposition"),
    KEY "qms_occurrences_analysis" ("ref_actionplan_analysis"),
    KEY "qms_occurrences_created" ("ref_created_by"),
    KEY "qms_occurrences_previous" ("ref_previous"),
    KEY "qms_occurrences_accepted" ("ref_accepted_by"),
    KEY "qms_occurrences_analised" ("ref_analised_by"),
    KEY "qms_occurrences_approved" ("ref_approved_by"),
    KEY "qms_occurrences_executed" ("ref_executed_by"),
    KEY "qms_occurrences_verified" ("ref_verified_by"),
    KEY "qms_occurrences_owner" ("ref_owner"),
    KEY "qms_occurrences_evaccepted" ("ref_event_accepted"),
    KEY "qms_occurrences_evapproved" ("ref_event_approved"),
    KEY "qms_occurrences_evexecuted" ("ref_event_executed"),
    KEY "qms_occurrences_evverified" ("ref_event_verified"),
    KEY "qms_occurrences_serial" ("serial"),
    KEY "qms_occurrences_workflow" ("workflow"),
    KEY "qms_occurrences_created_on" ("dt_created"),
    KEY "qms_occurrences_archived" ("archived"),
    KEY "qms_occurrences_dt_analysed" ("dt_analysed"),
    KEY "qms_occurrences_current" ("stage_current"),
    KEY "qms_occurrences_origin" ("stage_origin"),
    KEY "qms_categories_state" ("state"),
    KEY "ix_qms_occu_stage_origin" ("stage_origin"),
    KEY "ix_qms_occu_stage_current" ("stage_current"),
    KEY "ix_qms_occurrences_state_id" ("state", "id")
)

Query with column indexed isnt using index, that situation has performance issue:

select O.state from qms_occurrences O where O.serial like '%TSI-85496%'

id select_type table type possible_keys key  key_len ref  rows   filtered Extra      
 -- ----------- ----- ---- ------------- ---- ------- ---- ------ -------- -----------
 1  SIMPLE      O     ALL  NULL          NULL NULL    NULL 189266 100.00   Using where

select O.serial from qms_occurrences O where O.serial like '%TSI-85496%'

id select_type table type  possible_keys key                    key_len ref  rows   filtered Extra                   
-- ----------- ----- ----- ------------- ---------------------- ------- ---- ------ -------- ------------------------
1  SIMPLE      O     index NULL          qms_occurrences_serial 42      NULL 189266 100.00   Using where; Using index



 Comments   
Comment by Sergei Golubchik [ 2023-09-26 ]

This is normal. "Using index" means that the query can be resolved by using only index, there is no need to read the record data at all. Because you're looking through the index on the serial column and immediately print all matching values.

In the first case, though, after the server finds a matching serial value, it has to go to retrieve the record with this value and print the value of state from the record. Because the value of state is not present in the qms_occurrences_serial index.

Note that in both cases the value of "rows" is 189266 — the server reads the whole table, either through the qms_occurrences_serial index or through the primary key.

Comment by Artur Tomasi [ 2023-09-26 ]

but, I've only change the column of select, my table and my where are the same on the both queries. why the second one its instantly and the first took 10s?
This situations that happens is very stranges, because I just wana see one column diferent.

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