|
I need help creating a SQL query that will return a cocktail from a database, given that I have supplied all the ingredients that go into that cocktail So for example, I want the "Gin and Tonic" row to return only if I have supplied the correct ids for Gin (id is 1) and Tonic (id is 2). I I only supply "Tonic," I shouldn't get back the row I'm using SQLAlchemy and Flask, but I'm still having trouble wrapping my head around how the query will work altogether This is what my table structure looks like code:
code:
code:
code:
code:
code:
code:
teen phone cutie fucked around with this message at 21:20 on Jan 13, 2019 |
# ¿ Jan 13, 2019 21:06 |
|
|
# ¿ May 21, 2024 06:39 |
|
Ruggan posted:Are you only looking for EXACT matches, or are you looking for any recipes you can make using your list? Yes - Gin and Tonic should be returned if you included Whiskey Moonwolf posted:Ruggan's question's a good one. This is a really hard problem with a dynamic amount of parameters though. What you need to get is: So you're saying I could do something like 1. Client asks for all liquors including Gin and Tonic 2. SQL statement returns "Gin and Tonic" and "Gin and Tonic and Lime" 3. Python compares the client's list of IDs with the IDs of each Cocktail.ingredients 4. If all the client IDs appear in each Cocktail.ingredients.id, add that cocktail to a result array 5. Return the result array to the client ???
|
# ¿ Jan 13, 2019 23:46 |
|
Moonwolf posted:You can make that even simpler, you don't need the EXCEPT at all: I’ll try this and some of the others when I get home thanks! Additionally, I’m still not great at converting SQL to SQLAlchemy so any help there would be great as well e: looks like this one did the trick! welp here's the SQLAlchemy after lots of trial and error code:
code:
teen phone cutie fucked around with this message at 03:17 on Jan 15, 2019 |
# ¿ Jan 14, 2019 22:27 |
|
Lol wow. I wish I knew this days ago
|
# ¿ Jan 15, 2019 13:29 |
|
Say I have a relational table called ingredients_in_cocktails that have 2 primary/foreign keys: Cocktail ID and Ingredient ID so a row might look like: code:
code:
teen phone cutie fucked around with this message at 03:19 on May 26, 2019 |
# ¿ May 25, 2019 18:51 |
|
Ok so i need to create the ID column as a normal row, THEN remove the unique index, THEN give my ID column the unique index?PhantomOfTheCopier posted:You don't really want to do that. Either ingredients are unique or recipes are ordered. In the first case, "lime juice" should be a different ingredient than "lime wedge". I do have a "step" column in that table - I just didn't post it. I actually am solving for the problem where you need to wipe the rim of a cocktail glass with a lemon twist and then throw the lemon twist into the glass, similar to your sugar example. Scaramouche posted:Yeah, just make ID the primary key and unique. I'm assuming (and hope) my existing queries should be fine teen phone cutie fucked around with this message at 03:23 on May 26, 2019 |
# ¿ May 26, 2019 03:11 |
|
hmmm I seem to be running into some errors while doing this. Mainly because both cocktail_id and ingredient_id are foreign keys. When running something like: code:
And when trying code:
I am very much a SQL newbie, so I could use some hand-holding on this.
|
# ¿ May 26, 2019 06:37 |
|
Ruggan posted:You don’t need the ordinal/step to have meaning or appear. The steps could just say “wipe the rim with the lemon and save the lemon for later” and “put the lemon you saved into the drink” or w/e. This is a good point. I'd probably be better off just adding a "step text" column or something like that. Thanks for that. I think I'm over-complicating this. PhantomOfTheCopier posted:But this is all a trick. I'll bet you're really doing a homework problem to write instructions for making a peanut butter and jelly sandwich. Or I'm a front-end developer trying to learn SQL and Python in my spare time jesus loving christ
|
# ¿ May 27, 2019 05:27 |
|
I posted redacted a readacted version of my table. Here's what I actually have Cocktails: code:
code:
code:
|
# ¿ May 28, 2019 16:45 |
|
yeah these are all good points. tbh, I'm not really looking to over-engineer this thing. It's just a fun little portfolio project to prove I can work with backend tech. They're probably all stretch goals I can accomplish if I ever get bored.
|
# ¿ May 28, 2019 18:42 |
|
Not sure if there's a better thread for this, but I have a performance questions re: a personal project I'm working on: What do you people think is the most performant way to do the "unread count" for threads that Something Awful has? Talking about this thing: I'm building this feature into my project where I have "threads," "posts," and "users" and I'm thinking about making association table to store maybe the thread ID, user ID, and just arbitrary "number of posts read," but I also feel like that table could get really big, really quick and I wonder if there's a better way to handle something like this. I've been poking around at other forums and SA seems to be one of the only forums that even try to show that count, so I wonder if other forums just said gently caress it, we're not doing that because of this exact performance concern.
|
# ¿ Mar 23, 2022 23:13 |
|
PhantomOfTheCopier posted:Hence all those algorithms courses you took on computing and managing lists of windows, date ranges, finding overlaps, etc. yeah right all those algorithm courses. I'm totally not just building this project by the seat of my pants okay but if I'm reading your response right, you're just saying store store number of posts read and last post ID and make it simple?
|
# ¿ Mar 24, 2022 01:53 |
|
teen phone cutie posted:Not sure if there's a better thread for this, but I have a performance questions re: a personal project I'm working on: What do you people think is the most performant way to do the "unread count" for threads that Something Awful has? Talking about this thing: so i posted in the python thread about this, but now that I have my table schemas for users, threads, and read_count, I'm now looking for a query that will help me join the read count to the thread schema https://forums.somethingawful.com/showthread.php?threadid=3812541&pagenumber=171#post522793982
|
# ¿ Apr 14, 2022 07:02 |
|
Jabor posted:I'm reading the docs you linked and all I can think is that ORMs are a mistake. this is great thank you! I ended up translating it to the ORM by doing this: Python code:
|
# ¿ Apr 14, 2022 17:51 |
|
Does anyone have a good resource for learning indexes, how they work, what they do, how to effectively use them? I'm working a personal project and am about done with the backend and DB, but I'm still a beginner with DBs so I just learned that indexes are a thing and I'd love to read over a dummy's how-to guide if there's a good one out there. Specifically for MySQL if it matters.
|
# ¿ Jul 7, 2022 21:49 |
|
hi i'm diving into the Redis docs for the first time ever and outside of understanding caching as a concept, i'm going in with not much experience with a goal to cache some of my database queries, and while i'm sure i'm going to need more help in the future, i have one initial question about using Redis in general: should i be caching data that is subject to change a lot? for example, if i am making a query to get "page 1" of a bunch of data but some stuff might get moved to "page 2" in a short time, is that even worth caching, even if i bust the cache when new data is created?
|
# ¿ Feb 19, 2023 10:26 |
|
nielsm posted:Caching search results like that can make sense from a user experience point of view. Query the entire result set, save in the cache with a key for the specific user and query, so the results don't move when the user browses the pages. but what if there's hundreds of pages? what's the best way to cache a large amount of paginated data?
|
# ¿ Feb 19, 2023 23:25 |
|
hi again i'm hoping someone can help me out with a really trick mysql statement. i'm trying to get the latest Offense from an Offense table that is tied to a user, but the trick is offenses are tied to posts and private messages, not users, so i first need to join together Posts and PrivateMessages before I can filter down on the user level i have 4 tables: Posts, PrivateMessagePosts, Users, and Offenses. Offenses are tied to either a `Post.id` or `PrivateMessagePost.id` but not a user. they look like this: so far I have a SQLAlchemy query which will get a list of posts given a thread ID (that part is irrelevant), and return them with all of the offenses tied to them. it looks like this: pay no mind to the current offense join! that is to get a CSV of all offenses tied to each post we're querying. what i want additionally is the latest offense that is active on the user Python code:
SQL code:
|
# ¿ Feb 22, 2023 23:23 |
|
also i think i've posted about using ORMs before here and was told they suck, and now i'm finally understanding it. they generate some rear end queries, which is why i'm trying to manually write this query without letting the ORM get the relational data.
|
# ¿ Feb 22, 2023 23:25 |
|
man i literally got there right before you posted that. thanks!!!! i have been ready to smash my keyboard looking for this solution
|
# ¿ Feb 23, 2023 03:56 |
|
i previously posted ITT about wanting to implement Redis for some paginated SQL queries and one suggestion was to request all the data and just store it as one object and just bust the cache when an item is updated or added (items cannot be removed at this time) But i was thinking of another possible solution and i'd like to be sanity checked here. What I was thinking is: 1. create a factory function that will take page number and return a SELECT statement for a page of items 2. when calling my SELECT statements in whatever GET request, run the factory function with the requested page and store the returned query as the Redis key (if the Redis key/value doesn't already exists. if it does, just read from memory) 3. when adding/updating an item, run another SELECT query to get what page this item is going to be on and then execute the factory function again with the calculated page number to get what the SELECT statement is going to be and then nullify that Redis key/value does this sound like it could work? am i overengineering this? i'm just really not sure how to properly use Redis with paginated data that can always be added to or updated teen phone cutie fucked around with this message at 23:11 on Mar 14, 2023 |
# ¿ Mar 14, 2023 20:29 |
|
nielsm posted:I think you're thinking of a suggestion I made, but what I was thinking of was actually to use the cache to keep the result set constant while the user was browsing it. That is, nothing would invalidate or update the cached result set, other than a timeout or other indication that the user was done using that result set. In that model, you don't attempt to update the cached result set at all, but the user also won't see new items unless they explicitly invoke a new search or request a reload of the result set. In return, the user gets fully predictable paging with no results jumping around or getting skipped when switching page. gotcha yeah i figured i was bastardizing the explanation a bit. though this solution still doesn't seem optimal for my use-case. the paginated items are comments in a thread, so the page results are going to be consistent other than a comment being edited or a new comment being added, which i would like to be instant with adds/updates busting the cache.
|
# ¿ Mar 14, 2023 21:53 |
|
i have a crazy query that I need a bit of help optimizing. most of it is fast, but there's one piece of it that is slowing it down to a screeching halt Basically I have an endpoint that is returning me a paginated array of posts that belong to threads, which themselves belong to forums. I want to get the users active ban. This all works fine. The part that's slowing this down is the "author_total_posts" because I want to return the count of every single post the user made JavaScript code:
SQL code:
Hopefully this makes sense. I can provide more information about the tables if need be but yeah the gist is just that last LEFT OUTER JOIN takes forever to get the total post count for each post author that's being returned in a list of posts teen phone cutie fucked around with this message at 02:03 on Jan 15, 2024 |
# ¿ Jan 14, 2024 23:54 |
|
Hammerite posted:is that absolutely the smallest example you can create that exhibits the undesired behaviour? yep sorry i meant the third. The lateral thing I picked up from stackoverflow, so I'm not sure if there's an easier way to accomplish what I want. SQL is not my biggest strength. As I understand it, the reason I need the lateral subquery is because the main query is getting a page of posts. So let's say for example the query is getting page 301 of the "SELECT * FROM Questions WHERE Type = 'Stupid'" thread. BUT I want to also get all the posts you've made on SA total and list it under your avatar. So in your case you have "14748" posts and I need to get that from all threads that are not deleted. So the thread in the outer-most query is just this thread, but in the lateral subquery, it's all threads you've posted in that are not deleted Hammerite posted:In most cases where you refer to multiple instances of a table you give each instance a unique alias, but you didn't do that for "threads". It's referenced in the outermost query and in the counting query as "threads". I can't imagine that "threads" in the counting query could sensibly refer to anything but the most local one, but for consistency if nothing else you might as well make sure. so you're saying make 2 different aliases for each, the outer and the subquery? teen phone cutie fucked around with this message at 02:22 on Jan 15, 2024 |
# ¿ Jan 15, 2024 02:03 |
|
Hammerite posted:is that absolutely the smallest example you can create that exhibits the undesired behaviour? When I remove the last outer join, the query goes from 2 seconds to 100ms, so I'm 100% certain it's the problem. Unless, you're suggesting I try something else?
|
# ¿ Jan 15, 2024 02:05 |
|
|
# ¿ May 21, 2024 06:39 |
|
McGlockenshire posted:What's EXPLAIN say? Hammerite posted:This is probably the most practical solution to your immediate problem. Denormalise by storing a user's number of posts as a column in the User table. If it's important that the number be accurate and you're worried that you'll miss cases where it should be updated, there are strategies for dealing with that (triggers, scheduling tasks to update the column) lemme give this stuff a shot when i get some time. will report back!
|
# ¿ Jan 16, 2024 00:41 |