English 中文(简体)
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.

In the question the poster states that the same query runs in 4 seconds in PHP MyAdmin, so the problem is not in MySql or the inability to use an index due to the %, but in Tomcat or the data access layer.

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签