Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using "while" statement while "selecting" #1

Open
AdamSEY opened this issue Feb 3, 2018 · 4 comments
Open

using "while" statement while "selecting" #1

AdamSEY opened this issue Feb 3, 2018 · 4 comments

Comments

@AdamSEY
Copy link

AdamSEY commented Feb 3, 2018

I guess using "while" statement while "Selecting" is not possible right?

Using "while" statement while "Selecting" much better for performance. Because storing the data in a variable then looping to edit something is not preferred.

@AdamSEY AdamSEY changed the title using "while selecting" using "while" statement while "selecting" Feb 3, 2018
@ocram
Copy link
Contributor

ocram commented Feb 3, 2018

Thanks, good question!

You seem to be referring to the difference between the following two approaches:

$rows = $stmt->fetchAll();
// ...

// vs

while ($row = $stmt->fetch()) {
    // ...
}

First, performance in terms of execution speed is quite similar for these two approaches, and the first approach might even be a little bit faster.

What you have in mind is probably memory consumption, though. For very large result sets, the second approach may actually use a lot less memory if you are executing an unbuffered query.

You can only take advantage of this and the effect does only materialize if you process the data right away, i.e. inside the loop, and don’t need access to the whole result set outside of the loop anymore. So if you build an array inside the loop manually, you can just use the first approach instead.

In modern applications, you often don’t output data immediately but pass data to a templating engine which inserts them into the (HTML) templates. In that case, you will usually have to use the first approach, anyway.

But the second approach still has several use cases where it is preferable. You’re right.

Internally, we use the first approach for the select method and the second approach for the selectRow method in the PdoDatabase class.

We should add another method that uses the second approach, just like selectRow, but instead of returning a single row it should return a wrapper around the underlying result so that you can call something like next on this result or use it in a while or foreach loop. This new method should, unlike the existing methods, execute an unbuffered query, e.g. with

$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

for MySQL, so that you can really take advantage of the reduced memory consumption.

@AdamSEY
Copy link
Author

AdamSEY commented Feb 5, 2018

Does your library support unbuffered query?

@ocram
Copy link
Contributor

ocram commented Feb 5, 2018

No, not at the moment. That’s because buffered queries are really what you want with the methods that this library currently offers, and unbuffered queries would only be useful with that new query method that could be used with loops. If we add that method, we’ll probably use unbuffered queries there.

@AdamSEY AdamSEY closed this as completed Feb 26, 2018
@ocram
Copy link
Contributor

ocram commented Feb 28, 2018

Let’s keep this open because this is something we should definitely implement at some point in the future. Thanks again for the feedback here!

@ocram ocram reopened this Feb 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants