在团队中是否应统一使用 ORM?使用 SQL 语句有什么坏处?
如题。
团队规模为10人。编程环境为 PHP + Python。
个人觉得在大家都会 SQL,并且掌握一些 SQL 技巧时,统一使用 SQL 语句可以在以后性能调优时更直观。不知各位“过来人”有何高见。
另外在 Model 里有没有必要把 phpredis 的函数重新封装为 ORM ?感觉 Redis 本身就是为速度存在的,如果再在入口处加一些解析、判断、封装,是不是会有悖于 Redis 的主旨,而且涉及数据交互的 Model 在我们团队里仅由2人负责,编码规范化的问题应该不是问题。
回复内容:
如题。
团队规模为10人。编程环境为 PHP + Python。
个人觉得在大家都会 SQL,并且掌握一些 SQL 技巧时,统一使用 SQL 语句可以在以后性能调优时更直观。不知各位“过来人”有何高见。
另外在 Model 里有没有必要把 phpredis 的函数重新封装为 ORM ?感觉 Redis 本身就是为速度存在的,如果再在入口处加一些解析、判断、封装,是不是会有悖于 Redis 的主旨,而且涉及数据交互的 Model 在我们团队里仅由2人负责,编码规范化的问题应该不是问题。
SQL一个比较大的麻烦就是不限权(或者是限权不细)。一个SQL语句的书写失误,可能毁掉整个系统的所有数据。
因此甚至包括WordPress在内的,几乎所有的框架都不怎么提倡直接把SQL语句硬编码(hard code)在程序中,而是必须封装起来。
不要觉得只有两个人做就不必封装了——缺少规矩,人少也出事儿。
可以使用一些轻量级的ORM,好处就是:
1、开发快,很多细节封装起来了,比如数据库连接的使用和释放
2、面向对象的开发方式让代码更易理解和维护
3、完全不会限制你做SQL调优,比如类似mybatis这样轻量级的ORM,还是使用SQL访问数据
网上有的是AR类,随便搞一个用,不要直接写SQL,要是想调节性能你可以加一层logging专门记录慢查询和方法。
我见过很多人把redis封装成ORM的接口,为了使用方便,包裹一些判断和加载那都太正常了,我们可以按需要给redis的ORM、Cache、Count、Queue分别封装, 这处性能影响不大。最主要是加速开发以及可维护性。
使用SQL的好处是直观一点, 但坏处比好处更多,例如硬编码的维护性低、前端人员水平良莠不齐你不能保证每个人写的SQL都一样,最主要的是编写速度慢,我觉得数据库语句这种东西,前端开发人员甚至不该直接接触。
在项目中,更偏向于直接写SQL,而不是用ORM,对于Python之类的弱类型语言,更甚。 SQL表达能力已经很强,比较简单,易于上手。
程序员学习SQL,好处有:
1. SQL语句,可以在命令行(mysql) & 其它图形化工具 执行,一边修改,一边调试。
2. 在互联网上,SQL的文档很多,而ORM的文档偏少。有困难时,比较容易找到解答
3. SQL更通用,你可能学习了很多ORM,但不明白SQL,你能说自己懂ORM了么?
程序员,熟悉SQL,进而数据库,是对自己的要求。这可能是DBA的事,但也是你进步,增值的方法。
可以参考discuz的封装,另外对所有涉及到sql的更改都进行2人左右的review
http://www.cnblogs.com/needrunning/archive/2011/11/15/2250068.html
个人觉得
使用orm的好处
<code>1. 节约时间,指的是程序员的时间,如果题主不是做效率要求很高的东西,整个团队节约下来的时间是很客观的,非常喜欢一句话“宁花机器一分,不花程序员一秒”,题主选择了python,相信应个对这个体会比较深吧(生命苦短,我用python~) 2. 适合工程,对同一类需求,基本上不同人写出来的东西是一样的,sql有这种问题,如果一个人写了很复杂的东西,这个成员离职或怎样,这段代码就会变成野草(非常难看懂,非常痛苦),早晚会被之后的维护者重写的,orm的话代码逻辑比较清晰。 </code>
orm的坏处
<code>1. 效率,orm的话会存在一些效率的问题,毕竟编译的时候会生成一些别的代码,可能一个复杂的需求写4、5十行sql的效率 > orm的效率,不过实在是很蛋疼 2. 学习成本,很多人都是先学sql,然后接触orm,自然有学习成本的问题,而且使用orm就要符合orm的规范,有些高效的orm使用方式还是一种隐藏的附加技能,需要不断探索 </code>
综上
对于一个把程序员的时间看作第一位的团队(项目超期是很正常的事情),如果对程序的执行效率要求不是超高的话,是用orm绝对是让你轻松的一件事情。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.
