简体   繁体   中英

MySQL like query runs extremly slow for 5000 records table

I have this issue on our production server. The application stack is,

  • Java Web App on Tomcat 6.0.18
  • iBatis data access layer
  • MySQL 5.0 database
  • CentOS

The system is deployed on virtual server having around 256 MB memory.

Real problem:

The query like,

select * from customer

executes in around 10 seconds however if the following query is executed,

select * from customer where code like '%a%'

right after executing the above query, the system goes into indefinite processing and ultimately forces Tomcat to restart !.

Table statistics: - No. of records : 5000 - Primary Key : code

The same query PHP MyAdmin executes in around 4 seconds.

Do you think it could be MySQL problem ? Any idea to debug this. I am right now enabling the detailed logs and will keep updating this question with my findings but would appreciate your db insights.

I recently encountered a similar issue with MySQL in one of my production systems.

As a commenter noted above, the issue is the wildcard searching on the text field, and in particular the leading % in the search.

We dropped the leading % and reduced time take for a search query by several orders of magnitude (from a server grinding 60seconds+ to "no time at all").

Alternatives would be to use a Full-Text index or a system like Lucene for searching.

It seems your MySQL server is not setup correctly, taking 10 seconds for 5000 records should not be acceptable.

How are you accessing your db, via java code? In that case please give your high level logic here, maybe you are not efficiently reusing the db handlers.

The % in the beginning of the search is causing the table to never use an index. The % is a wildcard so it has to go through every single record in the database. Combine this with the fact that tomcat is running as well as a MySQL is running makes it even worse. There is not a lot of room to put an index in memory to read from.

You need to up the amount of memory on that box to let MySQL create the necessary memory caches it needs and room to allow the queries to work fast.

在问题中,发帖人指出,同一查询在PHP MyAdmin中将在4秒钟内运行,因此问题不在于MySql或由于%而导致无法使用索引,而是在Tomcat或数据访问层中。

Have you tried creating an autoincrement primary field, and making code a separate unique index.

I've had issues with text as primary key being very slow in certain environments before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM