简体   繁体   English

MySql 并发查询慢

[英]MySql slow with concurrent queries

I'm running queries in parallel against a MySql database.我正在对MySql数据库并行运行查询。 Each query takes less than a second and another half a second to a second to fetch.每个查询都需要不到一秒和半秒到一秒的时间来获取。

This is acceptable for me.这对我来说是可以接受的。 But when I run 10 of these queries in parallel and then attempt another set in a different session everything slows down and a single query can take some 20 plus seconds.但是当我并行运行这些查询中的 10 个,然后在不同的 session 中尝试另一个集合时,一切都会变慢,单个查询可能需要 20 多秒。

My ORM is hibernate and I'm using C3P0 with <property name="hibernate.c3p0.max_size">20</property> .我的 ORM 是hibernate ,我正在使用带有<property name="hibernate.c3p0.max_size">20</property>C3P0 I'm sending the queries in parallel by using Java threads.我使用Java线程并行发送查询。 But I don't think these are related because the slowdown also happens when I run queries in MySql Workbench .但我不认为这些是相关的,因为当我在MySql Workbench中运行查询时也会发生减速。 So I'm assuming something in my MySql config is missing, or the machine is not fast enough.所以我假设我的 MySql 配置中的某些内容丢失了,或者机器速度不够快。

This is the query:这是查询:

select 
    *
FROM
    schema.table
where
    site = 'sitename' and (description like '% family %' or title like '% family %')
limit 100 offset 0;

How can I make this go faster when facing let's say 100 concurrent queries?当面对 100 个并发查询时,如何使这个 go 更快?

I'm guessing that this is slow because the where clause is doing a full text search on the description and title columns;我猜这很慢,因为 where 子句正在对描述和标题列进行全文搜索; this will require the database to look through the entire field on every record, and that's never going to scale.这将要求数据库查看每条记录的整个字段,而这永远不会扩展。

Each of those 10 concurrent queries must read the 1 million rows to fulfill the query.这 10 个并发查询中的每一个都必须读取 100 万行才能完成查询。 If you have a bottleneck anywhere in the system - disk i/o, memory, CPU - you may not hit that bottleneck with a single query, but you do hit it with 10 concurrent queries.如果您在系统中的任何地方遇到瓶颈 - 磁盘 i/o、memory、CPU - 您可能不会通过单个查询遇到瓶颈,但您确实会遇到 10 个并发查询。 You could use one of these tools to find out which bottleneck you're hitting.您可以使用其中一种工具来找出您遇到的瓶颈。

Most of the time, those bottlenecks (CPU, memory, disk) are too expensive to upgrade - especially if you need to scale to 100 concurrent queries.大多数时候,这些瓶颈(CPU、memory、磁盘)升级成本太高——尤其是当您需要扩展到 100 个并发查询时。 So it's better to optimize the query/ORM approach.所以最好优化查询/ORM 方法。

I'd consider using Hibernate's built-in free text capability here - it requires some additional configuration, but works MUCH better when looking for arbitrary strings in a textual field.我会考虑在这里使用 Hibernate 的内置自由文本功能- 它需要一些额外的配置,但在文本字段中查找任意字符串时效果更好。

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

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