方法一:
sql = "select {xxx} from student A,class B where A.class_id=B.id and A.id in {xxxx}"
res = excute(sql)
方法二:
sql = "select * from student where id in {xxxx}"
res = excute(sql)
foreach(res as v){
sql2 = "select {xxx} from class where id = v[class_id]";
res2 = excute(sql2)
res[xxx] = res2[xxx]
}
很初级的问题,不过还是问一下
我之前对这种情况都是用方法一的,觉得没商量;
但最近发现有人用第二种方法,内部系统,访问量小,所以也没啥影响,但他说第一种会增加mysql压力,可能会造成mysql故障,我就怀有迟疑态度了。
请比较熟悉的人帮我分析一下,谢谢
这种问题的原则基本上就是如果能很简单地用 SQL 算,就在数据库算,然后缓存结果。
所以个人偏好方法一。
能用SQL算就在数据库里进行运算吧,= =难道自己写的php语句还会比SQL执行的运算效率高?不要把mysql想的太脆弱。
尽量简化代码,所以。。第一种。
第一种虽然说是Join查询,实际上只有一次访问数据库,加上索引不会慢
第二种肯定不会用,死伤~
第一种更简介明了,一般用第一种。
实际哪种性能更好会跟 student
和 class
两个表相对大小有关。
如果第二种方式系统负载低,其实可以把sql 优化成这样:
sql = "select {xxx} from (select * from student where id in (1,2,3)) A,class B where A.class_id=B.id "
如果class表的数据可以不每次从数据库查,全部缓存下来的话,第二种思想还可以。毕竟class不会太多
第一种里面有连接操作,DB在进行连接操作时很费劲(特别是表的记录很多时),而查询操作基本很快.
首先一个, 你的第二个sql 明显给的不对了. 在 程序中做join, 起码我们应该这么写:
伪码:
sql = "select * from student where id in {xxxx}" res = excute(sql) string ids foreach(res as v){ // append v[class_id] to ids. } // ids is now like "1, 3, 4" sql2 = "select {xxx} from class where id in ( $ids )"; res2 = excute(sql2)
这样只需要两次查询, 而你的实现明显 在黑 第二种 方式 啊.
关于在 sql里还是 在程序里做 join, High Performance Mysql 里有讨论, 第三版, 第六章, Complex Queries Versus Many Queries, 和 Join Decomposition 两节.
把 多表join 拆为 多个单表查询, 有以下优势(具体细节自己看书啦):
• Caching can be more efficient.
• Executing the queries individually can sometimes reduce lock contention.
• Doing joins in the application makes it easier to scale the database by placing tableson different servers.
• The queries themselves can be more efficient.
• You can reduce redundant row accesses.
• To some extent, you can view this technique as manually implementing a hashjoin instead of the nested loops algorithm MySQL uses to execute a join. A hash join might be more efficient.> undefined> undefined> undefined> undefined