执行更多或复杂的SQL查询,还是使用PHP过滤数据? [英] Execute more or complicated SQL queries, or use PHP to filter data?

查看:97
本文介绍了执行更多或复杂的SQL查询,还是使用PHP过滤数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个将获得大量流量的PHP应用程序.我一直了解到,我应该限制SQL查询的数量(现在每个访问者大约有15个).我可以将其限制为使用PHP进行数据过滤和排序的一半左右,但这会更快吗?另外,我可以在查询中使用"JOIN"来减少查询数量,但这真的比执行多个查询快吗?谢谢!

I'm building a PHP app which will get a lot of traffic. I have always learned that I should limit the number of SQL queries (right now it's about 15 per visitor). I could limit this to about half, using PHP to filter and sort data, but would this be faster? Alternatively, I could use "JOIN" in the query to reduce the number of queries, but is this really faster than executing multiple queries? Thanks!

推荐答案

如果每个访问者有15个查询,除非您的应用程序很大,否则您很可能做错了事.

If you have 15 queries per visitor, you most likely did something wrong, unless your application is pretty big.

使用PHP而不是MySQL来排序和过滤数据

用PHP进行排序和过滤不会使您的应用程序更快,但肯定会使它变慢,如果给定正确的索引,MySQL的优化速度将非常快,因此您应该在可能的情况下使用它.

Doing the sorting and filtering in PHP will not make your application faster, it will make it slower for sure, MySQL is optimized to be very fast given the right indexes, so you should certainly use it when you can.

了解数据库索引,它将使您的生活更轻松,并提高应用程序的性能.这是一个链接:

Learn about database indexing and it will make your life easier and increase your application's performance. Here is a link :

http://www.mysqltutorial.org/mysql-create-drop- index.aspx

联合查询与多个查询

您问使用联接的执行速度是否比执行多个查询更快.

You ask if using join's would be faster than executing multiple queries.

答案是肯定的,使用给定正确索引的join总是会更快.这又回到了第一个主题,即索引编制.

The answer is yes, it will always be faster to use join's given the right indexes. Which also comes back to the first topic, indexing.

但是,请记住,连接的效率与您要达到的效率一样高,如果操作正确,它将非常有效,如果操作错误,将非常慢,因此您必须了解正确执行操作的基本知识.

However, keep in mind that joining is as efficient as you make it to be, it will be extremely efficient if done right, and extremely slow if done wrong, so you must know the basics to do it right.

您可以在此处查找有关join的介绍:

You can look here for an introduction to join's :

http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/

这篇关于执行更多或复杂的SQL查询,还是使用PHP过滤数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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