[英]PHP/SQL: framework query building vs string concatenation
Consider 2 ways of querying the database: 考虑两种查询数据库的方法:
With a framework (Yii): 使用框架(Yii):
$user = Yii::app()->db->createCommand()
->select('id, username, profile')
->from('tbl_user u')
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();
With string concatenation (separating individual parts of a SQL statement): 使用字符串连接(分隔SQL语句的各个部分):
$columns = "id,username,profile"; // or =implode(",",$column_array);
//you can always use string functions to wrap quotes around each columns/tables
$join = "INNER JOIN tbl_profile p ON u.id=p.user_id";
$restraint = "WHERE id=$id ";//$id cleaned with intval()
$query="SELECT $columns FROM tbl_user u {$restraint}{$join}";
//use PDO to execute query... and loop through records...
Example with string concatenation for pagination: 分页的字符串连接示例:
$records_per_page=20;
$offset = 0;
if (isset($_GET['p'])) $offset = intval($_GET['p'])*$records_per_page;
Squery="SELECT * FROM table LIMIT $offset,$records_per_page";
Which method has better performance? 哪种方法有更好的表现?
Use which is right for you and your project team. 使用适合您和您的项目团队。 Frameworks are written for a reason, so use them if it suits, but if it doesn't (and there are reasons they don't) then fall away. 框架是出于某种原因编写的,所以如果它适合使用它们,但如果它不适合(并且有理由它们没有),那么它就会消失。
I don't know Yii, but if you look at a lot of frameworks, all they do is build a string query from the parts at the end of the day, hopefully taking advantage of parametization but not always. 我不知道Yii,但如果你看一下很多框架,他们所做的只是在一天结束时从部分构建一个字符串查询,希望利用参数化但并非总是如此。 So, regarding speed, string concat is probably "fastest" - but you're unlikely to really see the difference with a stop watch (you could benchmark if you needed with 1000 queries, but other features such as better error checking or caching may unfairly slow or speed up hte results). 因此,关于速度,字符串concat可能是“最快的” - 但你不太可能真正看到秒表的差异(如果你需要1000个查询你可以进行基准测试,但其他功能,如更好的错误检查或缓存可能不公平减慢或加快结果)。
But one advantage frameworks have is they can add context-sensitive caching and know when you update table X that you query caches for A, D and F need to be deleted, but queries B, C and E are all good. 但是一个优势框架是他们可以添加上下文敏感的缓存,并且知道当你更新表X时,你需要删除查询A,D和F的缓存,但查询B,C和E都是好的。
You also have "easy to read" and "debug" and "functionality" to worry about. 您还需要担心“易于阅读”,“调试”和“功能”。 The top example is much easier to read, which is important in a shared project. 顶部示例更容易阅读,这在共享项目中很重要。
You also need to consider prepared statements - does the framework use them? 您还需要考虑预准备语句 - 框架是否使用它们? If so, does it allow you to re-use them (as opposed to merely using them for syntax purposes). 如果是这样,它是否允许您重复使用它们(而不是仅仅将它们用于语法目的)。
But can the framework do sub-selects? 但是框架可以进行子选择吗? Can it do parametization inside the "JOIN ON"? 可以在“JOIN ON”中进行参数化吗? If not, string concatination with PDO may be more appropriate. 如果没有,字符串与PDO的连接可能更合适。
It's not a hard and fast answer - but hopefully provides all the points you need to consider. 这不是一个艰难而快速的答案 - 但希望提供您需要考虑的所有要点。
Recommendation: use framework unless you really notice it being slow, using too much memory or there is some other good reason not to. 建议:使用框架除非你真的注意到它很慢,使用太多内存或者还有其他一些好的理由不这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.