6
surgiie
7y

Are sql joins a bad practice? :o
I recently did some work on a page for a site ive never worked on cause my boss told me to. So they recently added product detail video urls to a table that has a relationship to the products table. The existing code was querying for the products on that said page and then during the loop that was outputting the products ,there was another query for getting the url for the current iteration/product. Told my coworker that this imo was pretty inefficient way to do it and switched it to a join and did 1 query then output that but his words were "The way it is now maybe ineffecient in your opinion but it works. Also combining inner joins with left or right is not a good practice. If the data is changed upstream the entire query would need to be redone to accommodate the change". Mind you that they query views a lot which are all made from queries that use joins and I'm also pretty sure these views were written by someone who used to be here because these guys are not good at sql or at least that's what there queries show. I'm at the point now where I'm realizing that my boss and this other guy don't give a fuck about efficiency or doing things the right way they just want it "to work". So this coworker changed my query back to the way it was because he said it broke the shopping cart even though that was already broken when I started... What is life? Maybe I'm the stupid one?

Comments
  • 2
    There's a chance you could be wrong but I'm thinking you're right. They may just not understand joins and want something they feel comfortable supporting in case you leave the company. Lots of variables to consider in this one.
  • 3
    Joins can be "expensive", but in your case it's much better than that nested loop. Especially if the tables are indexed correctly. So, sorry to hear that ignorance from your boss and colleagues :(
  • 0
    @bryceleo well I can see if it was multiple videos per product then id say the join query would be wrong and would need modified but its only one video per product. But Idk I just work here and do what I'm told lol. Its "just stick to what works and don't worry about making things great"around here.
  • 0
    @SleepyOne yeah the database is a god damn shit show. They don't take advantage of fk's so they just redefine columns in other tables instead of using the relationship, they dont add constraints or index columns. Its pretty bad around here:/ I'm just dealing with it and do what I'm told until can find another job but job hunt has been unsuccessful so far:(
  • 1
    Joins are better in this situation I'd say as it's easier on the DB to deal with that than a lot of queries...
  • 1
    It depends.
    Generally speaking, the sentence "sql joins are bad" means nothing and I would gladly burn alive someone who tells me so ;) nah, joking!

    In this particular case, if I understood the situation well enough, the join is perfectly fine.
    Otherwise, I would have gone for a 1:n relationship (1 product: n videos) and a table dedicated exclusively to videos. Beware, I may be wrong, it depends.
  • 1
    I'm fairly certain your coworkers are wrong and their design is extremely unscalable...I saw a situation like this at my work, some code was fetching a list of object IDs, and then fetched the objects one by one. Ruined performance during load testing.
Add Comment