您好,欢迎访问一九零五行业门户网

这两种查询逻辑应该用哪个?

方法一:
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 = 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 idsforeach(res as v){ // append v[class_id] to ids.}// ids is now like 1, 3, 4sql2 = 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
额,显然是在用第一种啊
原因? 各位叔叔姐姐大爷大妈,求考虑下,sql的主要用途是干嘛的?
sql script的计算数量级是多少的?
excute 貌似不是t-sql92规范中支持的吧?
楼上说的没错,楼主绝对是高级黑 我想说,如何你想混合计算,那起码中间的foreach 返回值相关的东西,你丢该程序去干行吗
summary如果这里你不选方法一,那你就当我什么都没说
不是偏好问题,是堆代码的原则性问题:1.先尽可能降低sql文发行次数。 2.再优化sql文执行效率。
方法二把发行sql文写在循环代码里,是写代码的大忌,无论你做的东西有多么的轻量级。
ps:sql文基本不可能有得不到的结果集,所以不要用多次发行sql文的方式来解决查询结果集拼凑问题。
肯定不会用第二种,因为mysql再慢也比当前语言实现的快,而且查询在语句端更容易优化,sql更加容易维护。
其它类似信息

推荐信息