Solved

Index not getting used in mysql query

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

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 17

Assisted Solution

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now