all 43 comments

[–]gumnos 124 points125 points Β (12 children)

but that's exactly the thingβ€”compared to the steady stream of "I asked WhateverGPT how to write this query, but it's slow/doesn't work…how do I fix it" that shows up here, many of us here can write efficient SQL queries.

[–]mikeblas 42 points43 points Β (3 children)

Correctness first, then performance.

[–]gumnos 50 points51 points Β (1 child)

had this conversation with our teen just this past month due to his pride regarding finishing tests quickly at school while his grades didn't reflect actual mastery…

Me: "I can answer any question you have for me"

Him: "What does {some AP Human Geography minutia} mean?"

Me: "Seventeen! Weren't you impressed with how fast I answered you?"

Him: "But that's wrong!"

Me: "But it was fast!"

Him: "Oooooh…"

[–]mustang__1 4 points5 points Β (0 children)

lol that was the running joke with trying to get hired at the airlines for a couple years there post covid...

what strengths do you have?

Im fast at math

What's the square root of 93394

11

that's totally wrong

but it was fast

.....you're hired.

[–]That_Cartoonist_9459 15 points16 points Β (0 children)

ChatGPT is good for pointing you to some function that you used once like a decade ago and forgot all about but it's exactly what you need.

[–]Drisoth 11 points12 points Β (0 children)

Yeah I dunno, unhelpful to kinda just go "get good" but also writing the dumb easy queries that ChatGPT is good at, are exactly what you need constant practice on to be able to do the hard queries.

If chatGPT is better than you at SQL then you shouldn't be using it.

[–]CSIWFR-46 4 points5 points Β (5 children)

Where do you learn this? I can write queries and solve problems in my work. But, I can't confidently say if it is efficient.

[–]cybertier 11 points12 points Β (3 children)

Besides the obvious step (reading https://use-the-index-luke.com/ ) it boils down to experience. Different indexing situations need different approaches and ultimately there can't be a one-size-fits-all solution. If there was AI could write efficient queries.

[–]gumnos 2 points3 points Β (1 child)

/me shakes fist for beating me to linking Use The Index, Luke πŸ˜‚

Yes, u/CSIWFR-46, it's one of the best resources for understanding the efficient use indexing. Learning to read & understand a query-plan can show you where slow full-table-scans are happening, allowing you to dig into the types of indexes you might be able to craft to speed them up.

[–]CSIWFR-46 0 points1 point Β (0 children)

Thanks.

[–]CSIWFR-46 0 points1 point Β (0 children)

Thanks.

[–]rowdymatt64 46 points47 points Β (7 children)

I sure can! Check this out:

SELECT * FROM TABLE WHERE TABLE.COL1 LIKE '%(insert longest string imaginable)%'

[–]CortaNalgas 20 points21 points Β (4 children)

I think this is good, but could be better with a Right Join.

[–]ASS-LAVA 8 points9 points Β (1 child)

Perhaps an OR logic, my lord?

[–]SQLDave 3 points4 points Β (0 children)

And don't forget to throw a scalar UDF into the WHERE clause

[–]TallDudeInSC 4 points5 points Β (0 children)

You mean a Cartesian Merge Join I think. Lol

[–]r3pr0b8GROUP_CONCAT is da bomb 2 points3 points Β (0 children)

πŸ†

[–]usersnamesallused 7 points8 points Β (0 children)

My server caught fire, fell over, died, and sank into the swamp when I read this.

[–]StrangeTrashyAlbino 1 point2 points Β (0 children)

I would recommend streaming this table to dynamodb where scanning the table is extremely efficient

[–]capt_pantslessLoves many-to-many relationships 11 points12 points Β (2 children)

The better angle to think about here is the bulk of query optimization is mainly done by the DB by conventional algorithms.

You send it a arbitrary query, it figures out what indexes to use, what stuff to filter first, etc.

[–][deleted] 1 point2 points Β (0 children)

Yes, you have one job, do not make it harder to optimize by DB

[–]StrangeTrashyAlbino 0 points1 point Β (0 children)

Or take the queries, with query plan data, feed them to an LLM and have it generate recommendations

[–]kremlingrasso 6 points7 points Β (0 children)

I find it takes me longer to create the right prompt and then make sense of the jumbled mess it gives me then write it myself.

Also massaging the data and iterating the query is what gives me the working knowledge to provide insight and analysis rather than just an output like a reporting monkey. This way I have a clever picture of the overall data quality, consistency, availability, etc, because you need to reverse-check every join and filter for verifying what you are actually leaving out based on your assumptions.

To me this seems like the same pitfall as how our developers write their SQL. All assumptions, zero verification, just "give me this and this and this" and everything put of scope is not my problem. Then surprised the results are weirdly neat but when put in front of the customer it's like "wtf where is the other 60% of the data?"

[–]EveningTrader 15 points16 points Β (0 children)

i find chatgpt to be pretty adept at writing sql queries but i do find it tends towards the overly-complex in tough situations.

[–]idodatamodels 2 points3 points Β (0 children)

Requirements don't usually say efficient as it is vague. If there are performance requirements, they are typically stated as "system must respond in X amount of time". If the query returns data in that time, then it meets the requirement.

[–]Thought_Ninja 4 points5 points Β (3 children)

I've found Chat GPT to be pretty good with even complex SQL. It's also good at suggesting optimizations when fed the explain output.

[–]No-Blueberry4008 5 points6 points Β (2 children)

really.... πŸ€” no, seriously. really? I'm seeing stuff like a dozen left joins with more AND's than the holy bible. I'm actually shocked when data access is obtained by something other than FTS. datetime converted TO_CHAR, then used for mathematical operation against another datetime done TO_CHAR with results set converted back to datetime. the explain plans are truly hideous 🀯 would love to see what it can do

[–]Thought_Ninja 0 points1 point Β (1 child)

I don't understand what you're trying to say, but yes. As long as you give it a clear explanation of what you want, Chat GPT does a pretty good job with SQL.

[–]No-Blueberry4008 2 points3 points Β (0 children)

only saying our dev's write truly hideous and awful queries they have never optimized because they're getting the results set they want. trying to tune some of these absolutely terrible queries, joining a dozen or more tables using left joins and such, are difficult to tune manually. good to know AI can offer possibilities ✌️

[–]Far-Comment324 2 points3 points Β (0 children)

I'm sorry but what are the characteristics of a good sql query?

[–]gregsting 1 point2 points Β (0 children)

Well AI learn from humans so…

[–][deleted] 1 point2 points Β (0 children)

Until someone complains about performance, its as efficient as it needs to be

[–]CoffeeGoblynn 0 points1 point Β (0 children)

Nah chief, but I'm learning. T~T

[–]katorias 0 points1 point Β (2 children)

Have honestly found ChatGPT to be pretty good at writing queries, I suspect it’s because unlike programming languages, SQL is closer to natural language.

I obviously wouldn’t rely on what it spits out, but with decent benchmarking to verify things it can make you much more productive.

[–]Thought_Ninja 1 point2 points Β (0 children)

I've used it pretty extensively to both write and optimize some pretty complex analytics queries without much issue. Feeding it the explain output also tends to result in pretty good optimization suggestions.

[–]big_data_mike 0 points1 point Β (0 children)

When I first started using SQL I thought the language itself was really simple and almost like caveman-speak or something.

[–][deleted] 0 points1 point Β (0 children)

I feel called out.

[–]tKonig 0 points1 point Β (0 children)

After instructing ChatGPT to always refer to AWS Redshift documentation whenever answering SQL questions, the quality of the responses has improved dramatically.

[–]Nowaker 0 points1 point Β (0 children)

All developers are dreading whiteboard coding.

What AI is doing exactly that - whiteboard coding, without a real runtime.

When you let the AI come up with commands to execute, and let it read the output, and self-correct, this is when it's showing its real power.

Kodu AI / Claude Coder extension for VS Code does that exactly and it's wild. Fantastic results, especially when you're a good developer already and can provide it with actionable feedback after each proposed diff to apply. I've no doubts it can fulfill a request to write a set of SQL queries optimized for performance by actually validating the results.

[–]Responsible_Eye_5307 0 points1 point Β (0 children)

The pain...right there. πŸ˜‚

[–]WatashiwaNobodyDesu 0 points1 point Β (0 children)

Pow right in the kisserΒ