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

->cache(true)->sum('attribute') in mysql doesn't work. #17349

Open
BenasPaulikas opened this issue Jun 7, 2019 · 10 comments
Open

->cache(true)->sum('attribute') in mysql doesn't work. #17349

BenasPaulikas opened this issue Jun 7, 2019 · 10 comments
Labels

Comments

@BenasPaulikas
Copy link

What steps will reproduce the problem?

->cache(true)->sum('attribute') in mysql doesn't work.
->cache(true)->one() - works

What is the expected result?

doing 100x ->cache(true)->sum('attribute') would do sql query only 1 time

What do you get instead?

sql query is executed 100x times

Additional info

Q A
Yii version 2.0.?
PHP version 7
Operating system Linux
@samdark
Copy link
Member

samdark commented Jun 8, 2019

Is that MySQL specific? Does it depend on how cache is configured?

@samdark samdark added status:to be verified Needs to be reproduced and validated. type:bug Bug labels Jun 8, 2019
@alex-code
Copy link
Contributor

Which version of Yii are you using?
If it's older than 2.0.14 this could be why.

@BenasPaulikas
Copy link
Author

Updated to 2.0.20 looks liek now it works except:

->cache(true)->sum('HOUR(TIMEDIFF(created, completed))')

This is not being cached

@alex-code
Copy link
Contributor

How do you know that it's not caching?
I tested a simple loop with a sum query and it's cached.

@BenasPaulikas
Copy link
Author

image

      for($i = 0; $i < 10; $i++) {
            Request::find()->where(['garage_id' => 153])->cache(true)->sum('HOUR(TIMEDIFF(created, completed))');
        }

@BenasPaulikas
Copy link
Author

If I remove ->where(['garage_id' => 153]) then it works .. Is this expected ?

@alex-code
Copy link
Contributor

Do you have a cache configured?

@Arkeins
Copy link
Contributor

Arkeins commented Nov 18, 2022

Hi !

Even if this issue is old, i face the same problem, so i'll put what i found here, in case that can help someone :)

I believe the sum() result is not cached when there are not results (the query return null).

In that case, queryScalar() (which is used internally by sum()) return false.

When checking the cache, false will then be returned, which also means that there is no valid entry in cache for this query, leading to a new execution.

@bizley
Copy link
Member

bizley commented Nov 18, 2022

Could you prepare new issue with that?

@Arkeins
Copy link
Contributor

Arkeins commented Nov 21, 2022

After some more digging I was kinda right.

The query returns null, which is cached.

The part that fails seems to be this one:

                if (is_array($result) && isset($result[0])) {
                    Yii::debug('Query result served from cache', 'yii\db\Command::query');
                    return $result[0];
                }

(in db/Command.php line 1157)

isset seems to return false if the value is null.

Changing to

                if (is_array($result) && array_key_exists(0, $result)) {
                    Yii::debug('Query result served from cache', 'yii\db\Command::query');
                    return $result[0];
                }

fix the issue for me (I have no clue if it breaks anything).

Should i still create a new issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants