PHP、OOP 和数据库 - 性能问题

发布于 2024-11-19 01:55:43 字数 795 浏览 2 评论 0原文

我有一个关于在 PHP 中使用 OOP 和数据库时的性能问题。我将通过示例提出我的问题,假设类 foo 代表某个表中的一行。现在假设我需要在网络应用程序的 5 个不同页面上使用 foo。

问题是,在 5 页的每一页上,我将使用不同列的数据。 (即第一页将使用第 1 列和第 2 列,而第二页将使用第 3 列和第 4 列,等等。)

OOP 方法(据我所知)建议当我在某个特定行上初始化 foo 时,我将连接并获取该行的所有列并构建我的对象。然后我可以继续我的逻辑并使用我可能需要的任何数据。

我遇到的问题是,使用过程方法(在网络方面我更习惯这种方法)不会浪费资源来下载我不需要的列,因为查询将专门根据需要定制(即,如果我在第一页,我只会下载第 1 列和第 2 列,因为这就是我需要的。)

我的 OOP 方法是否错误,或者额外的开销是否微不足道,以至于开发人员通常会下载他们不下载的数据需要?

谢谢,抱歉,如果这已经被涵盖,我认为这将是一个有趣的话题! :)

Erik

进一步澄清:

课程就像:

class foo
{
  $column1;
  $column2;
  $column3;
  $column4;

  public function _construct($id)
  {
    //get column 1,2,3 and 4 from database where table_id = $id
  }
}

问题是,如果我只需要第 1 列一页,我就免费下载第 2 列、第 3 列和第 4 列。在程序方法中你不会这样做。我的 OOP 模型不好还是这样可以?

I have a question regarding performance when using OOP in PHP together with databases. I'll ask my question by example, suppose a class foo represents a row from some table. Now suppose I need to use foo at 5 different pages on my web app.

The catch is that on each of the 5 pages I will use data from different columns. (i.e. the first page will use column1 and column2 while the second page uses column3 and column 4, etc..)

The OOP approach (as far as I can see) would suggest that when I initialize foo on some particular row I would connect and fetch all the columns of that row and build my object. I could then proceed with my logic and use whatever data that I might need.

The issue I have with this is that with the procedural approach (which I'm more used to when it comes to web) would not waste resources to download columns that I do not need since the query would be specifically tailored to the needs of the particular page.(i.e. If im on the first page I would only download column1 and column2 since that's what I need.)

Am i going about the OOP approach wrong or is the extra overhead so insignificant that developers in general download data which they do not need?

Thanks and sorry if this has already been covered, I thought it would be an interesting topic! :)

Erik

further clarification:

The class is like:

class foo
{
  $column1;
  $column2;
  $column3;
  $column4;

  public function _construct($id)
  {
    //get column 1,2,3 and 4 from database where table_id = $id
  }
}

The issue is that if i only need column1 one one page i download column2,3 and 4 for nothing. In procedural approach you would not do that. Is my OOP model bad or is this ok?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦年海沫深 2024-11-26 01:55:43

您仍然可以通过在构造时使用要抓取的列数组,或使用公共类方法来处理查询抓取,将选择性查询合并到 OOP 类中。

构造函数示例:

<?php

class Foo{
    public function __construct( $column ) {

        if(is_array($column)){
             if(count($column) > 1){
                 $result = mysql_query('SELECT `'.implode('`,`', $column).'` FROM `table`;');
             }else{
                 $result = mysql_query('SELECT `'.$column[0].'` FROM `table`;');
             }
        }else{
             $result = mysql_query('SELECT `'.$column.'` FROM `table`;');
        }

        $this->result = mysql_result($result, 0);
    }
}
?>

公共函数方法与此相同,只是您可以返回结果而不是设置 $this->result

You can still incorporate the selective query inside of an OOP class by using either an array of columns to grab upon construction, or by using a public class method to handle the query grabbing.

Example of constructor:

<?php

class Foo{
    public function __construct( $column ) {

        if(is_array($column)){
             if(count($column) > 1){
                 $result = mysql_query('SELECT `'.implode('`,`', $column).'` FROM `table`;');
             }else{
                 $result = mysql_query('SELECT `'.$column[0].'` FROM `table`;');
             }
        }else{
             $result = mysql_query('SELECT `'.$column.'` FROM `table`;');
        }

        $this->result = mysql_result($result, 0);
    }
}
?>

The public function method would be identical to that, except you could return the result instead of setting $this->result.

旧人九事 2024-11-26 01:55:43

我不完全确定我理解你的问题。我认为以下三件事可以适用于您解决此问题的方式:

A) 您正在尝试构建一个对象,然后在整个脚本中使用该对象中包含的数据。
B) 您正在使用 PDO 风格的数据库拉取。
C) 您正在使用 PHP SPL 来生成一个对象的迭代,该对象包含从数据库中提取信息的方法。

我现在假设您正在使用选项 A。如果我错了,请原谅我,我根本不想低估您的知识……只是从这里开始。

OOP 的方法并不是提取所有数据以使其在整个脚本中可用。将其视为函数的集合而不是数据的集合,尽管它很可能是其中之一或两者。您将像编写没有 OOP 的函数一样编写类方法。唯一的区别是,该对象可用于与您的脚本进行通信,次数超过您需要的次数...

为了清楚地回答您的问题,我从不提取超出我需要的数据。出于安全和性能原因。您应该像使用过程风格一样使用类。您可以在实例化类时(使用构造函数方法)执行脚本所需的所有数据拉取,但请确保这只是您需要的数据。

----添加

class foo{

  function getData($page){
       //Query to get the results you want based on the page number entered...
       //Process it as you normally would into a result set, array, or whatever.
       return $results;      
  }

}

然后调用

$foo = new Foo();
$page = "The page or the column that you want to pull";
$data = $foo->getData($page);

您仍在按程序执行所有操作,但现在您有一个动态函数,可以根据您作为页面发送的内容提取数据...在这种情况下,我看不出有任何理由使用构造函数...只是一个 getter 方法。

这有帮助吗?

I'm not entirely sure I understand your question. There are three things that I think could apply to how you are approaching this problem:

A) You are trying to build an object and then use data contained in that object throughout your script.
B) You are using a PDO style database pull.
C) You are using PHPs SPL to produce an iteration over an object which contains methods to pull information from the database.

I'll assume for now that you are using option A. Please forgive me if I am wrong and I am not trying to underestimate your knowledge at all...just getting it started here.

The approach of OOP is not to pull in all data to have it available throughout your script. Think of it as a collection of functions instead of a collection of data, although it could easily be either or both. You'll write your class methods just like you write functions without OOP. The only difference is, the object can be used to communicate with your script over the number of times that you need it to...

To answer your question plainly, I never pull more data than I need. For both security and performance reasons. You should use a class just like you use the procedural style. You could do all of your data pulls that will be required for the script upon instantiating the class (using a constructor method), but make sure that it's only the data you will need.

----Added

class foo{

  function getData($page){
       //Query to get the results you want based on the page number entered...
       //Process it as you normally would into a result set, array, or whatever.
       return $results;      
  }

}

Then call that

$foo = new Foo();
$page = "The page or the column that you want to pull";
$data = $foo->getData($page);

Your still doing everything procedurally, but now you have a dynamic function that can pull data based on what you send in as page... IN this case, I don't see any reason to use a constructor...only a getter method.

Does that help?

灯角 2024-11-26 01:55:43

一般方法是仅选择您需要的列
foo->db->tablename->select('all', where date = $date)
快速浏览一下 cakephp 和 symfony 等框架,它可能会帮助您更好地了解它通常是如何完成的。

The general approach will be to select only the columns you need
foo->db->tablename->select('all', where date = $date).
Take a quick look at frameworks such as cakephp and symfony, it might help you get a better idea of how it's generally done.

蓝眸 2024-11-26 01:55:43

我的两分钱。这取决于很多因素以及它如何影响整个应用程序,即。数据库请求数、每条记录的大小、行集的大小等,

当我遇到缓慢的请求或高内存使用率时,我个人会加载所有列和配置文件以查找瓶颈。如果我遇到瓶颈,那么我会考虑延迟加载此时仅需要的列。

My two cents. It depends on a number of things and how it affects the application as a whole ie. # of database requests, size per record, size of rowset, etc

I personally load all columns and profile to look for bottlenecks when I experience slow requests or high memory usage. If I have bottlenecks then I consider lazy loading only required columns at that point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文