管理和维护原始SQL查询与ORM并存的最佳实践

huangapple go评论67阅读模式
英文:

Best Practices for Managing and Maintaining Raw SQL Queries in Codebase Alongside an ORM

问题

My company is currently using an Object-Relational Mapping (ORM) system for handling database operations. However, I've noticed that for some complex queries, fetching a small amount of data (like 20 rows) results in a surprisingly high number of queries (over 80) due to the ORM loading related tables separately instead of using JOIN operations.

To address this, I suggested implementing the Repository pattern. Under this approach, we would use raw SQL for complex queries, while still utilizing the ORM for standard CRUD operations. However, my team raised concerns that incorporating raw SQL could lead to maintenance issues down the line.

I argued that potential maintenance difficulties could be mitigated with the appropriate measures, such as implementing comprehensive unit testing for our SQL queries. However, they remained hesitant, with their primary counter-argument being that our current ORM-based setup is adequately serving our needs as of now (given that we are not experiencing high loads).

I'm reaching out to the community to gain a broader perspective on this issue. How do you manage raw SQL in your codebase alongside an ORM? What strategies do you employ to ensure maintainability? Is unit testing sufficient, or do you have additional documentation or other best practices to share? I'd greatly appreciate any insights you can provide.

英文:

My company is currently using an Object-Relational Mapping (ORM) system for handling database operations. However, I've noticed that for some complex queries, fetching a small amount of data (like 20 rows) results in a surprisingly high number of queries (over 80) due to the ORM loading related tables separately instead of using JOIN operations.

To address this, I suggested implementing the Repository pattern. Under this approach, we would use raw SQL for complex queries, while still utilizing the ORM for standard CRUD operations. However, my team raised concerns that incorporating raw SQL could lead to maintenance issues down the line.

I argued that potential maintenance difficulties could be mitigated with the appropriate measures, such as implementing comprehensive unit testing for our SQL queries. However, they remained hesitant, with their primary counter-argument being that our current ORM-based setup is adequately serving our needs as of now (given that we are not experiencing high loads).

I'm reaching out to the community to gain a broader perspective on this issue. How do you manage raw SQL in your codebase alongside an ORM? What strategies do you employ to ensure maintainability? Is unit testing sufficient, or do you have additional documentation or other best practices to share? I'd greatly appreciate any insights you can provide.

答案1

得分: 1

有时ORM生成的SQL查询效率非常低下,而仓储模式允许将SQL查询与其他层分开,通过在其他层之间分配职责来实现。将原始SQL查询存储在单独的层中,我们可以获得以下好处:

  • 由于关注点分离的严格划分,更容易维护SQL查询。
  • 调整原始SQL查询比ORM生成的SQL查询更容易。
  • 原始SQL查询通常更易读且更简洁。
英文:

Sometimes ORMs generate highly inefficient SQL queries and repository pattern allows to separate SQL queries from other layers by dividing responsibilities among other layers. Having stored raw SQL queries in a separate layer, we have the following benefits:

  • the maintaining of sql queries is easier because of great separation of concerns.

  • it is easier to tune raw sql queries than SQL query generated by ORM

  • raw SQL queries are usually more readable and less verbose

huangapple
  • 本文由 发表于 2023年6月15日 12:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定