如何在我的应用程序中使用Explain找到Mysql查询的缓慢



我开发了一个Spring Boot应用程序,并使用MySql存储值。当我使用API存储值时,它非常好,但当我使用1000的倍数(1K、10K、100K等(存储行时,它需要很长时间才能完成。我如何才能找到缺少时间存储数据的地方。

MySql插入查询插入一行所花费的时间较长。如何发现Mysql查询的缓慢?

我在logback.xml中添加了以下行,

<Logger name="org.hibernate.type.descriptor.sql" level="trace"/> 

但它为相同的值提供了两次绑定参数,这是一个问题吗?

select
schema0_.id as id1_0_,
schema0_.active as active2_0_,
schema0_.body as body3_0_,
schema0_.created_at as created_4_0_,
schema0_.created_by as created_5_0_,
schema0_.name as name6_0_,
schema0_.updated_at as updated_7_0_,
schema0_.version as version8_0_ 
from
my_schema schema0_ 
where
schema0_.name=? 
and schema0_.version=? 
and schema0_.active=1
binding parameter [1] as [VARCHAR] - [xxxxx]
binding parameter [1] as [VARCHAR] - [xxxxx]
binding parameter [2] as [VARCHAR] - [1.0]
binding parameter [2] as [VARCHAR] - [1.0]
extracted value ([id1_0_] : [BIGINT]) - [1]
extracted value ([id1_0_] : [BIGINT]) - [1]

如果您的my_schema表非常大,并且MySQL选择的执行计划是全表扫描,那么此查询可能很耗时。此外,插入物可能同时很快。

通过详细的Hibernate输出,您的查询可能会受益于以下复合索引:

CREATE INDEX idx ON my_schema (name, version, active);

如果使用此索引,MySQL将有可能避免在评估查询时检查许多记录。您可以尝试直接在MySQL上添加此索引,然后再次测试Hibernate的性能。

最新更新