Solved

Index not getting used in mysql query

Posted on 2016-09-19
2
47 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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