-1

私は通常、自分のプロジェクトで可能な MySQL クエリを少なくしようとしてから、PHP を使用してデータをフォーマットします。

正確なデータを適切な形式で提供するより多くの MySQL クエリでパフォーマンスが向上するのか、それとも単一のクエリで必要なすべてを取得し、PHP で結果を解析して必要な配列を取得する方がよいのか疑問に思っていました。

4

2 に答える 2

3

In the modern PHP, where encapsulation is one of the main points while writing code, a bunch of queries won't be executed, if you explicitly don't call the methods, which do the job.

Against some old-procedural style, where you can have in one page 100 queries and testing with is..() functions, which query to use, you will have a high load to your engine, because of the runtime compilation.

Thus, you can afford youself as much conditions as you want in a lot of queries, and less PHP to test it.

Let's have an example with something small.

Assume, you have something like a model, for an item called "Article". You want a search either by id, author or name. There's nothing wrong to have this:

public function getArticleById($id) {
    $sql = "SELECT id, name, author FROM article WHERE id = $id";
    $query = $this->db->query($sql);
    $result = $this->db->fetch($query);

    return $result; //assuming you have query and fetch method, where the last one return an array/object
}

public function getArticleByName($name) {
    $sql = "SELECT id, name, author FROM article WHERE name = $name";
    $query = $this->db->query($sql);
    $result = $this->db->fetch($query);

    return $result;
}

public function getArticleByAuthor($author) {
    $sql = "SELECT id, name, author FROM article WHERE author = $author";
    $query = $this->db->query($sql);
    $result = $this->db->fetch($query);

    return $result;
}

Depends on what you want, you can use one of these methods. I usually asume, you can reuse the information returned by these methods in more than one page, because it returns the whole needed information for a single Article for example. On the other hand, I would assume you have used some bad practices, if, for example, the article has a DateTime field, and you use it in 3 pages with different formats - then you will need PHP to reformat the datetime, each time you use the method, which returns the info, and will be total overkill if you have 3 queries, which returns the datetime with different formats.

Also, total overkill is to request the whole table, and filter the information with PHP, instead of WHERE:

public function getArticle() {
        $sql = "SELECT id, name, author FROM article";
        $query = $this->db->query($sql);
        $result = $this->db->fetch($query);

        return $result; 
    }

//...

foreach ($model->getArticle() as $article) {
    if ($article['name'] == $name) {
        //...
    }

Talking about performance is subjective thing, it really matters how and how often you query the database, so a single query which formats the data in given manner is reused often enough, you won't load your core.

于 2013-09-05T10:13:22.403 に答える
2

経験則として..

通常、私が従うルールは、データベースのようなある種の永続的な構造に対して行うリクエストの量を最小限に抑えることです。データベース リクエストは非常に高速ですが、通常、メモリ内の構造を言語で操作できるようにする方が高速です。また、DBMS のビジー状態にも依存します。このテーブルからリクエストしている他の人はいますか? あなただけですか?それを使うのはいつもあなただけですか?

お望みならば..

いくつかのベンチマーク テストを実行して、2 つのスレッドが終了するまでの時間を単純に計ることができます。複数の MySQL クエリを含むもの。1 つは単一の MySQL クエリを使用します。顕著な一貫した違いが見られる場合は、答えがあります。

于 2013-09-05T09:44:15.547 に答える