SQL querying models data

Hi, Is there a way to query data in models. When I use the API Playground and write JS to query, it is not able to process large data and its costs in usage hours 😦 I just want to know database counts per shopify store per shopify model to optimize my data storage. Thanks in advance
15 Replies
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
Thanks Antoine. Is there a way now to access the data base via sql or something?
Chocci_Milk
Chocci_Milk•3mo ago
No, you need to use Gelly in order to make count queries. You can't use SQL on the database (yet) for security reasons. The database is not one per application but one joint database split for each app
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
Got it. Thanks Antoine. Will check out Computed fields on gelly. Hi Antoine, I setup below in api/models/shopifyShop/views/summary.gelly view { id domain orderCount: count(orders) productsCount: count(products) productVariants: count(productVariants) discountsCount: count(discounts) } This used to work earlier but now I am getting "CombinedError:"[GraphQL] canceling statement due to statement timeout"" I guess its because of large data set 😕, any suggestions how to do this?
Chocci_Milk
Chocci_Milk•3mo ago
Yeah, I would recommend instead making 4 different queries for things like count (unless you have a time range)
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
Thanks. I did and it worked for 2 out 4. Basically order and discounts which are perhaps the biggest tables, it didnt work for that 😕
Chocci_Milk
Chocci_Milk•3mo ago
Do you know how many records are in the tables? Also, did you make sure to add a where so that you can minimize the amount of records to be fetched?
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
That is what I am trying to figure out.. how many records per shop are there in the table.. so we can figure out optimization things. I didnt put a where clause yet.
Chocci_Milk
Chocci_Milk•3mo ago
Do you have a ton of shops to do that for or are you targetting specific domains?
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
I wanted to find out per shop # of orders and # of discounts. I already cleaned up the uninstalled customers to reduce the size but again I guess that didnt work Okay, it seems to have worked now on its own.. I guess it takes time for view to get data after view is created.
Chocci_Milk
Chocci_Milk•3mo ago
I don't think that should be the case. Maybe there was an issue when trying to implement the db indexes that the view would need?
[Gadget] Martin
[Gadget] Martin•3mo ago
The way the view is written it runs 4 subqueries for each shop record, all part of single overall query that is subject to the timeout restrictions. You can run the individual counts more efficiently if you turn the query around. E.g. for count(orders) you could instead run view { orders { shop: shop.id orderCount: count(id) [ group by shop ] } } . (as a global view, not a model view) if you wanted a model view on order then the query would be just view { shop: shop.id orderCount: count(id) [ group by shop ] }
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
Not sure but it worked as per expectation and all good now 🙂 Thanks Martin and do I still put this gelly inside shopifyShop i.e. model level or do I need to put this at a global level? Currently I have this in api/models/shopifyShop/views/orderCount.gelly view orderCount { id domain orderCount: count(orders) }
[Gadget] Martin
[Gadget] Martin•3mo ago
The first version of the view is a global view, so that would go into api/views/, the second version is a model view for orders so it would go into api/models/order/views/.
Gaurav Agarwal
Gaurav AgarwalOP•3mo ago
Got it. Thanks Martin

Did you find this page helpful?