r/SQLOptimization • • Sep 08 '25

SQL Struggles: Share Your Most Frustrating Moments in Writing Queries

I’m working on a small AI project that generates and optimizes SQL queries. Curious: what’s the most frustrating part of writing or optimizing SQL in your work?

11 Upvotes

20 comments sorted by

•

u/mikeblas Sep 08 '25

I'll approve this for now, since it's tenuously related to SQL optimization.

→ More replies (1)

5

u/chunkyks Sep 09 '25

Recently I was consuming xml in sql and it made me want to murder someone. Xml, such a good idea but such a horrifying pain to actually work with.

1

u/Hairy_Border_7568 Sep 17 '25

Yeah XML inside SQL is brutal 😅 I’m actually building a query optimization agent — this kind of pain point is exactly what I’m trying to tackle.

1

u/Informal_Pace9237 Sep 09 '25

In Which RDBMS flavor?

2

u/chunkyks Sep 09 '25

I was using postgres. Most recently I ended up writing this hideous thing:

https://github.com/chunky/pgtcx/blob/main/setup.sql#L21

1

u/many_hats_on_head Sep 11 '25

I have optimzied the query further:

DROP VIEW IF EXISTS activity CASCADE;
CREATE VIEW activity AS
SELECT
  tcx.tcxid AS tcxid,
  (xpath('./tcx:Id/text()', a.activity_xml, a.ns))[1]::text AS activityid,
  (xpath('./@Sport', a.activity_xml, a.ns))[1]::text AS Sport,
  (xpath('./tcx:Notes/text()', a.activity_xml, a.ns))[1]::text AS Notes,
  to_timestamp((xpath('.//tcx:Lap/@StartTime', a.activity_xml, a.ns))[1]::text,
               'YYYY-MM-DD"T"HH24:MI:SS"Z"')::timestamp AS LapStartTime,
  (xpath('.//tcx:Lap/tcx:TotalTimeSeconds/text()', a.activity_xml, a.ns))[1]::text::real AS TotalTimeSeconds,
  (xpath('.//tcx:Lap/tcx:DistanceMeters/text()', a.activity_xml, a.ns))[1]::text::real AS DistanceMeters,
  (xpath('.//tcx:Lap/tcx:MaximumSpeed/text()', a.activity_xml, a.ns))[1]::text::real AS MaximumSpeed,
  (xpath('.//tcx:Lap/tcx:Calories/text()', a.activity_xml, a.ns))[1]::text::real AS Calories,
  (xpath('.//tcx:Lap/tcx:AverageHeartRateBpm/tcx:Value/text()', a.activity_xml, a.ns))[1]::text::real AS AverageHeartRateBpm,
  (xpath('.//tcx:Lap/tcx:MaximumHeartRateBpm/tcx:Value/text()', a.activity_xml, a.ns))[1]::text::real AS MaximumHeartRateBpm,
  (xpath('.//tcx:Lap/tcx:Intensity/text()', a.activity_xml, a.ns))[1]::text AS Intensity
FROM tcx
CROSS JOIN LATERAL (
  -- create namespace array once and extract the first Activity node once per row
  SELECT
    (xpath('/tcx:TrainingCenterDatabase/tcx:Activities/tcx:Activity', tcx.body, ns))[1]::xml AS activity_xml,
    ns
  FROM (VALUES (ARRAY[ARRAY['tcx', 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2']]))
    AS v(ns)
) a;

Table comparison:

FeatureOriginal query (Repetitive XPATH)Optimized query (LATERAL JOIN)Winner
PerformanceVery Poor. The database must parse and traverse the entire XML document from the root for every single column in the SELECT list. For 11 columns, that's 11 full XML document traversals per row.Excellent. The database traverses the full XML document only once per row to extract the relevant <Activity> node. All subsequent operations run on this much smaller, in-memory XML fragment, which is dramatically faster.Optimized query
ReadabilityPoor. The SELECT list is cluttered with very long, repetitive XPath strings and namespace definitions. It's hard to see the structure at a glance.Excellent. The logic is clean and separated. The LATERAL join clearly states "first, find the Activity node." The SELECT list then uses short, simple, relative paths (./@Sport, ./tcx:Notes/text()) that are easy to read and understand.Optimized query
MaintainabilityVery Poor. It violates the DRY (Don't Repeat Yourself) principle. If the path to the <Activity> node ever needed to change, you would have to edit it in 11 different places. The namespace is also repeated 11 times. This is error-prone.Excellent. If the path to the <Activity> node changes, you only need to update it in one place inside the LATERAL subquery. The namespace is also defined only once.Optimized query

Try it yourself.

1

u/Hairy_Border_7568 Sep 17 '25

Nice! I’m also building a query optimization agent — cool to see others working in this space

→ More replies (4)
→ More replies (1)
→ More replies (1)

1

u/alinroc Sep 10 '25

Most frustrating when writing - getting clear requirements.

Most frustrating when optimizing:

  • Quirky client libraries or code generators that decide they know know best, making it more difficult to write a query that the optimizer will handle.
  • Developers who think they can apply DRY principles to their database schemas which all work great with 100K records on a table in the dev environment but fall flat on their face with 100M records in production, and then refuse to accept that their code isn't compatible with how the RDBM "thinks".

1

u/Hairy_Border_7568 Sep 17 '25

Totally agree — I’m building a query optimization agent and these are exactly the cases I’m trying to address.

1

u/Known-Delay7227 Sep 11 '25

Most of my struggles are reading poorly written queries…nested queries upon nested queries with select *’s and no alias’s. So fun yaay

1

u/Hairy_Border_7568 Sep 17 '25

Yeah, those are painful 😅 I’m building a query optimization agent, so I’ve been collecting examples like this

1

u/Unnam Sep 12 '25

Finding the right data fields from the right tables, knowing which ones to use and which ones to ignore. The right filters to be added. DATE related where etc clauses

1

u/Hairy_Border_7568 Sep 17 '25

That’s a real challenge. I’m working on a query optimization agent, and these kinds of filter decisions are what I’m focusing on.