APIJSON icon indicating copy to clipboard operation
APIJSON copied to clipboard

【联表查询速度极慢】

Open efwevg opened this issue 1 year ago • 6 comments

Description

apijson版本:6.2.0 数据库:mysql 8.0.21 JDK/基础库: 1.8.0_17 OS/系统:win11 问题描述: 两表数据都在1600条左右、结果也是1600条左右,一对一关系,且增加redis对联表查询的速度差不多,求大佬解惑 附图: 微信图片_20240513155336

efwevg avatar May 13 '24 07:05 efwevg

image 但是将redis去除之后反而快很多 image

efwevg avatar May 13 '24 08:05 efwevg

APIJSON 性能是有测试过的,包括在腾讯的项目中实际表现也很好: https://github.com/Tencent/APIJSON/releases/tag/5.1.5

Redis 如果没部署(连接耗时,超时才有结果),或者不在本机部署(网络耗时),光连 Redis 查询就会很耗时。

你现在这种写法 1.首先引用赋值(外键关联)写法就错了 "id>": "/Srctions/router_id" 对应 WHERE id > '/Srctions/router_id' 结果可能是扫全表然后查不到值

应该改成 "id@": "/Srctions/router_id"

可以加 @explain: true 或者在控制台日志查看生成和执行的 SQL。

建议多看看 通用文档测试用例

2.其次如果写对了,那也是 N+1 次查询,大数据量下性能确实差,需要改成 JOIN,可以试试 APP JOIN 和 LEFT/INNER JOIN https://github.com/Tencent/APIJSON/issues/374

3.最后 Log.DEBUG = true(默认 false) 时,打印日志对性能会有明显影响,可以关掉后再测试

TommyLemon avatar May 18 '24 16:05 TommyLemon

1.如果写法改为"id@": "/Srctions/router_id" sql会执行多次,会和数据量无限叠加,速度更慢

efwevg avatar May 28 '24 06:05 efwevg

用 JOIN

TommyLemon avatar May 31 '24 16:05 TommyLemon

https://github.com/Tencent/APIJSON/issues/718#issuecomment-2142590824 使用join,副表查询也是一条条SQL执行,n+1问题依然存在,但是查询1000条数据时,会执行1001次SQL image

gxmanito avatar Nov 05 '24 10:11 gxmanito

@gxmanito https://github.com/Tencent/APIJSON/issues/769#issuecomment-2462499241

TommyLemon avatar Nov 07 '24 15:11 TommyLemon