一个大查询与许多小查询?

时间:2022-12-03 16:55:01

I'd like to know, which option is the most expensive in terms of bandwith and overall efficiency.

我想知道,就带宽和整体效率而言,哪种选择是最昂贵的。

Let's say I have a class Client in my application and a table client in my database.

假设我的应用程序中有一个类客户机,数据库中有一个表客户机。

Is it better to have one static function Client.getById that retrieves the whole client record or many (Client.getNameById, Client.getMobileNumberById, etc.) that retrieve individual fields?

最好有一个静态函数客户端。获取整个客户端记录或多个(客户端)的getById。getNameById,客户机。检索单个字段的getMobileNumberById等)?

If a single record has a lot of fields and I end up using one or two in the current script, is it still better to retrieve everything and decide inside the application what to do with all the data?

如果一个记录有很多字段,而我最终在当前脚本中使用了一两个字段,那么检索所有内容并在应用程序中决定如何处理所有数据是否更好?

I'm using PHP and MySQL by the way.

顺便说一下,我用的是PHP和MySQL。

1 个解决方案

#1


12  

Is it better to have one static function Client.getById that retrieves the whole client record or many (Client.getNameById, Client.getMobileNumberById, etc.) that retrieve individual fields?

最好有一个静态函数客户端。获取整个客户端记录或多个(客户端)的getById。getNameById,客户机。检索单个字段的getMobileNumberById等)?

Yes, it is.

是的,它是。

Network latency and lag as well as the overheads of establishing a connection mean that making as small a number of database calls as possible is the best way to keep the database from saturation.

网络延迟和延迟以及建立连接的开销意味着尽可能少地进行数据库调用是防止数据库饱和的最佳方法。

If the size of the data is really so much that you see an effect, you can consider retrieval of the specific fields you need in one single query (tailor the queries to the data).

如果数据的大小如此之大,以至于您看到了效果,那么您可以考虑在单个查询中检索所需的特定字段(将查询定制为数据)。

#1


12  

Is it better to have one static function Client.getById that retrieves the whole client record or many (Client.getNameById, Client.getMobileNumberById, etc.) that retrieve individual fields?

最好有一个静态函数客户端。获取整个客户端记录或多个(客户端)的getById。getNameById,客户机。检索单个字段的getMobileNumberById等)?

Yes, it is.

是的,它是。

Network latency and lag as well as the overheads of establishing a connection mean that making as small a number of database calls as possible is the best way to keep the database from saturation.

网络延迟和延迟以及建立连接的开销意味着尽可能少地进行数据库调用是防止数据库饱和的最佳方法。

If the size of the data is really so much that you see an effect, you can consider retrieval of the specific fields you need in one single query (tailor the queries to the data).

如果数据的大小如此之大,以至于您看到了效果,那么您可以考虑在单个查询中检索所需的特定字段(将查询定制为数据)。