Code Monkey home page Code Monkey logo

Comments (2)

hellokaton avatar hellokaton commented on June 3, 2024

提的非常好。第一点这块的性能我没有测试过,是把之前用 MyBatis 框架的分页 count 思路拿过来直接用了,你可以把这块的测试截图发一下;第二点属于细节上的优化,可以 fix;第三点是个大问题,我应该再考虑下不足之处,你说到变成 SQL 的 in ? 是 join 吗?

from anima.

ydq avatar ydq commented on June 3, 2024

1.目前已知mysql是有性能问题的(粗略测试相差10倍),sqlserver可能做了优化或者测试数据样本不足没发现什么影响
2.细节问题
3.eg:
数据库有 users 表 和 depts表,Java模型上 User 和 Dept 做了双向的关联
查询User列表时做了一个Join操作:

假定查询到了5条users记录
此时会根据User列表循环5次查询depts表,并一一进行封装。

改进建议如下:首先根据查询到的User列表,将User对象和Dept对象做Join关联的字段假定是deptId全部提取到一个set集合,这样Join的字段肯定是少于等于User列表的size的,如下例子假定查询到的users表结果集如下:

uid dept_id
1 1
2 1
3 2
4 3
5 3
  • 按原先的Join之后,会发送5条 select * from depts where id = ? | 参数依次 1,1,2,3,3 的语句,数据库会连接5次,其中有两条还重复(id=1和3的会被重复查询一次)
  • 如果使用in优化后 只需要发送一条 select * from detps where id in (?,?,?) | 参数组装好的set集合 [1,2,3],数据库只用连接1次,大大减少了连接数据库的次数,同时查询的结果也会少两条
    查询回来后可以直接将Dept使用lambda的group按id分组的到一个map
//Group后的伪Map模型
{
        "1":"dept[{id:1}]",
        "2":"dept[{id:2}]",
        "3":"dept[{id:3}]"
}

然后程序中再循环一次User列表,依次按关联的detpId匹配设置dept对象

from anima.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.