我应该使用一个大的SQL Select语句还是几个小的SQL语句? [英] Should I use one big SQL Select statement or several small ones?

查看:129
本文介绍了我应该使用一个大的SQL Select语句还是几个小的SQL语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用从MySQL发送的数据构建一个PHP页面.

I'm building a PHP page with data sent from MySQL.

有更好的选择

    具有4个表联接的
  • 1个SELECT查询,或者
  • 没有表联接的
  • 4个小的SELECT查询;我确实从ID中选择了
  • 1 SELECT query with 4 table joins, or
  • 4 small SELECT queries with no table join; I do select from an ID

哪种方法更快?每种方法的优点/缺点是什么?每个表只需要一行.

Which is faster and what is the pro/con of each method? I only need one row from each tables.

推荐答案

如果您真的很担心,则应运行一个配置文件工具,因为它取决于很多事情,并且可能会有所不同,但通常来说,减少查询量会更好编译后,往返数据库的往返次数减少.

You should run a profiling tool if you're truly worried cause it depends on many things and it can vary but as a rule its better to have fewer queries being compiled and fewer round trips to the database.

确保您可以过滤内容以及使用where和join on子句的能力.

Make sure you filter things as well as you can using your where and join on clauses.

但是,老实说,这通常并不重要,因为与数据库所能做的相比,您可能不会受到那么大的打击,因此,除非优化是您的规范,否则您不应该过早地做它,而要做最简单的事情.

But honestly, it usually doesn't matter since you're probably not going to be hit all that hard compared to what the database can do, so unless optimization is your spec you should not do it prematurely and do whats simplest.

这篇关于我应该使用一个大的SQL Select语句还是几个小的SQL语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆