Solved

Question regarding mysql index

Posted on 2014-02-14
2
477 Views
Last Modified: 2014-02-14
Hello,

I have a table with the following structure:

id (int, auto increment), text1, lang

I also have created an index on text1,lang.

When I run the following query:

select * from mytable where name like 'cre%' I get 2500 records back.

and when I run

select * from mytable where name like 'cre%' and lang='en' I get 25 records back.

However if I run:
explain select * from mytable where name like 'cre%' and lang='en' I see that 2500 records are scanned.

Is that correct, since I have created the index on those two columns? My assumption was that only 25 records should be scanned.

Thanks a lot!
0
Comment
Question by:infodigger
2 Comments
 
LVL 19

Accepted Solution

by:
regmigrant earned 500 total points
ID: 39858935
Are there separate indexes on Text1 and lang or a single multicolumn index?

 As I understand it a multi-column index will only be used if the where clause is against the left most column of the index - in your example the Lang part is not available to the optimizer if its the second column of the index - you may get a better result with separate indices on each.

This link shows some useful hints :
https://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39858957
What output do you get from your EXPLAIN statement
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.

791 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