statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 73 forks source link

Query loads all entries from a collection in a multisite environment #155

Closed maxi032 closed 11 months ago

maxi032 commented 1 year ago

I have a multisite with a Products collection that has a taxonomy product_categories applyed to it. Blueprints are stored statically but collections and taxonomies are in db. I think that the driver is loading all entries from all sites that have that specific product category. What is causing this query? Just the fact that I assigned a taxonomy to a collection? Instead what I would expect is that when I am o products/nl only the entries of site nl would be queried+origin. I also see that there is an n+1 problem that seems to be caused by the driver: I would have expected select * from products where id in (23676, 23681, 32787, only ids of nl) instead I get:

select * from `entries` where `collection` = 'products' and (json_extract(`data`, '$."product_categories"') is not null AND json_type(json_extract(`data`, '$."product_categories"')) != 'NULL')
184ms
/vendor/statamic/cms/src/Query/EloquentQueryBuilder.php:57
statamicdb
select * from `entries` where `id` = 23676
43ms
/vendor/statamic/cms/src/Query/EloquentQueryBuilder.php:57
statamicdb
select * from `entries` where `id` = 23681
81.31ms
/vendor/statamic/cms/src/Query/EloquentQueryBuilder.php:57
statamicdb

I am using statamic v3 pro with Laravel9 What am I doing wrong?

ryanmitchell commented 1 year ago

Thanks for reporting. The n+1 problem should be resolved by: https://github.com/statamic/eloquent-driver/commit/59a45e33484e74a24379bb2f01496bc99db0de78

I hope to get some time this week to look into the query not scoping to the site and figure out a fix for that.

ryanmitchell commented 1 year ago

Can you let me know the tag you are using or control panel page you are seeing the

select * from `entries` where `collection` = 'products' and (json_extract(`data`, '$."product_categories"') is not null AND json_type(json_extract(`data`, '$."product_categories"')) != 'NULL')

query on? Any combinations I try result in the site being added, so I'm struggling to recreate what you are seeing.

ryanmitchell commented 1 year ago

@maxi032 any update? I’m unable to recreate currently

maxi032 commented 1 year ago

@ryanmitchell Thanks for the reply. I'll let you know soon. Sorry I haven't seen your answer

maxi032 commented 1 year ago

@ryanmitchell Here is the CP page of the Products collection: products-collection And heree is the taxonomy term 'product_category': blueprint-taxonomy-term-product-categories

Product categories have a parent: blueprint-taxonomy-term-product-categories-parent1

Edit page of the parent field: blueprint-taxonomy-term-product-categories-parent

ryanmitchell commented 1 year ago

Im still unsure where you are seeing the querying of all the entries - ideally you would supply a sample repository showing the issue and clear directions as to how to recreate the issue. This would allow be to investigate fully without any guesswork.

ryanmitchell commented 11 months ago

Closing due to inactivity. Feel free to re-open if you are providing the information requested or a sample repository with details of where to see the issue.