I've a linux VM with Mysql 5.7.19. Logging slow queries and log-queries-not-using-indexes
I noticed this query:
# Time: 2017-08-05T11:10:36.881359Z
# User@Host: root[root] @ localhost [127.0.0.1] Id: 2070
# Query_time: 0.000381 Lock_time: 0.000145 Rows_sent: 1 Rows_examined: 35
SET timestamp=1501931436;
SELECT * FROM `cliente` order by denominazione;
Because I set long_query_time = 5
, seems the query is logged because it doesn't use indexes. Explaning the query I've:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE cliente NULL ALL NULL NULL NULL NULL 35 100.00 Using filesort
This is my table ddl:
CREATE TABLE `cliente` (
`id` bigint(20) NOT NULL,
`creatoDa` varchar(255) DEFAULT NULL,
`dataInserimento` datetime DEFAULT NULL,
`dataUltimaModifica` datetime DEFAULT NULL,
`modificatoDa` varchar(255) DEFAULT NULL,
`ENTITY_UID` varchar(36) NOT NULL,
`version` int(11) DEFAULT NULL,
`attivo` tinyint(1) NOT NULL DEFAULT '1',
`denominazione` varchar(255) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`note` longtext,
`password` varchar(255) NOT NULL,
`username` varchar(255) NOT NULL,
`dataUltimoPing` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `cliente`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `UK_75j8vo25r1ws35vccamep965h` (`ENTITY_UID`),
ADD UNIQUE KEY `UK_sgwkt4eif8lb1e5miakwnu7q9` (`username`),
ADD KEY `idx_denominazione` (`denominazione`),
ADD KEY `idx_attivo` (`attivo`)
I'm wondering why Mysql is not using index to sorting results considering that I've a index <768 on denominazione (varchar(255)) field.
答案 0 :(得分:2)
MySQL optimizer is cost based and has detected a full table scan is the fastest way to retrieve the records with your limited number of rows/records. Instead of doing random disk i/o to retrieve the records that is much slower for these few existing records. Additional records will automatically cause the optimizer to take advantage of your index when appropriate.