简体   繁体   English

MySQL之类的查询对于5000条记录表的运行极其缓慢

[英]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 Tomcat 6.0.18上的Java Web App
  • iBatis data access layer iBatis数据访问层
  • MySQL 5.0 database MySQL 5.0数据库
  • CentOS CentOS的

The system is deployed on virtual server having around 256 MB memory. 该系统部署在具有约256 MB内存的虚拟服务器上。

Real problem: 实际问题:

The query like, 查询像

select * from customer

executes in around 10 seconds however if the following query is executed, 在大约10秒内执行,但是如果执行以下查询,

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 !. 在执行上述查询后,系统立即进入不确定处理,并最终迫使Tomcat重新启动!。

Table statistics: - No. of records : 5000 - Primary Key : code 表统计信息: -记录数:5000-主键:代码

The same query PHP MyAdmin executes in around 4 seconds. 同一查询PHP MyAdmin大约在4秒钟内执行。

Do you think it could be MySQL problem ? 您认为这可能是MySQL问题吗? 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. 我最近在一个生产系统中遇到了与MySQL类似的问题。

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"). 我们降低了前导的%并将搜索查询所花费的时间减少了几个数量级(从服务器耗时60秒以上到“根本没有时间”)。

Alternatives would be to use a Full-Text index or a system like Lucene for searching. 替代方法是使用全文索引或像Lucene这样的系统进行搜索。

It seems your MySQL server is not setup correctly, taking 10 seconds for 5000 records should not be acceptable. 看来您的MySQL服务器设置不正确,花费10秒读取5000条记录应该是不可接受的。

How are you accessing your db, via java code? 您如何通过Java代码访问数据库? In that case please give your high level logic here, maybe you are not efficiently reusing the db handlers. 在这种情况下,请在此处提供高级逻辑,也许您没有有效地重用db处理程序。

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. 再加上tomcat和MySQL都在运行,这使情况变得更糟。 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. 您需要在该框上增加内存量,以使MySQL创建所需的必要内存缓存,并为允许查询快速运行提供空间。

在问题中,发帖人指出,同一查询在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. 以前,在某些环境中,文本作为主键的速度非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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