开发者社区 问答 正文

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

方法一:

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故障,我就怀有迟疑态度了。
请比较熟悉的人帮我分析一下,谢谢

展开
收起
a123456678 2016-06-29 13:56:43 1896 分享 版权
1 条回答
写回答
取消 提交回答
  • 第一种更简介明了,一般用第一种。
    实际哪种性能更好会跟 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 "
    2019-07-17 19:49:04
    赞同 展开评论