Solved

Index not getting used in mysql query

Posted on 2016-09-19
2
48 Views
Last Modified: 2016-09-27
Hi,
I have the following table :
CREATE TABLE `note` (
  `id` char(36) NOT NULL,
  `body` text CHARACTER SET utf8mb4,
  `title` varchar(255) CHARACTER SET utf8mb4 DEFAULT NULL,
  `preview` text CHARACTER SET utf8mb4,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `user` varchar(34) DEFAULT NULL,
  `conversation` varchar(34) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Open in new window


And I performed the following query :
explain select * from note where user='u:ynul5luymmnlys0u' and conversation is not null order by updated desc;

Open in new window

In the output the key column had value NULL
which is expected as there is not index on user column.
So i added an index :
create index user_idx on note (user);

And performed the same query the output still had NULL in key column ??
Although in possible_keys it did show user_idx. Dont know why it didnt use the index on user column.

Please help me in understanding the above behavior.

Thanks
0
Comment
Question by:Rohit Bajaj
2 Comments
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 350 total points
ID: 41804901
Hi!

See my comment here.

It depends on your data and number of rows in the table what the optimizer does. If your table statistics shows that one user has more than 25% of conversation values then this index would not be enough to filter out the data fast enough and therefore the optimzer chooses tablescan.

Regards,
     Tomas Helgi
0
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 150 total points
ID: 41804959
You can also force your index on your query if you are sure.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
join tables 4 57
insert row field data graphically 4 32
Coldfusion/Mysql page error related to dynamic table creation. 9 43
check mysql insert 12 38
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question